QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#773035 | #9551. The Emperor | renew | RE | 0ms | 0kb | Python3 | 1.7kb | 2024-11-22 23:59:31 | 2024-11-22 23:59:31 |
answer
import sys
sys.setrecursionlimit(10**6) # 增加递归深度限制
MOD = 998244353
MAXN = 2005
# 初始化全局变量
f = [[-1] * MAXN for _ in range(MAXN)]
g = [[-1] * MAXN for _ in range(MAXN)]
vis = [[0] * MAXN for _ in range(MAXN)]
a = [0] * MAXN
b = [0] * MAXN
c = [0] * MAXN
d = [0] * MAXN
t = [0] * MAXN
# 递归函数
def dfs(u, v):
if f[u][v] != -1:
return
if vis[u][v]:
print("-1")
sys.exit(0)
vis[u][v] = 1
if t[u] == 0:
if v == 0:
f[u][v] = 0
g[u][v] = 1
vis[u][v] = 0
return
if t[u] == 1:
if v == a[u]:
f[u][v] = b[u]
g[u][v] = 1
vis[u][v] = 0
return
dfs(d[u], c[u])
dfs(f[d[u]][c[u]], v)
f[u][v] = f[f[d[u]][c[u]]][v]
g[u][v] = (1 + g[d[u]][c[u]] + g[f[d[u]][c[u]]][v]) % MOD
vis[u][v] = 0
def main():
n = int(input()) # 读取n
# 读取输入
for i in range(1, n + 1):
s = input().strip()
if s == "HALT;":
t[i] = 0
s = input().strip() # 跳过 "IF"
c[i] = int(input().strip()) # 读取条件
s = input().strip() # 跳过 "THEN GOTO"
d[i] = int(input().strip()) # 读取跳转目标
else:
t[i] = 1
a[i], b[i], c[i], d[i] = map(int, input().split()[::2]) # 读取a[i], b[i], c[i], d[i]
# 初始化 f 和 g 为 -1
for i in range(1, n + 1):
for j in range(1025):
f[i][j] = -1
g[i][j] = -1
# 开始 DFS
dfs(1, 0)
# 输出结果
print(g[1][0])
if __name__ == "__main__":
main()
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
1 HALT; PUSH 1 GOTO 1