huanglihuan @ 2024-05-09 21:04:21
永远输出 No
:
#include <bits/stdc++.h>
#include <cstdio>
#define int long long
using namespace std;
const int N = 105;
int n,m;
char mp [N] [N];
bool vis [N] [N];
void dfs (int x,int y)
{
if (x == n && y == m)
{
cout << "Yes";
exit (0);
}
vis [x] [y] = true;
if (x + 1 <= n && x + 1 >= 1 && mp [x + 1] [y] == '.' && ! vis [x + 1] [y]) dfs (x + 1,y);
if (x - 1 <= n && x - 1 >= 1 && mp [x - 1] [y] == '.' && ! vis [x - 1] [y]) dfs (x - 1,y);
if (y + 1 <= n && y + 1 >= 1 && mp [x] [y + 1] == '.' && ! vis [x] [y + 1]) dfs (x,y + 1);
if (y - 1 <= n && y - 1 >= 1 && mp [x] [y - 1] == '.' && ! vis [x] [y - 1]) dfs (x,y - 1);
cout << "No";
exit (0);
}
signed main () {
ios :: sync_with_stdio (false);
cin.tie (0),cout.tie (0);
cin >> n >> m;
for (int i = 1;i <= n;i ++)
for (int j = 1;j <= m;j ++) cin >> mp [i] [j];
dfs (1,1);
return 0;
}
by huanglihuan @ 2024-05-09 21:05:51
如有dalao回复,我会在第二天给关。
by Special_Tony @ 2024-05-09 21:11:04
@huanglihuan 你怎么可以一个小dfs结束了就说它是No呢,No只能是整个递归都结束后还没找到答案再No
by huanglihuan @ 2024-05-09 21:29:09
@Special_Tony 可是我的代码是所有的路都判断过一遍了呀?
by huanglihuan @ 2024-05-09 21:31:24
他没有路,可以走了,不就是直接输出 No,然后结束程序吗?
by J20220912 @ 2024-05-10 13:52:31
#include <bits/stdc++.h>
#include <cstdio>
#define int long long
using namespace std;
const int N = 105;
int n,m;
char mp [N] [N];
bool vis [N] [N];
void dfs (int x,int y)
{
if (x == n && y == m)
{
cout << "Yes";
exit (0);
}
vis [x] [y] = true;
if (x + 1 <= n && x + 1 >= 1 && mp [x + 1] [y] == '.' && ! vis [x + 1] [y]) dfs (x + 1,y);
if (x - 1 <= n && x - 1 >= 1 && mp [x - 1] [y] == '.' && ! vis [x - 1] [y]) dfs (x - 1,y);
if (y + 1 <= m && y + 1 >= 1 && mp [x] [y + 1] == '.' && ! vis [x] [y + 1]) dfs (x,y + 1);
if (y - 1 <= m && y - 1 >= 1 && mp [x] [y - 1] == '.' && ! vis [x] [y - 1]) dfs (x,y - 1);
// if (y + 1 <= n && y + 1 >= 1 && mp [x] [y + 1] == '.' && ! vis [x] [y + 1]) dfs (x,y + 1);
// if (y - 1 <= n && y - 1 >= 1 && mp [x] [y - 1] == '.' && ! vis [x] [y - 1]) dfs (x,y - 1);
}
signed main () {
ios :: sync_with_stdio (false);
cin.tie (0),cout.tie (0);
cin >> n >> m;
for (int i = 1;i <= n;i ++)
for (int j = 1;j <= m;j ++) cin >> mp [i] [j];
dfs (1,1);
cout << "No";
return 0;
}
by Chu_awa_xing @ 2024-05-13 13:40:22
输出no的地方改到调用dfs函数下面一行
by J20220912 @ 2024-05-14 13:38:21
y的范围要用m
by huanglihuan @ 2024-05-16 20:29:59
@Special_Tony @Chu_awa_xing @J20220912 对我改进代码有利的只有J20220912,所以我将关注他
by huanglihuan @ 2024-05-16 20:30:30
其他大佬:thx
by huanglihuan @ 2024-05-16 20:32:22
算了,都关了
此贴结