(BFS)codeforces #Round354-div2-D

原创 2016年05月31日 00:22:58

题目链接:题目链接


题意:一个n*m的区域,每个格子都有上下左右四个门,相邻的两个格子A可以通向B当且仅当A对B的门和B对A的门都打开,问从起点S到终点T需要的最短时间


#include<bits/stdc++.h>
using namespace std;
const int N=1003;
int n,m;
int sx,sy,tx,ty;
int dirx[]={1,-1,0,0};
int diry[]={0,0,1,-1};
char matrix[N][N];
bool visit[N][N][4];
char rotat[6][15]={{"+-|^><vLRUD*"},{"+|->v^<UDRL*"},{"+-|v<>^RLDU*"},{"+|-<^v>DULR*"}};
class node
{
    public:
    int x;
    int y;
    int angle;
    int step;
    public:
    node(int x,int y,int a,int s):x(x),y(y),angle(a),step(s){}
};
int number(char c)
{
    if(c=='+') return 0;
    if(c=='-') return 1;
    if(c=='|') return 2;
    if(c=='^') return 3;
    if(c=='>') return 4;
    if(c=='<') return 5;
    if(c=='v') return 6;
    if(c=='L') return 7;
    if(c=='R') return 8;
    if(c=='U') return 9;
    if(c=='D') return 10;
    if(c=='*') return 11;
}
bool yestop(char c)
{
    if(c=='+'||c=='|'||c=='^'||c=='L'||c=='R'||c=='D')
    return 1;
    return 0;
}
bool yesbottom(char c)
{
    if(c=='+'||c=='|'||c=='v'||c=='L'||c=='R'||c=='U')
    return 1;
    return 0;
}
bool yesleft(char c)
{
    if(c=='+'||c=='-'||c=='<'||c=='R'||c=='U'||c=='D')
    return 1;
    return 0;
}
bool yesright(char c)
{
    if(c=='+'||c=='-'||c=='>'||c=='L'||c=='U'||c=='D')
    return 1;
    return 0;
}
bool in(int x,int y)
{
    if(x<=0||x>n||y<=0||y>m)
    return 0;
    return 1;
}
queue<node> Q;
int bfs()
{
    Q.push(node(sx,sy,0,0));
    visit[sx][sy][0]=1;
    while(!Q.empty())
    {
        node p=Q.front();
        Q.pop();
        if(p.x==tx&&p.y==ty)
        return p.step;
        for(int i=0;i<4;i++)
        {
            int nx=p.x+dirx[i];
            int ny=p.y+diry[i];
            switch(i)
            {
                case 0:
                    if(in(nx,ny)&&!visit[nx][ny][p.angle]&&yesbottom(rotat[p.angle][number(matrix[p.x][p.y])])
                       &&yestop(rotat[p.angle][number(matrix[nx][ny])]))
                       {
                           Q.push(node(nx,ny,p.angle,p.step+1));
                           visit[nx][ny][p.angle]=1;
                       }
                       break;
                case 1:
                    if(in(nx,ny)&&!visit[nx][ny][p.angle]&&yestop(rotat[p.angle][number(matrix[p.x][p.y])])
                       &&yesbottom(rotat[p.angle][number(matrix[nx][ny])]))
                       {
                           Q.push(node(nx,ny,p.angle,p.step+1));
                           visit[nx][ny][p.angle]=1;
                       }
                       break;
                case 2:
                    if(in(nx,ny)&&!visit[nx][ny][p.angle]&&yesright(rotat[p.angle][number(matrix[p.x][p.y])])
                       &&yesleft(rotat[p.angle][number(matrix[nx][ny])]))
                       {
                           Q.push(node(nx,ny,p.angle,p.step+1));
                           visit[nx][ny][p.angle]=1;
                       }
                       break;
                case 3:
                    if(in(nx,ny)&&!visit[nx][ny][p.angle]&&yesleft(rotat[p.angle][number(matrix[p.x][p.y])])
                       &&yesright(rotat[p.angle][number(matrix[nx][ny])]))
                       {
                           Q.push(node(nx,ny,p.angle,p.step+1));
                           visit[nx][ny][p.angle]=1;
                       }
                       break;
            }
        }
        p.angle=(p.angle+1)%4;
        if(!visit[p.x][p.y][p.angle])
        {
            p.step++;
            Q.push(node(p));
            visit[p.x][p.y][p.angle]=1;
        }
    }
    return -1;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    scanf("%s",matrix[i]+1);
    scanf("%d%d",&sx,&sy);
    scanf("%d%d",&tx,&ty);
    printf("%d\n",bfs());
}


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

题目链接:http://codeforces.com/contest/676/problem/D D. Theseus and labyrinth time limit per t...

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

Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a recta...

Codeforces Round #354 (Div. 2) D (BFS)

D. Theseus and labyrinth time limit per test 3 seconds memory limit per test 256 megabyt...

【Codeforces Round 354 (Div 2)E】【数学 多项式除法 讨论】The Last Fight Between Human and AI 多项式除以x-k是否值整除

E. The Last Fight Between Human and AI time limit per test 1 second memory limit per test 256 me...

Codeforces Round #354 (Div. 2)B.Pyramid of Glasses(思维题)

题目链接:http://codeforces.com/contest/676/problem/B B. Pyramid of Glasses time limit per ...

Codeforces Round #354 (Div. 2) B. Pyramid of Glasses (模拟)

B. Pyramid of Glassestime limit per test 1 second memory limit per test 256 megabytes input sta...

Codeforces Round #354 (Div. 2) A Nicholas and Permutation(水题)

http://codeforces.com/problemset/problem/676/A A. Nicholas and Permutation time limit per ...

【Codeforces Round 354 (Div 2)B】【简单模拟】Pyramid of Glasses 漏斗漏水n层t秒灌满多少个

B. Pyramid of Glasses time limit per test 1 second memory limit per test 256 megabytes ...

Codeforces Round #354 (Div. 2) E (数学题)

E. The Last Fight Between Human and AI time limit per test 1 second memory limit per test...

Codeforces Round #354 (Div. 2) C (二分???)

C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:(BFS)codeforces #Round354-div2-D
举报原因:
原因补充:

(最多只允许输入30个字)