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");
        }
    }

}
源码地址: https://pan.quark.cn/s/d1f41682e390 miyoubiAuto 米游社每日米游币自动化Python脚本(务必使用Python3) 8更新:更换cookie的获取地址 注意:禁止在B站、贴吧、或各大论坛大肆传播! 作者已退游,项目不维护了。 如果有能力的可以pr修复。 小引一波 推荐关注几个非常可爱有趣的女孩! 欢迎B站搜索: @嘉然今天吃什么 @向晚大魔王 @乃琳Queen @贝拉kira 第三方库 食用方法 下载源码 在Global.py中设置米游社Cookie 运行myb.py 本地第一次运行时会自动生产一个文件储存cookie,请勿删除 当前仅支持单个账号! 获取Cookie方法 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 按刷新页面,按下图复制 Cookie: How to get mys cookie 当触发时,可尝试按关闭,然后再次刷新页面,最后复制 Cookie。 也可以使用另一种方法: 复制代码 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 控制台粘贴代码并运行,获得类似的输出信息 部分即为所需复制的 Cookie,点击确定复制 部署方法--腾讯云函数版(推荐! ) 下载项目源码和压缩包 进入项目文件夹打开命令行执行以下命令 xxxxxxx为通过上面方式或取得米游社cookie 一定要用双引号包裹!! 例如: png 复制返回内容(包括括号) 例如: QQ截图20210505031552.png 登录腾讯云函数官网 选择函数服务-新建-自定义创建 函数名称随意-地区随意-运行环境Python3....
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值