hdu3912 Turn Right

Turn Right

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


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
 


  简单的模拟,DFS ,因为  if 后少一对括号,做了很久o(╯□╰)o;  每次按题目顺序查找能进入的第一个房间,然后记录进入的方向,然后根据进入的方向继续查找能进的房,注意的是到达出口或者入口所在的房间不能马上就停止,还得依然要安顺序看看有没有可以走的房间,值得走到出口方向。
   提供2组数据: 3   4   1   2 
           0  0  0  
                                            0  0   1   0
                                            0  1    1
                                            0   1 0 0 
                                             1  0   0
        注意是否到达   1  2  及 2  1  是否到达。答案YES    . 
                                               
          

#include<iostream>
#include<cstring>
using namespace std;
int R,C,entc,outc;
bool grid[500][500][2],vsd[500][500];
int test(){
for( int i=0;i<R;i++){
    for(int j=0;j<C;j++)
        if(vsd[i][j]==0) {return 0;}
}
return 1;
}


 void  dfs(int x,int y,int tag,int f){
     vsd[x][y]=1;
 if(tag==0&&x==R-1&&y==outc){
     if(f==1&&grid[x-1][y][1]==0&&!vsd[x-1][y]) dfs(x-1,y,tag,2);                      
    else  if(y>0&&grid[x][y-1][0]==0&&!vsd[x][y-1]) dfs(x,y-1,tag,1);
    else  dfs(x,y,1,2);
 }
else  if(tag==1&&x==0&&y==entc){
    if(f==3&&grid[1][y][1]==0&&!vsd[1][y]) dfs(1,y,tag,0);
    else if(y<C-1&&grid[x][y][0]==0&&!vsd[x][y+1]) dfs(x,y+1,tag,3);


    else if(test())cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
 }else{
 if(f==0){                                                                                                    //从右边进来
    if(y>0&&grid[x][y-1][0]==0){ dfs(x,y-1,tag,1);    }
    else if(x<R-1&&grid[x][y][1]==0) { dfs(x+1,y,tag,0);    }
    else if(y<C-1&&grid[x][y][0]==0){dfs(x,y+1,tag,3); }
    else { dfs(x-1,y,tag,2); }
 }
 else if(f==1){                                                                                   //从上边进来
    if(x>0&&grid[x-1][y][1]==0){ dfs(x-1,y,tag,2);    }
    else if(y>0&&grid[x][y-1][0]==0) { dfs(x,y-1,tag,1);    }
    else if(x<R-1&&grid[x][y][1]==0){dfs(x+1,y,tag,0); }
    else { dfs(x,y+1,tag,3); }
 }
 else if(f==2){                                                                                        //从左边进来
     if(y<C-1&&grid[x][y][0]==0){ dfs(x,y+1,tag,3);    }
    else if(x>0&&grid[x-1][y][1]==0) { dfs(x-1,y,tag,2);    }
    else if(y>0&&grid[x][y-1][0]==0){dfs(x,y-1,tag,1); }
    else { dfs(x+1,y,tag,0); }


 }else                                                                                        //从下边进来
 {
  if(x<R-1&&grid[x][y][1]==0){ dfs(x+1,y,tag,0);    }
    else if(y<C-1&&grid[x][y][0]==0) { dfs(x,y+1,tag,3);    }
    else if(x>0&&grid[x-1][y][1]==0){dfs(x-1,y,tag,2); }
    else { dfs(x,y-1,tag,1); }
 }
 }
 }


int main(void){
int t;
cin>>t;
while(t--){
  cin>>R>>C>>entc>>outc;
    for( int i=0;i<R;i++){
        for(int j=0;j<C-1;j++){
            cin>>grid[i][j][0];    //存竖向墙
        }
        if(i==R-1) break;
       for(int j=0;j<C;j++){
            cin>>grid[i][j][1];   //存横向墙
        }


    }
    memset(vsd,0,sizeof(vsd));
    dfs(0,entc,0,0);
}
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值