codeforces 354D. Theseus and labyrinth bfs

D. Theseus and labyrinth
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size n × m and consists of blocks of size 1 × 1.

Each block of the labyrinth has a button that rotates all blocks 90 degrees clockwise. Each block rotates around its center and doesn't change its position in the labyrinth. Also, each block has some number of doors (possibly none). In one minute, Theseus can either push the button in order to rotate all the blocks 90 degrees clockwise or pass to the neighbouring block. Theseus can go from block A to some neighbouring block B only if block A has a door that leads to block B and block B has a door that leads to block A.

Theseus found an entrance to labyrinth and is now located in block (xT, yT) — the block in the row xT and column yT. Theseus know that the Minotaur is hiding in block (xM, yM) and wants to know the minimum number of minutes required to get there.

Theseus is a hero, not a programmer, so he asks you to help him.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in labyrinth, respectively.

Each of the following n lines contains m characters, describing the blocks of the labyrinth. The possible characters are:

  • «+» means this block has 4 doors (one door to each neighbouring block);
  • «-» means this block has 2 doors — to the left and to the right neighbours;
  • «|» means this block has 2 doors — to the top and to the bottom neighbours;
  • «^» means this block has 1 door — to the top neighbour;
  • «>» means this block has 1 door — to the right neighbour;
  • «<» means this block has 1 door — to the left neighbour;
  • «v» means this block has 1 door — to the bottom neighbour;
  • «L» means this block has 3 doors — to all neighbours except left one;
  • «R» means this block has 3 doors — to all neighbours except right one;
  • «U» means this block has 3 doors — to all neighbours except top one;
  • «D» means this block has 3 doors — to all neighbours except bottom one;
  • «*» means this block is a wall and has no doors.

Left, right, top and bottom are defined from representing labyrinth as a table, where rows are numbered from 1 to n from top to bottom and columns are numbered from 1 to m from left to right.

Next line contains two integers — coordinates of the block (xT, yT) (1 ≤ xT ≤ n, 1 ≤ yT ≤ m), where Theseus is initially located.

Last line contains two integers — coordinates of the block (xM, yM) (1 ≤ xM ≤ n, 1 ≤ yM ≤ m), where Minotaur hides.

It's guaranteed that both the block where Theseus starts and the block where Minotaur is hiding have at least one door. Theseus and Minotaur may be initially located at the same block.

Output

If Theseus is not able to get to Minotaur, then print -1 in the only line of the output. Otherwise, print the minimum number of minutes required to get to the block where Minotaur is hiding.

Examples
Input
2 2
+*
*U
1 1
2 2
Output
-1
Input
2 3
<><
><>
1 1
2 1
Output
4
Note

Assume that Theseus starts at the block (xT, yT) at the moment 0.


题意:一个n*m的图,给起点坐标和终点坐标,从起点走到终点。每个点是一堵墙或者向一个房间上下左右有门或者没门,每个点的门如题意给的。从一个点能走到下一个点的条件是两个点相对着地方都有门才能走,用时+1。在每个点有一个操作,可以把图上的每一个点顺时针旋转90度,用时+1。求起点到终点的最短用时。

我的写法:写法就是一般的bfs,比较麻烦的就是两个点之间是否能连通的问题。对于每个点,有4种状态,代表进行了在这个点进行了几次旋转操作。
        判断两个点是否能走,即判断两点之间是否有门,先写一个check函数,在旋转0次时判断往分别往上下左右走的时候哪几种门能走通。4种状态,那么就需要写4个这种函数。很麻烦,但是也可以就写一个函数然后对每次要走的方向处理一下再进行判断就行了。我们可以在纸上模拟一下,往上走且当前字符是'^'但是转动了2次,直接进行判断就是把这个字符转动2次的字符找出来再对比,但是这个题情况太多,不方便这样进行判断。但是反过来想,我对走的方向进行处理行不行。试着模拟一下,发现这样是行的。意思就是,将门转动2次与将走的方向转动2次进行判断是一样的。但是处理方向就只写一个判断函数就行了。(看的别人的,感觉这个姿势很巧啊)
关于走的方向的处理,很重要。
还有我不懂的是,为什么优先队列639ms,改成队列就是327ms了。
CODE
#include <bits/stdc++.h>

using namespace std;
const int N = 1000+10;

struct node
{
    int x,y;
    int time; ///走到一个点的用时
    int ci;   ///走到一个点时转动了几次
    node(int a,int b,int c,int d)
    {
        x = a,y = b,time = c,ci = d;
    }
    friend bool operator < (node a,node b)
    {
        return a.time > b.time;
    }
};
int n,m;
int x1,y1;     ///起点
int x2,y2;     ///终点
char s[N][N];  ///图
bool vis[N][N][6]; ///每个点有4种状态
int dir[4][2] = {{-1,0},{0,-1},{1,0},{0,1}};  ///上左下右,方向很重要!!

bool chk(int x2,int y2)   ///坐标是否合法
{
    if(x2 < 1 || x2 > n || y2 < 1 || y2 > m) return false;
    return true;
}

bool check(char c,int i)  ///走的方向是否有门
{
    if(!i) return c=='+'||c=='^'||c=='L'||c=='R'||c=='D'||c=='|';
    else if(i==3) return c=='+'||c=='-'||c=='>'||c=='L'||c=='U'||c=='D';
    else if(i==2) return c=='+'||c=='|'||c=='v'||c=='L'||c=='R'||c=='U';
    else return c=='+'||c=='<'||c=='-'||c=='R'||c=='U'||c=='D';
}

int bfs(int x1,int y1)
{
    memset(vis,false,sizeof vis);
    queue<node> q;
    q.push(node(x1,y1,0,0));
    while(!q.empty())
    {
        node u = q.front();
        q.pop();
        if(u.x == x2 && u.y == y2)
        {
            return u.time;
        }
        if(vis[u.x][u.y][(u.ci+1)%4] == false)
        {
            q.push(node(u.x,u.y,u.time+1,(u.ci+1)%4));
            vis[u.x][u.y][u.ci] = true;
        }
        for(int i = 0;i < 4;i++)
        {
            int tx = u.x+dir[i][0];
            int ty = u.y+dir[i][1];
            if(!chk(tx,ty)) continue;
            if(!vis[tx][ty][u.ci] && check(s[u.x][u.y],(u.ci+i)%4) && check(s[tx][ty],(u.ci+i+2)%4))
            {
                q.push(node(tx,ty,u.time+1,u.ci));
                vis[tx][ty][u.ci] = true;
            }
        }
    }
    return -1;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++)
        scanf("%s",s[i]+1);
    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    int ans = bfs(x1,y1);
    printf("%d\n",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值