PleasantSheep @ 2024-02-27 19:43:44
using namespace std;
int maze[105][105]={0};
int n, m;
int ans = 0;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
void dfs(int x,int y)
{
if(x==n&&y==m)
{
ans++;
}
for (int i = 0; i < 4;i++)
{
if(maze[x+dx[i]][y+dy[i]]==1)
{
maze[x + dx[i]][y + dy[i]] = 0;
dfs(x + dx[i], y + dy[i]);
maze[x + dx[i]][y + dy[i]] = 1;
}
}
}
int main()
{
cin >> n >> m;
char z;
for (int i = 1; i <= n;i++)
{
for (int j = 1; j <= m;j++)
{
cin >> z;
if(z=='.')
maze[i][j] = 1;
else
maze[i][j] = 0;
}
}
dfs(1, 1);
if(ans>=1)
cout << "Yes";
else
cout << "No";
return 0;
}
by yuanshiren_qidong @ 2024-02-27 19:52:29
em......
by zcxxcz2110 @ 2024-02-27 20:03:15
刚才现场写的
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int n, m;
int vis[110][110], walk[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
char mp[110][110];
struct node
{
int x, y;
};
queue<node> q;
void bfs(int x, int y)
{
memset(vis, -1, sizeof vis);
q.push({x, y});
vis[x][y] = 0;
while(!q.empty())
{
int xx = q.front().x, yy = q.front().y;
q.pop();
for(int i = 0; i < 4; i++)
{
int u = xx + walk[i][0], v = yy + walk[i][1];
if(1 <= u && u <= n && 1 <= v && v <= m && vis[u][v] == -1 && mp[u][v] == '.')
{
q.push({u, v});
vis[u][v] = vis[xx][yy] + 1;
}
}
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) cin >> mp[i][j];
bfs(1, 1);
if(vis[n][m] != -1) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
by JOKER_chu @ 2024-02-27 20:05:28
@PleasantSheep
改出来了,你这问题太多了,看看我改的
100分
#include <bits/stdc++.h>
using namespace std;
int maze[105][105]={0};
int n, m;
int ans = 0;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
void dfs(int x,int y)
{
if(x==n&&y==m)
{
ans++;
return ;
}
for (int i = 0; i < 4;i++)
{
if(x+dx[i] > 0 && x+dx[i] <= n && y+dy[i] > 0 && y+dy[i] <= m && maze[x+dx[i]][y+dy[i]]==0)
{
maze[x + dx[i]][y + dy[i]] = 1;
dfs(x + dx[i], y + dy[i]);
}
}
}
int main()
{
cin >> n >> m;
char z;
for (int i = 1; i <= n;i++)
{
for (int j = 1; j <= m;j++)
{
cin >> z;
if(z=='.')
maze[i][j] = 0;
else
maze[i][j] = 1;
}
}
dfs(1, 1);
if(ans>=1)
cout << "Yes";
else
cout << "No";
return 0;
}
by zcxxcz2110 @ 2024-02-27 20:10:32
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int maze[105][105]={0}, vis[105][105] = {0};
int n, m;
int ans = 0;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
void dfs(int x,int y)
{
if(x==n&&y==m)
{
ans++;
}
for (int i = 0; i < 4;i++)
{
if(1 <= x + dx[i] && x + dx[i] <= n && 1 <= y + dy[i] && y + dy[i] <= m && vis[x + dx[i]][y + dy[i]] == -1 && maze[x+dx[i]][y+dy[i]]==1)
{
vis[x + dx[i]][y + dy[i]] = vis[x][y] + 1;
maze[x + dx[i]][y + dy[i]] = 0;
dfs(x + dx[i], y + dy[i]);
maze[x + dx[i]][y + dy[i]] = 1;
}
}
}
int main()
{
cin >> n >> m;
char z;
for (int i = 1; i <= n;i++)
{
for (int j = 1; j <= m;j++)
{
cin >> z;
if(z=='.')
maze[i][j] = 1;
else
maze[i][j] = 0;
}
}
memset(vis, -1, sizeof vis);
dfs(1, 1);
if(ans>=1)
cout << "Yes";
else
cout << "No";
return 0;
}
by PleasantSheep @ 2024-02-28 20:07:21
@chuxm 感谢大佬指点!!!
by PleasantSheep @ 2024-02-28 20:08:54
@zcxxcz2110 感谢大佬!