一个困惑,改动前30分,改动后100分,求教

P1605 迷宫

```cpp #include <bits/stdc++.h> using namespace std; const int N = 10; int g[N][N],n,m,cnt; int sx,sy,ex,ey; bool st[N][N]; int dx[4] = {1,-1,0,0}, dy[4] = {0,0,1,-1}; void dfs(int x, int y) { if (x == ex && y == ey) { cnt ++; return; } for (int i = 0; i < 4; i++) { int nex = x + dx[i], ney = y + dy[i]; if (nex >= 1 && ney >= 1 && nex <= n && ney <= m && g[nex][ney] == 0 && !st[nex][ney]) { //x = nex, y = ney; st[nex][ney] = true; dfs(nex,ney); st[nex][ney] = false; } } return; } int main() { int t; cin >> n >> m >> t; cin >> sx >> sy >> ex >> ey; while (t--) { int i,j; cin >> i >> j; g[i][j] = 1; } st[sx][sy] = true; dfs(sx,sy); cout << cnt << endl; return 0; } ``` 这是ac了的代码
by Arrosw @ 2022-11-11 20:29:06


第一份代码把x和y改动了,另外3个方向的x和y就不一样了
by liuxy1234 @ 2022-11-11 20:32:22


因为回溯后x和y的值变掉了: ``` for (int i = 0; i < 4; i++) { int nex = x + dx[i], ney = y + dy[i]; if (nex >= 1 && ney >= 1 && nex <= n && ney <= m && g[nex][ney] == 0 && !st[nex][ney]) { //x = nex, y = ney; st[nex][ney] = true; dfs(nex,ney); st[nex][ney] = false; } } ``` 回溯回来后的循环有用x、y给nex、ney赋值,用的应该是原来的x和y的值呀
by 6k823 @ 2022-11-11 20:32:54


@[Hua_er](/user/823061) 小笨蛋,快来看
by 王茗仟 @ 2022-11-11 21:04:34


啊啊啊明白了,谢谢各位
by Arrosw @ 2022-11-12 08:30:40


|