Codeforces 196 B. Infinite Maze(bfs)
2018-01-04 21:58
459 查看
We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the plane became an infinite maze.
Now on this plane cell (x, y) is a wall if and only if cell
is
a wall.
In this problem
is a remainder of dividing number a by
number b.
The little boy stood at some cell on the plane and he wondered whether he can walk infinitely far away from his starting position. From cell (x, y) he can go to one of the following cells: (x, y - 1), (x, y + 1), (x - 1, y) and (x + 1, y),
provided that the cell he goes to is not a wall.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 1500) — the height and the width of the maze that the boy
used to cyclically tile the plane.
Each of the next n lines contains m characters — the description of the labyrinth. Each character is either a "#", that marks a wall, a ".",
that marks a passable cell, or an "S", that marks the little boy's starting point.
The starting point is a passable cell. It is guaranteed that character "S" occurs exactly once in the input.
Output
Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes).
Example
Input
Output
Input
Output
Note
In the first sample the little boy can go up for infinitely long as there is a "clear path" that goes vertically. He just needs to repeat the following steps infinitely: up, up, left, up, up, right, up.
In the second sample the vertical path is blocked. The path to the left doesn't work, too — the next "copy" of the maze traps the boy.
题解:
这题的思路就是只要能从一张图的一个位置走到另一个相同位置但是坐标不一样就算可以到无限远了,一开始以为很简单的宽展个9张图就可以了。。然后到25组就wa了
后来仔细想想不止9张图,就用数组visx[i][j]记录下经历这张图的这个位置x,visy[i][j]记录位置y,只要是第二次经过一张图的同一个位置就输出yes
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
#define ll long long
int n,m;
struct node
{
int x,y;
};
int dirx[4]={0,0,-1,1};
int diry[4]={-1,1,0,0};
int visx[1505][1505];
int visy[1505][1505];
char s[1505][1505];
queue<node>q;
int main()
{
int i,j,k,stx,sty;
node now,next;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",s[i]);
for(j=0;j<m;j++)
{
if(s[i][j]=='S')
{
stx=i;
sty=j;
}
visx[i][j]=10086111;
visy[i][j]=10086111;
}
}
now.x=stx;
now.y=sty;
int tag=0;
visx[stx][sty]=stx;
visy[stx][sty]=sty;
q.push(now);
int xx,yy;
while(!q.empty())
{
now=q.front();
q.pop();
for(i=0;i<4;i++)
{
next.x=now.x+dirx[i];
next.y=now.y+diry[i];
xx=(next.x%n+n)%n;
yy=(next.y%m+m)%m;
if(s[xx][yy]!='#')
{
if(visx[xx][yy]==10086111)//表示之前没有经历过,随便设一个特殊值
{
visx[xx][yy]=next.x;
visy[xx][yy]=next.y;
q.push(next);
}
else if(visx[xx][yy]!=next.x||visy[xx][yy]!=next.y)
{
tag=1;
goto loop;
}
}
}
}
loop:;
if(tag)
printf("Yes\n");
else
printf("No\n");
return 0;
}
Now on this plane cell (x, y) is a wall if and only if cell
is
a wall.
In this problem
is a remainder of dividing number a by
number b.
The little boy stood at some cell on the plane and he wondered whether he can walk infinitely far away from his starting position. From cell (x, y) he can go to one of the following cells: (x, y - 1), (x, y + 1), (x - 1, y) and (x + 1, y),
provided that the cell he goes to is not a wall.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 1500) — the height and the width of the maze that the boy
used to cyclically tile the plane.
Each of the next n lines contains m characters — the description of the labyrinth. Each character is either a "#", that marks a wall, a ".",
that marks a passable cell, or an "S", that marks the little boy's starting point.
The starting point is a passable cell. It is guaranteed that character "S" occurs exactly once in the input.
Output
Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes).
Example
Input
5 4 ##.# ##S# #..# #.## #..#
Output
Yes
Input
5 4 ##.# ##S# #..# ..#. #.##
Output
No
Note
In the first sample the little boy can go up for infinitely long as there is a "clear path" that goes vertically. He just needs to repeat the following steps infinitely: up, up, left, up, up, right, up.
In the second sample the vertical path is blocked. The path to the left doesn't work, too — the next "copy" of the maze traps the boy.
题解:
这题的思路就是只要能从一张图的一个位置走到另一个相同位置但是坐标不一样就算可以到无限远了,一开始以为很简单的宽展个9张图就可以了。。然后到25组就wa了
后来仔细想想不止9张图,就用数组visx[i][j]记录下经历这张图的这个位置x,visy[i][j]记录位置y,只要是第二次经过一张图的同一个位置就输出yes
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
#define ll long long
int n,m;
struct node
{
int x,y;
};
int dirx[4]={0,0,-1,1};
int diry[4]={-1,1,0,0};
int visx[1505][1505];
int visy[1505][1505];
char s[1505][1505];
queue<node>q;
int main()
{
int i,j,k,stx,sty;
node now,next;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",s[i]);
for(j=0;j<m;j++)
{
if(s[i][j]=='S')
{
stx=i;
sty=j;
}
visx[i][j]=10086111;
visy[i][j]=10086111;
}
}
now.x=stx;
now.y=sty;
int tag=0;
visx[stx][sty]=stx;
visy[stx][sty]=sty;
q.push(now);
int xx,yy;
while(!q.empty())
{
now=q.front();
q.pop();
for(i=0;i<4;i++)
{
next.x=now.x+dirx[i];
next.y=now.y+diry[i];
xx=(next.x%n+n)%n;
yy=(next.y%m+m)%m;
if(s[xx][yy]!='#')
{
if(visx[xx][yy]==10086111)//表示之前没有经历过,随便设一个特殊值
{
visx[xx][yy]=next.x;
visy[xx][yy]=next.y;
q.push(next);
}
else if(visx[xx][yy]!=next.x||visy[xx][yy]!=next.y)
{
tag=1;
goto loop;
}
}
}
}
loop:;
if(tag)
printf("Yes\n");
else
printf("No\n");
return 0;
}
相关文章推荐
- Codeforces 196 B. Infinite Maze
- codeforces 558C C. Amr and Chemistry(bfs)
- CodeForces - 540C Ice Cave(BFS)
- Codeforces 513F2 题解 (网络流-最大流 二分 BFS)
- Codeforces 540C Ice Cave (BFS)
- Codeforces 793B Igor and his way to work (dfs/bfs
- Codeforces 730 C Bulmart [bfs+贪心]
- codeforces 793B - Igor and his way to work(bfs)
- CodeForces-540C//Codeforces Round #301 (Div. 2)C BFS
- CodeForces - 540C Ice Cave(BFS)
- codeforces 659F (并查集 bfs)
- Codeforces-194C-Cutting Figure (思维+bfs)
- Codeforces 676D - Theseus and labyrinth 【BFS(二进制优化迷宫旋转)】
- Codeforces 242C King‘s Path(BFS+STL)
- Codeforces 520B Two Buttons 【BFS】||【spfa】
- BFS (图)——Codeforces 788 C. The Great Mixing
- [codeforces525D]BFS
- Codeforces 689B. Mike and Shortcuts(BFS)
- CodeForces - 586D Phillip and Trains (BFS || DP)
- codeforces 793B - Igor and his way to work(bfs)