ACM篇:HDU 3912 -- Turn Right

深搜。

处理方向时机智一点就行了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;

const int MAX = 500;
int n;
int m;
int ent_c, exit_c;
bool visit[MAX+2][MAX+2];
bool wall_row[MAX+2][MAX+2];
bool wall_col[MAX+2][MAX+2];

void read_maze();
void right_walk();
bool _check();

int main()
{
    int T;

    scanf("%d", &T);
    while (T--)
    {
        read_maze();
        right_walk();
        printf("%s\n", _check() ? "YES" : "NO");
    }

    return 0;
}

void read_maze()
{
    memset(wall_row, 0, sizeof(wall_row));
    memset(wall_col, 0, sizeof(wall_col));

    scanf("%d %d %d %d", &n, &m, &ent_c, &exit_c);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j < m; j++)
            scanf("%d", &wall_row[i][j]);
        if (i < n)
        {
            for (int j = 1; j <= m; j++)
                scanf("%d", &wall_col[i][j]);
        }
    }
}
inline bool in_maze(int r, int c) {return (r >= 1 && r <= n && c >= 1 && c <= m);}
bool can_pass(int nr, int nc, int r, int c)
{
    if (nr == r)
        return (nc < c) ? (!wall_row[nr][nc]) : (!wall_row[r][c]);
    else 
        return (nr < r) ? (!wall_col[nr][nc]) : (!wall_col[r][c]);
}
void _dfs(int r, int c, int d)
{
    static const int ROW[] = {-1, 0, 1, 0};
    static const int COL[] = {0, -1, 0, 1};

    visit[r][c] = true;
    for (int i = -1; i < 3; i++)
    {
        int nd = (d+i+4) % 4;
        int nr = r + ROW[nd];
        int nc = c + COL[nd];
        if ((nr==n+1 && nc == exit_c) || (!nr && nc == ent_c))
            return;
        if (in_maze(nr, nc) && can_pass(nr, nc, r, c))
        {
            _dfs(nr, nc, nd);
            break;
        }
    } 
}

void right_walk()
{
    memset(visit, 0, sizeof(visit));
    _dfs(1, ent_c, 2);
    _dfs(n, exit_c, 0);
}
bool _check()
{
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (!visit[i][j]) 
                return false;
    return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值