贪吃蛇迷宫80分求助

有点道理,吧vis删了吧

需要vis

加上vis[tx][ty] = 0;就会TLE(doge

(倔强的coder

void dfs(int x, int y, int idx)
{
    vis[x][y] = 1;
    if (ans)
        return;
    if (x == n && y == m)
    {
        ans = 1;
        return;
    }
    for (int i = 0; i < 4; i++)
    {
        int tx = x + dx[i], ty = y + dy[i];
        if (tx <= 0 || ty <= 0 || tx > n || ty > m)
            continue;
        if (mp[tx][ty] != c[(idx + 1) % 5])
            continue;
        if (vis[tx][ty])
            continue;
        dfs(tx, ty, (idx + 1) % 5);
    }
}
// main
dfs(1, 1, 0);
cout << (ans ? "Yes" : "No");

又挂了?

A了

@yhxyd0109

合在一起

所以解决方案给他了??

en

你把对于snuke的判断挪到for循环里头
再把这段删了
加上vis[tx][ty] = 0; 就会TLE(doge

解决方案

此贴结(楼主已AC)

1 个赞

结帖

此题目中不需要回溯。若回溯,在一定情况下会导致TLE,本题结果为TLE80

楼主已AC