HDU - 3912 Turn Right

Turn Right

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

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
2011 Multi-University Training Contest 8 - Host by HUST

Recommend
lcy | We have carefully selected several similar problems for you: 3914 3913 3911 3910 3916

墙给的数组比较难懂
使劲看吧
就是让从起点到终点在从终点到起点 看是否跑完全部格子
有可坑点就是到了出口那个格子也不算出去只有到了那个格子并且拐出去才算出去
eg
1
3 4 1 2
0 0 0
1 1 0 1
0 0 0
0 1 1 0
1 0 0

YES

#include<stdio.h>
#include<algorithm>
#include<string.h>


using namespace std;

int r,c,ec,exc;
int vis[600][600];
int count1[600][600];

int wall[1010][1010];
int dir[4][2]={0,-1,1,0,0,1,-1,0};

int check(int sx,int sy,int i)
{   

    int ex = sx+dir[i][0];
    int ey = sy+dir[i][1];
    if(ex<0||ey<0||ex>=r||ey>=c)
    {
        return 0;
//      printf("!!1\n");
    }
    if(vis[ex][ey] == 1)
    {
        return 0;
//      printf("!!2\n");
    }
    switch(i)
    {
        case 0:
            if(wall[2*sx+1][sy-1] == 1)
            {
//              printf("%d %d %d %d %d",sx,sy,2*sx+1,sy-1,wall[2*sx+1][sy-1]);
//              printf("!!3\n");
                return 0;
            }
            break;
        case 1:
            if(wall[2*sx+2][sy] == 1)
            {
//              printf("!!4\n");
                return 0;
            }
            break;
        case 2:
            if(wall[2*sx+1][sy] == 1)
            {
//              printf("!!5\n");
                return 0;
            }
            break;
        case 3:
            if(wall[2*sx][sy] == 1)
            {
//              printf("!!6\n");
                return 0;
            }
            break;

    }
    return 1;
}
int FF = 0;
int dfs(int sx,int sy,int ex,int ey,int pre,int out)
{
    if(FF == 1)
    {
        return 0;
    }
    vis[sx][sy] = 1;
    count1[sx][sy] = 1;
    int nx = sx;
    int ny = sy;
//  printf(" %d %d\n",nx,ny);


        int four = 3;
        int i = pre;
        while(four--)
        {
            i %= 4;
            if(nx == ex&&ny == ey&&i == out)
            {
                FF = 1;
                return 0;
            }
            int nextx = sx+dir[i][0];
            int nexty = sy+dir[i][1];
            if(check(nx,ny,i))
            {
//              printf("%d %d %d\n",nextx,nexty,i);
                vis[nextx][nexty] = 1;
                count1[sx][sy] = 1;
                dfs(nextx,nexty,ex,ey,(i+3)%4,out);
            }
            i++;
        }
    return 0;
}



int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(vis,0,sizeof(vis));
        memset(wall,0,sizeof(wall));
        memset(count1,0,sizeof(count1));

        scanf("%d%d%d%d",&r,&c,&ec,&exc);
        for(int i = 1;i < 2*r;i++)
        {
            if(!(i%2))//2 4
            {
                for(int j = 0;j < c;j++)
                {
                    scanf("%d",&wall[i][j]);
                }
            }
            else//1 3
            {
                for(int jj = 0;jj < c-1;jj++)
                {
                    scanf("%d",&wall[i][jj]);
                }
            }
        }
//      for(int i = 0;i < 2*r-1;i++)
//      {
//          if(i%2)//1 3
//          {
//              for(int j = 0;j < c;j++)
//              {
//                  printf("%d ",wall[i][j]);
//              }
//          }
//          else//0 2
//          {
//              for(int jj = 0;jj < c-1;jj++)
//              {
//                  printf("%d ",wall[i][jj]);
//              }
//          }
//          printf("\n"); 
//      }
//      
//      for(int i = 0;i < 7;i++)
//      {
//          for(int j = 0;j<4;j++)
//          {
//              printf("%d ",wall[i][j]);
//          }
//          printf("\n");
//      }
        FF = 0;
        dfs(0,ec,r-1,exc,0,1);

        int sum = 0;


        memset(vis,0,sizeof(vis));
        FF = 0;
        dfs(r-1,exc,0,ec,2,3);

        for(int i = 0;i < r;i++)
        {
            for(int j = 0;j<c;j++)
            {
                sum+=count1[i][j];
            }
        }
//      printf("%d\n",sum);
        if(sum == r*c)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值