0
点赞
收藏
分享

微信扫一扫

计蒜客 王子救公主 dfs

zhongjh 2022-06-29 阅读 29

计蒜客 王子救公主  dfs_i++
题意;如上,注意王子有可能跨越墙,前提使正好挨着墙。
思路:dfs王子和公主,开一个三维数组分别用vis[x][y][0],vis[x][y][1]标记王子和公主走到的点。
若存在二点相交即(vis[x][y][0]&&vis[x][y][1])==1,则说明王子可以预见公主。

5 5
#...#
w..##
...#.
#..s.
...##
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<cctype>
#include<stack>
#include<map>
#include<string>
#include<cstdlib>
#define ll long long
#define N 100010
using namespace std;
const ll maxn = 1e2 + 5;
//ll a[maxn],b[maxn];
//bool vis[maxn];
int ans[maxn];
vector<int>v[maxn];
int n, m;
bool vis[maxn][maxn][2];
bool flag;
char a[maxn][maxn];
void dfs(int x,int y,int d)
{
if(x>=n||x<0||y>=m||y<0||vis[x][y][d]||a[x][y]=='#')
return ;
vis[x][y][d]=true;
dfs(x+(2-d),y,d);
dfs(x-(2-d),y,d);
dfs(x,y+(2-d),d);
dfs(x,y-(2-d),d);
return;
}
int main() {
int x, y, tx, ty;
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(a[i][j] == 'w') {
x = i;
y = j;
} else if(a[i][j] == 'g') {
tx = i;
ty = j;
}
bool flag=false;
dfs(x, y, 0);
dfs(tx, ty, 1);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(vis[i][j][0]&&vis[i][j][1])
flag=true;
if(flag)
cout<<"yes"<<endl;
else cout<<"no"<<endl;
return 0;
}


举报

相关推荐

0 条评论