goujiao @ 2023-06-19 20:48:31
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int N=100+10;
char s[N][N];
int dfs(int i,int j)
{
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
queue<pair<int,int> > q;
q.push({1,1});
while(q.size())
{
pair<int,int> t=q.front();
s[t.first][t.second]='#';
q.pop();
for(int i=0;i<4;i++)
{
int x=t.first+dx[i],y=t.second+dy[i];
if(x>=1&&x<=n&&y>=1&&y<=m&&s[x][y]=='.')
{
if(x==n&&y==m) return 0;
q.push({x,y});
}
}
}
return 1;
}
int 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>>s[i][j];
if(dfs(1,1)==0) cout<<"Yes";
else cout<<"No";
}
by goujiao @ 2023-06-19 21:04:15
这个题是不能下载测试点了吗?我找了好久没找到呜呜呜
by SealMoBurp @ 2023-06-19 21:16:52
你这是BFS吧
by SealMoBurp @ 2023-06-19 21:25:55
你应该用DFS找是否可行 BFS是找最短路
by SealMoBurp @ 2023-06-19 21:39:25
@goujiao
#include <bits/stdc++.h>
using namespace std;
int n,m,dx[] = {1,-1,0,0},dy[] = {0,0,1,-1};
char a[105][105];
void dfs(int x,int y){
if (n == x && m == y){
cout << "Yes";
exit(0);//退出程序
}
a[x][y] = '#';
for (int i = 0;i < 4;i++){
int nx = dx[i] + x,ny = dy[i] + y;
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && a[nx][ny] == '.') dfs(nx,ny);
}
}
int main(){
cin >> n >> m;
for (int i = 1;i <= n;i++) for (int j = 1;j <= m;j++) cin >> a[i][j];
dfs(1,1);
cout << "No";//可以的话已经退出了
return 0;
}
深搜AC
by goujiao @ 2023-06-19 22:42:34
呼呼,哈哈,好的佬,拜你为师了
by goujiao @ 2023-06-19 22:43:26
@Seal_ZhouMoTong 佬,exit(0)什么意思
by SealMoBurp @ 2023-06-20 17:10:00
@goujiao exit(0)是退出整个exe程序,就是直接结束,不再往下搜
by goujiao @ 2023-06-22 10:30:14
@Seal_ZhouMoTong 嗷,好的谢谢