CSUOJ 1939 World's end BFS 仔细思考
2017-05-28 22:49
363 查看
Description
Given a n × m matrix. Each cell is “.” for a road, or is “#” for a wall. A girl stood at ‘S’ and she wanted to know whether she can walk infinitely far away from her now position.we know the matrix is noly the partof the world and if cell (x, y) is a road ,then cell
( x mod n , y mod m )is a road. if cell (x, y) is a wall ,then cell
( x mod n , y mod m )is a wall.If the girl is in position(x, y), the following of she can go is (x, y - 1), (x, y + 1), (x - 1, y) and (x + 1, y).
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 1500) — the height and the width of the matrix.Each of the next n lines contains m characters.
Output
Print "Yes" (without the quotes), if she can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes).
Sample Input
5 4 ##.# ##S# #..# #.## #..# 5 4 ##.# ##S# #..# ..#. #.##
Sample Output
Yes No
Hint
Multiple test cases.好吧,一开始的想法是拓展成一个九宫格,然后看看从中间的S点能不能跑到别的格子里的S点上去,但是这样做不行的。。
因为可能会存在要绕很多的格子,最后回到一个S的情况,换一句话说,仅仅考虑9宫格是不够的。
因此 ,我们考虑不完全的把地图生成出来,仅仅在一张n×m的地图上面跑,BFS的时候队列的里面的每一个点的坐标是真实的坐标,但是在判断的时候转化为在图上的坐标去判断。
对于当前的点p,假设我们可以拓展到点q,并且q还没有被访问过,那么就把q打上 一个vis标记,并且记录一下q当前的真实坐标
若可以拓展到q且q已经被访问过了,则判断q的真实坐标和当前走到q的坐标是不是一样的,假若真实坐标不一样,则代表存在一条路径经过一些地图块后依然可以走到q点,那么此时就代表可以走到无限远处
假若真实坐标是一样的,则什么事也没有发生
只要想清楚了,代码还是不难的
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxm=1510,dx[]{0,0,1,-1},dy[]{1,-1,0,0};
typedef pair<int,int> pa;
queue<pa> q;
int n,m;
char board[maxm][maxm];
bool vis[maxm][maxm],bfs();
pa rec[maxm][maxm],s;
int main(){
ios_base::sync_with_stdio(0);
while(cin>>n>>m){
for(int i=0;i<n;++i)
for(int j=0;j<m;++j){
cin>>board[i][j];
if(board[i][j]=='S')
board[i][j]='.',s=make_pair(i,j);
}
cout<<(bfs()?"Yes\n":"No\n");
memset(vis,0,sizeof(vis[0])*n);
}
return 0;
}
bool bfs(){
while(!q.empty())q.pop();
q.push(s);
vis[s.first][s.second]=true;
rec[s.first][s.second]=s;
int tmp_x,tmp_y;
while(!q.empty()){
s=q.front();q.pop();
for(int i=0;i<4;++i){
tmp_x=((s.first+dx[i])%n+n)%n,tmp_y=((s.second+dy[i])%m+m)%m;
if(board[tmp_x][tmp_y]!='#')
if(!vis[tmp_x][tmp_y])
vis[tmp_x][tmp_y]=true,q.push(rec[tmp_x][tmp_y]=make_pair(s.first+dx[i],s.second+dy[i]));
else if(rec[tmp_x][tmp_y]!=make_pair(s.first+dx[i],s.second+dy[i]))
return true;
}
}
return false;
}
相关文章推荐
- hdu 4260 The End of The World
- hdu Yet another end of the world(扩展欧几里得定理推论)
- hdu 4596 Yet another end of the world 线性同余方程组
- My End of the World Post
- Hdu 4596 Yet another end of the world(数论)
- 关于使用DFS,BFS的一些思考总结
- HDU 4596 Yet another end of the world(GCD)
- CSUOJ 1891 Full Tank? 最短路 bfs
- HDU 4596 Yet another end of the world(解一阶不定方程)
- HDU 4596 Yet another end of the world
- HDU 4596 - Yet another end of the world(扩展欧几里得)
- 算法总结(7)--leetcode上的递归,BFS,DFS思考
- [置顶] [cocos2d-x]从源码角度思考convertToWorldSpace()与convertToWorldSpaceAR()坐标系的转换
- HDU 4569——Yet another end of the world
- poj2446 Chessboard 二分图最大匹配 思考 匈牙利算法BFS实现
- Yet another end of the world
- 【LVL1_5_c】【思考题】【7】char *p1="hellow world"和char p2[]="hellow world"的区别
- HDU 4596 J - Yet another end of the world(数论)
- [CodeChef 13 Feb-CookOff] End Of The World 2
- Word Ladder(找出start——end的最短长度)——bfs