HDU 3912 (13.07.08)
来源:互联网 发布:macpro怎么下载软件 编辑:程序博客网 时间:2024/06/04 19:15
Turn Right
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 881 Accepted Submission(s): 297
Problem Description
This summer, ELT and his classmates went to Beijing for a training of coding. ELT have never been to Beijing before, so at the weekend, he together with some friends went to the National Museum, it's free for students!
The National Museum consists of many parts. One part of it is an exhibition of Ancient China From Xia Dynasty to Qing Dynasty, it needs a big room to show all the things. What's more, there exist many walls to hang pictures. The boundary of this room is walls except the entrance and exit.
With walls, an entrance and an exit, this room can be regarded as a maze. To make it simple, this room is a R*C grid, wall is constructed at some edges of grid. The entrance is always at the first row, and the exit is always at the last row, just like the picture below.
ELT can't remember his direction in maze, but he is a clever boy. He knew an algorithm called "Always Turn Right", it's procedure is as follows: at any grid of this room, if we can turn right(no wall at right side), then we must turn right; if we can't turn right but can go straight forward, then we must go forward; if we can't go forward but can turn left, then we must turn left; if we can't even turn left, we just turn backward. In the picture above, if we use this algorithm, we'll visit these grids in order: Entrance --> (0, 1) --> (0, 0) --> (0, 1) --> (0, 2) --> (1, 2) --> (1, 1) --> (1, 0) --> (2, 0) --> (1, 0) --> (1, 1) --> (2, 1) --> (2, 2) --> Exit. Very easy, doesn't it?
ELT uses "Always Turn Right" algorithm to visit this room from entrance to exit, and then from exit to entrance. He wants to know whether he walked all grids in the room. Now ELT is dizzy because the maze is too big, can you help him?
The National Museum consists of many parts. One part of it is an exhibition of Ancient China From Xia Dynasty to Qing Dynasty, it needs a big room to show all the things. What's more, there exist many walls to hang pictures. The boundary of this room is walls except the entrance and exit.
With walls, an entrance and an exit, this room can be regarded as a maze. To make it simple, this room is a R*C grid, wall is constructed at some edges of grid. The entrance is always at the first row, and the exit is always at the last row, just like the picture below.
ELT can't remember his direction in maze, but he is a clever boy. He knew an algorithm called "Always Turn Right", it's procedure is as follows: at any grid of this room, if we can turn right(no wall at right side), then we must turn right; if we can't turn right but can go straight forward, then we must go forward; if we can't go forward but can turn left, then we must turn left; if we can't even turn left, we just turn backward. In the picture above, if we use this algorithm, we'll visit these grids in order: Entrance --> (0, 1) --> (0, 0) --> (0, 1) --> (0, 2) --> (1, 2) --> (1, 1) --> (1, 0) --> (2, 0) --> (1, 0) --> (1, 1) --> (2, 1) --> (2, 2) --> Exit. Very easy, doesn't it?
ELT uses "Always Turn Right" algorithm to visit this room from entrance to exit, and then from exit to entrance. He wants to know whether he walked all grids in the room. Now ELT is dizzy because the maze is too big, can you help him?
Input
First line is an integer T, means T test cases. In each test case, the first line has four numbers: R, C, Ent_Column, Exit_Column. Ent_Column is the column number of entrance; Exit_Column is the column number of exit.
Then following 2*R-1 lines, 2*i line have C-1 numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i, j+1), 2*i+1 line have C numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i+1, j). Number 1 represents a wall, 0 represents no wall.
We guarantee that there exists a path from entrance to exit.
2 <= R, C <= 500
0 <= Ent_Column, Exit_Column < C
Then following 2*R-1 lines, 2*i line have C-1 numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i, j+1), 2*i+1 line have C numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i+1, j). Number 1 represents a wall, 0 represents no wall.
We guarantee that there exists a path from entrance to exit.
2 <= R, C <= 500
0 <= Ent_Column, Exit_Column < C
Output
If ELT can walk all grids in the room, print one line "YES", otherwise, print one line "NO".
Sample Input
13 4 1 20 0 01 1 0 10 0 00 0 0 01 0 0
Sample Output
YES一开始不知道怎么做, 看过一份别人的题解后, 一下就出来了~这里定义了一个结构体, 表示走迷宫的人的坐标以及面朝哪个方向(f)下:0 左:1 上:2 右:3然后代码虽长, 但是很容易懂~AC代码如下:#include<stdio.h>#include<stdlib.h>#include<string.h>struct Point{ int x, y, f; Point(int a, int b, int d) { x = a; y = b; f = d; }};int num;int R, C, EN, EX;int map1[505][505];int map2[505][505];int vis[505][505];void init() { scanf("%d%d%d%d", &R, &C, &EN, &EX); memset(vis, 0, sizeof(vis)); num = 0; for(int i = 0; i < 2*R-1; i++) { if(i & 1) { for(int j = 0; j < C; j++) scanf("%d", &map1[i/2][j]); } else{ for(int j = 0; j < C-1; j++) scanf("%d", &map2[i/2][j]); } }}void DFS(Point cur, Point end) { int f = cur.f; while(1) { if(!vis[cur.x][cur.y]) num++; vis[cur.x][cur.y] = 1; if(cur.x == end.x && cur.y == end.y) { if((cur.f == 1 && f == 2) || (cur.f == 3 && f ==0)) break; if(cur.f == f && f == 0 && (cur.y == 0 || map2[cur.x][cur.y-1]) == 1) break; if(cur.f == f && f == 2 && cur.y == C-1 || map2[cur.x][cur.y] == 1) break; } if(cur.f == 0) { if(cur.y > 0 && map2[cur.x][cur.y-1] == 0) { cur.y--; cur.f = 1; } else if(cur.x+1 < R && map1[cur.x][cur.y] == 0) { cur.x++; cur.f = 0; } else if(cur.y+1 < C && map2[cur.x][cur.y] == 0) { cur.y++; cur.f = 3; } else if(cur.x > 0 && map1[cur.x-1][cur.y] == 0) { cur.x--; cur.f = 2; } } else if(cur.f == 1) { if(cur.x > 0 && map1[cur.x-1][cur.y] == 0) { cur.x--; cur.f = 2; } else if(cur.y > 0 && map2[cur.x][cur.y-1] == 0) { cur.y--; cur.f = 1; } else if(cur.x+1 < R && map1[cur.x][cur.y] == 0) { cur.x++; cur.f = 0; } else if(cur.y+1 < C && map2[cur.x][cur.y] == 0) { cur.y++; cur.f = 3; } } else if(cur.f == 2) { if(cur.y+1 < C && map2[cur.x][cur.y] == 0) { cur.y++; cur.f = 3; } else if(cur.x > 0 && map1[cur.x-1][cur.y] == 0) { cur.x--; cur.f = 2; } else if(cur.y > 0 && map2[cur.x][cur.y-1] == 0) { cur.y--; cur.f = 1; } else if(cur.x+1 < R && map1[cur.x][cur.y] == 0) { cur.x++; cur.f = 0; } } else if(cur.f == 3) { if(cur.x+1 < R && map1[cur.x][cur.y] == 0) { cur.x++; cur.f = 0; } else if(cur.y+1 < C && map2[cur.x][cur.y] == 0) { cur.y++; cur.f = 3; } else if(cur.x > 0 && map1[cur.x-1][cur.y] == 0) { cur.x--; cur.f = 2; } else if(cur.y > 0 && map2[cur.x][cur.y-1] == 0) { cur.y--; cur.f = 1; } } }}void solve() { Point begin = Point(0, EN, 0); Point end = Point(R-1, EX, 2); DFS(begin, end); DFS(end, begin); if(num == R*C) printf("YES\n"); else printf("NO\n");}int main() { int Case; scanf("%d", &Case); while(Case--) { init(); solve(); } return 0;}
- HDU 3912 (13.07.08)
- HDU 1175 (13.07.08)
- HDU 3912 Turn Right
- HDU 3912 Turn Ringht
- hdu 3912 模拟
- HDU 3999 (13.07.07)
- HDU 1228 (13.07.13)
- HDU 3732 (13.07.25)
- HDU 4500 (13.07.31)
- HDU 4510 (13.07.31)
- HDU 4508 (13.07.31)
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- java线程之间状态的转换
- GSM900/1800MHZ
- 【ant】ant property 总结
- 一点vs属性配置的体会
- Android Studio初步使用教程
- HDU 3912 (13.07.08)
- puts()函数和printf函数的区别
- 当程序崩溃的时候怎么办
- x86结构机器码识别及其反汇编
- 【转】Java虚拟机学习 - 垃圾收集器
- 如何使用Git源控制与Xcode在iOS 6
- HDOJ4315[阶梯博弈]
- linux debug : addr2line追踪出错地址
- 一个相当牛的类似YY多人语音视频会议系统源码 文档齐全 .