codeforces 196 b
4076 ワード
http://codeforces.com/contest/196/problem/B
B.Infinite Maze
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
out put
スタンダードアウト
We've got a rectanglar n ×m-cell maze.Each cell is eigther passable,or is a wall.A little boy found the maze and cyclically a pline with it so that the plane became nite maze.Now cell (x,̵y) is a wall if and only if cell is a wall.
In this problem is a remander of dividing number a. by number b.
The little boy stood at some cell on the pline and he wondered whe can walk infinitely far away from his starting position.From cell (x,̵y) ヘcan go to one of the follwing cell: (x,̵y-̵1) (x,̵y̵+̵1) (x̵-̵1,̵y) and (x̵+̵1、̵y)、provided that the cel he gos 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 the pline.
Each of the next n LINE contains m character s-the description of the labyrith.Each character is either a「21879;」、that marks a wall、a「.」、that marks a passable cell、or an「S」、that marks the little boy's startpoint.
The starting point is a passable cell.It is garanted that character“S”occurs exactly once in the input.
Output
Print「Yes」、if the little boy can walk infinitely far from the starting point.Other wise、print「No」.
Examples
input
In the first sample the little boy can go up for infinitely long as there is a“clear path”that gos verticaly.He just needs to repeat the following steps infinitely:up、up、left、up、up、up、up、up、up、up、up、up、up、up、up、up、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.
この迷宫は上下左右无限につなぎ合わせることができるので、S时から无限远まで歩くことができるかどうかを闻きます.
最初、自分で書いたbfs、wa 4とmle 6はとてもばつが悪くて、他の人の書いた問題解を見て、やっと模索してきました.
考え:Sから無限まで歩くということは、S自身がある点に到達することができます.そして、この道はxにあります.
嗳include using namespace std;char s[1505][1505];int f[1505][1505];struct node{ int x,y;st,vis[1505][1505]//visはこの点に初めて来た時の座標を記録して、次に来た時の座標int xx[4]={0,1,-1};int yy[4]={1、-1,0,0};int n,m;int bfs(){ node c; f[st.x][st.y]=1; vis[st.x][st.y].x=st. x; vis[st.x][st.y].y=st.y; queue q q.push(st) while(!q.empty() { node d=q.front() q.pop() for(int i=0;i<4;i+) { int x=d.x+xx[i] int y=d.y+yy[i] node cc ccc.x=x; ccc.y=y; c.x=(x%n+n)%n c.y=(y%m+m)%m if(s[c.x][c.y]==''\33451;'のcontinue; if(!f[c.x][c.y]) { f[c.x][c.y]=1; q.push(cc)//ccであることを注意する. vis[c.x][c.y].x=x; vis[c.x][c.y].y=y; } else { if((vis[c.x]、[c.y].x!=x)𞓜(vis[c.x]、[c.y.=y)//2回目の座標は初めてと違います.true { return 1; } } } } return 0;int main(){ scanf(「%d%」&n,&m); for(int i=0;i { scanf("%s",s[i]) for(int j=0;j { if(s[i][j]='S') { st.x=i st.y=j; s[i][j]='; } } } if(bfs() { pusts("ハイ") } else { puts(「No」) } return 0;
B.Infinite Maze
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
out put
スタンダードアウト
We've got a rectanglar n ×m-cell maze.Each cell is eigther passable,or is a wall.A little boy found the maze and cyclically a pline with it so that the plane became nite maze.Now cell (x,̵y) is a wall if and only if cell is a wall.
In this problem is a remander of dividing number a. by number b.
The little boy stood at some cell on the pline and he wondered whe can walk infinitely far away from his starting position.From cell (x,̵y) ヘcan go to one of the follwing cell: (x,̵y-̵1) (x,̵y̵+̵1) (x̵-̵1,̵y) and (x̵+̵1、̵y)、provided that the cel he gos 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 the pline.
Each of the next n LINE contains m character s-the description of the labyrith.Each character is either a「21879;」、that marks a wall、a「.」、that marks a passable cell、or an「S」、that marks the little boy's startpoint.
The starting point is a passable cell.It is garanted that character“S”occurs exactly once in the input.
Output
Print「Yes」、if the little boy can walk infinitely far from the starting point.Other wise、print「No」.
Examples
input
5 4
##.#
##S#
#..#
#.##
#..#
out putYes
input5 4
##.#
##S#
#..#
..#.
#.##
out putNo
ノートIn the first sample the little boy can go up for infinitely long as there is a“clear path”that gos verticaly.He just needs to repeat the following steps infinitely:up、up、left、up、up、up、up、up、up、up、up、up、up、up、up、up、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.
この迷宫は上下左右无限につなぎ合わせることができるので、S时から无限远まで歩くことができるかどうかを闻きます.
最初、自分で書いたbfs、wa 4とmle 6はとてもばつが悪くて、他の人の書いた問題解を見て、やっと模索してきました.
考え:Sから無限まで歩くということは、S自身がある点に到達することができます.そして、この道はxにあります.
嗳include using namespace std;char s[1505][1505];int f[1505][1505];struct node{ int x,y;st,vis[1505][1505]//visはこの点に初めて来た時の座標を記録して、次に来た時の座標int xx[4]={0,1,-1};int yy[4]={1、-1,0,0};int n,m;int bfs(){ node c; f[st.x][st.y]=1; vis[st.x][st.y].x=st. x; vis[st.x][st.y].y=st.y; queue q q.push(st) while(!q.empty() { node d=q.front() q.pop() for(int i=0;i<4;i+) { int x=d.x+xx[i] int y=d.y+yy[i] node cc ccc.x=x; ccc.y=y; c.x=(x%n+n)%n c.y=(y%m+m)%m if(s[c.x][c.y]==''\33451;'のcontinue; if(!f[c.x][c.y]) { f[c.x][c.y]=1; q.push(cc)//ccであることを注意する. vis[c.x][c.y].x=x; vis[c.x][c.y].y=y; } else { if((vis[c.x]、[c.y].x!=x)𞓜(vis[c.x]、[c.y.=y)//2回目の座標は初めてと違います.true { return 1; } } } } return 0;int main(){ scanf(「%d%」&n,&m); for(int i=0;i { scanf("%s",s[i]) for(int j=0;j { if(s[i][j]='S') { st.x=i st.y=j; s[i][j]='; } } } if(bfs() { pusts("ハイ") } else { puts(「No」) } return 0;