lihans @ 2024-07-27 12:10:44
#include<bits/stdc++.h>
using namespace std;
int m,n;
char mp[1001][1001];
int dx[6]={0,0,0,-1,1};
int dy[6]={0,-1,1,0,0};
void vis(int x,int y)
{
mp[x][y]='#';
}
bool dfs(int x,int y)
{
if(x==n&&y==m) return true;
if(mp[x+dx[1]][y+dy[1]]=='#'&&mp[x+dx[2]][y+dy[2]]=='#'&&mp[x+dx[3]][y+dy[3]]=='#'&&mp[x+dx[4]][y+dy[4]]=='#')
return false;
for(int i=1;i<=4;i++)
{
if(mp[x+dx[i]][y+dy[i]]=='.')
{
vis(x+dx[i],y+dy[i]);
dfs(x+dx[i],y+dy[i]);
}
}
}
int main()
{
memset(mp,'#',sizeof(mp));
cin>>n>>m;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
cin>>mp[i][j];
}
}
if(dfs(1,1))
{
cout<<"Yes";
}
else
{
cout<<"No";
}
return 0;
}
https://www.luogu.com.cn/record/167960659
我这还有输出的,这里直接RE了 求大佬帮忙调qaq
by DreamInk @ 2024-07-27 12:25:14
@lihans 你只要不开O2,就不会RE啦
by Diamond_ZuanShi @ 2024-07-27 18:37:41
你可以用数组标记,这样比函数标记要方便,而且你是dfs,好像没回溯嘿?
by Ahws_rwhy @ 2024-08-01 10:06:58
@lihans
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
bool bfs(int n, int m, vector<vector<char>>& maze) {
vector<vector<bool>> visited(n, vector<bool>(m, false));
queue<pair<int, int>> q;
q.push({0, 0});
visited[0][0] = true;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
if (x == n-1 && y == m-1) {
return true;
}
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny] && maze[nx][ny] == '.') {
q.push({nx, ny});
visited[nx][ny] = true;
}
}
}
return false;
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<char>> maze(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> maze[i][j];
}
}
bool result = bfs(n, m, maze);
if (result) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
by Unknown_Jun @ 2024-08-09 21:40:01
21行后面还要跟一个回溯
mp[x+dx[i]][y+dy[i]]='.';
@lihans