Codeforces Round #354 (Div. 2) D. Theseus and labyrinth(BFS)

1 篇文章 0 订阅

传送门:点击打开链接


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 ≤ n1 ≤ yT ≤ m), where Theseus is initially located.

Last line contains two integers — coordinates of the block (xM, yM) (1 ≤ xM ≤ n1 ≤ 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.


题意:一张图,一堆点,几扇门,能否到终点,不能则输出-1。


思路:这题体现了审清题目是十分重要的...红字比较关键,一般(wo)的思维是1为底 n为顶,白白贡献了N次WA,还有坐标的XY轴绝对不能弄反了。不然就会出现这种莫名其妙的WA:


2(在机器上提交和在codeforces提交的结果不同,说到底还是程序写的不对,但不容易查错)
Time: 0 ms, memory: 21804 KB
Verdict: WRONG_ANSWER
Input
2 3
<><
><>
1 1
2 1
Participant's output
293666487
Jury's answer
4
Checker comment
wrong answer expected '4', found '293666487'

剩下的就比较简单了,一次90°,可以转换为四张地图,也就是三维bfs。


其他要注意的点:1.转一次是全图都转,而不是仅仅所在的那块。

                             2.转动为顺时针90°,而并非逆时针。

                             3.起点和终点可能为同一个点。

                             4.俩块间必须相互连通才能移动。



#include<bits/stdc++.h>
using namespace std;
int n,m;
char mp[4][1005][1005]={0};
int hav[4][1005][1005]={0};
struct node{
    int a;
    int b;
    int time;
    int type;
    friend bool operator < (node a,node b){
        return a.time>b.time;
    }
};
int bg1,bg2,ed1,ed2;
int dir[5][2]={{0,1},{1,0},{0,-1},{-1,0},{0,0}};
bool check(int x1,int y1,int di,int type){
    if(di==4) return true;
    if(di==1){
        char c=mp[type][x1][y1];
        char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];
        if(c=='+'||c=='|'||c=='v'||c=='L'||c=='R'||c=='U'){
            if(d=='+'||d=='|'||d=='^'||d=='L'||d=='R'||d=='D'){
                return true;
            }
        }
        return false;
    }
    else if(di==0){
       char c=mp[type][x1][y1];
        char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];

        if(c=='+'||c=='-'||c=='>'||c=='L'||c=='D'||c=='U'){
            if(d=='+'||d=='-'||d=='<'||d=='U'||d=='R'||d=='D'){

                return true;
            }
        }
        return false;
    }
    else if(di==3){
       char c=mp[type][x1][y1];
        char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];
        if(d=='+'||d=='|'||d=='v'||d=='L'||d=='R'||d=='U'){
            if(c=='+'||c=='|'||c=='^'||c=='L'||c=='R'||c=='D'){
                return true;
            }
        }
        return false;
    }
    else if(di==2){
       char c=mp[type][x1][y1];
        char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];
       if(d=='+'||d=='-'||d=='>'||d=='L'||d=='D'||d=='U'){
            if(c=='+'||c=='-'||c=='<'||c=='U'||c=='R'||c=='D'){
                return true;
            }
        }
        return false;
    }
}
int bfs(int x,int y,int type){
	if(bg1==ed1&&bg2==ed2) return 0;
    node first,next;
    first.a=x,first.b=y,first.time=0;
	first.type=type;
    priority_queue<node>que;
    que.push(first);
    int fx,fy,fz;
    while(!que.empty()){
        first=que.top();
       
        que.pop();
        for(int i=0;i<5;i++){
            if(i!=4){
            fx=dir[i][0]+first.a;
            fy=dir[i][1]+first.b;
            fz=first.type;
    
            }
            else{
                fx=first.a;
                fy=first.b;
                fz=(first.type+1)%4;
            }
            if(fx>=0&&fx<n&&fy>=0&&fy<m&&check(first.a,first.b,i,fz)&&!hav[fz][fx][fy]){
            if(fx==ed1-1&&fy==ed2-1){
                    return first.time+1;
                }
                else next.time=first.time+1;
                next.a=fx;
                next.b=fy;
                next.type=fz;
               hav[fz][fx][fy]=1;
                que.push(next);
            }
        }
    }
    return -1;
}
int main(void){

#define _Araragi_Tsukihi
#ifndef _Araragi_Tsukihi
     freopen("in.txt", "r", stdin);
     freopen("out.txt", "w", stdout);
#endif
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",mp[0][i]);
    for(int i=0;i<n;i++)
            for(int j=0;j<m;j++){

            switch(mp[0][i][j]){
                case '+':
                       for(int k=1;k<4;k++) mp[k][i][j]='+';
                    break;
                case '-':
                    mp[1][i][j]=mp[3][i][j]='|';
                    mp[2][i][j]='-'; break;
                case '|':
                   mp[1][i][j]=mp[3][i][j]='-';
                   mp[2][i][j]='|'; break;
                case '^':
                    mp[1][i][j]='>'; mp[2][i][j]='v'; mp[3][i][j]='<';
                    break;
                case '>':
                    mp[1][i][j]='v'; mp[2][i][j]='<'; mp[3][i][j]='^';
                    break;
                case '<':
                   mp[1][i][j]='^'; mp[2][i][j]='>'; mp[3][i][j]='v';
                    break;
                case 'v':
                   mp[1][i][j]='<'; mp[2][i][j]='^'; mp[3][i][j]='>';
                    break;
                case 'L':
                    mp[1][i][j]='U'; mp[2][i][j]='R'; mp[3][i][j]='D';
                    break;
                case 'R':
                     mp[1][i][j]='D'; mp[2][i][j]='L'; mp[3][i][j]='U';
                    break;
                case 'U':
                    mp[1][i][j]='R'; mp[2][i][j]='D'; mp[3][i][j]='L';
                    break;
                case 'D':
                    mp[1][i][j]='L'; mp[2][i][j]='U'; mp[3][i][j]='R';
                    break;
                case '*':
                    for(int k=1;k<4;k++) mp[k][i][j]='*';
                    break;
            }

        }
    scanf("%d%d%d%d",&bg1,&bg2,&ed1,&ed2);
    printf("%d\n",bfs(bg1-1,bg2-1,0));
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值