HDU 3912 Turn Right 【水题 模拟搜索】

Turn Right

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 414    Accepted Submission(s): 159


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?
 

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
 

Output
If ELT can walk all grids in the room, print one line "YES", otherwise, print one line "NO".
 

Sample Input
1
3 4 1 2
0 0 0
1 1 0 1
0 0 0
0 0 0 0
1 0 0
 

Sample Output
YES
 

Source

表示做这种非递归非“传统”迭代的搜索总会脑抽。。。总是不自觉的想回溯或者什么的blabla。。
just按照题目给出的顺序模拟就好,简化写法就写个状态转移,三维标记状态、三维标记障碍即可。
读入建图比较坑爹,不过还是那句话,完全按照“题目所述”,就很easy。
迭代搜索那块就是写的清楚明白就绝不坑爹。

虽然有更好的做法,但是这复杂度,俨然不会超,就这样吧。何况那做法也没有啥本质上的优化。
ps,这题类似poj3083.。。
代码:
#include <iostream>
#include 
<cstdio>
#include 
<cstring>
#define maxn 505
bool vis[maxn][maxn][5],map[maxn][maxn][5];
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
int rot[] = {1,0,3,2};
int r,c,st,en;
bool check(int x,int y,int dir,bool a)
{
    
return ((x >= 1 && x <= r) && (y >= 1 && y <= c) && !vis[x][y][dir]) || (x == r + 1 && y == en && a == 0|| (x == 0 && y == st && a == 1);
}
void gao()
{
    scanf(
"%d %d %d %d",&r,&c,&st,&en);
    st
++;
    en
++;
    memset(map,
0,sizeof(map));
    
for(int i = 0;i < 2 * r - 1;i++)
    {
        
if(i % 2 == 0)
        {
            
for(int j = 1;j <= c - 1;j++)
            {
                
int a;
                scanf(
"%d",&a);
                map[i
/2+1][j][0= map[i/2+1][j+1][2= a;
            }
        }
        
else
        {
            
for(int j = 1;j <= c;j++)
            {
                
int a;
                scanf(
"%d",&a);
                map[(i
+1)/2][j][1= map[(i+1)/2+1][j][3]= a;
            }
        }
    }
    memset(vis,
0,sizeof(vis));
    
int x,y,d,tx,ty,td;
    x 
= 1;
    y 
= st;
    d 
= 1;
    
bool md = 0;
    
while(1)
    {
        
if(x == r + 1 && y == en)
        {
            x 
= r;
            y 
= en;
            d 
= 3;
            md 
= 1;
            
continue;
        }
        vis[x][y][d] 
= true;
        
bool flag = false;
        
for(int i = 0;i < 4;i++)
        {
            td 
= (d + rot[i])%4;
            tx 
= x + dx[td];
            ty 
= y + dy[td];
            
if(!map[x][y][td] && check(tx,ty,td,md))
            {
                x 
= tx;
                y 
= ty;
                d 
= td;
                flag 
= true;
                
break;
            }
        }
        
if(!flag)
            
break;
    }
    
bool flag = true;
    
for(int i = 1;i <= r;i++)
    {
        
for(int j = 1;j <= c;j++)
        {
            
bool mark = false;
            
for(int k = 0;k < 4;k++)
                
if(vis[i][j][k])
                {
                    mark 
= true;
                    
break;
                }
            
if(!mark)
            {
                flag 
= false;
                
break;
            }
        }
        
if(!flag)
            
break;
    }
    puts((flag)
?"YES":"NO");
}
int main()
{
    
int t;
    scanf(
"%d",&t);
    
for(int i = 0;i < t;i++)gao();
}

posted on 2011-08-05 21:51 BUPT-[aswmtjdsj] @ Penalty 阅读(370) 评论(0)  编辑 收藏 引用 所属分类: HDU Solution Report 模拟搜索


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2012年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(1)

随笔分类(150)

随笔档案(71)

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜