Codeforces 676D Theseus and labyrinth 模擬+bfs

題意:有一個n*m的地圖,每個點代表一個房間,每個房間可能有四個門,例如>代表右邊只有一個門在右邊即只能向右走,L代表左邊沒有門只能除了左其他都可以走等等。現在給出起點和終點,每次你可以把全部房間旋轉90度或者移動到相鄰的房間,但前提是兩個房間之間都有有門,現在要你求起點出發到終點的最少時間。

分析:vis肯定不能有step步數的標記。。 那麼轉多少次好像沒有限制,,可是還是要只能開4的大小。然後轉的過程可以在走的時候每一次都判斷轉不轉。。這樣就只需要開4.。

大佬的代碼。

#include<iostream>
#include<stdio.h>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=1001;
const int INF=0x3f3f3f3f;
int n,m;
char s[maxn][maxn];
bool door[maxn][maxn][4];
int sx,sy;
int ex,ey;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int dis[maxn][maxn][4];
void deal(int x,int y)
{
    switch (s[x][y])
    {
        case '+':
        {
            door[x][y][0]=true;
            door[x][y][1]=true;
            door[x][y][2]=true;
            door[x][y][3]=true;
            break;
        }
        case '-':
        {
            door[x][y][1]=true;
            door[x][y][3]=true;
            break;
        }
        case '|':
        {
            door[x][y][0]=true;
            door[x][y][2]=true;
            break;
        }
        case '^':
        {
            door[x][y][0]=true;
            break;
        }
        case '>':
        {
            door[x][y][1]=true;
            break;
        }
        case '<':
        {
            door[x][y][3]=true;
            break;
        }
        case 'v':
        {
            door[x][y][2]=true;
            break;
        }
        case 'L':
        {
            door[x][y][0]=true;
            door[x][y][1]=true;
            door[x][y][2]=true;
            break;
        }
        case 'R':
        {
            door[x][y][0]=true;
            door[x][y][2]=true;
            door[x][y][3]=true;
            break;
        }
        case 'U':
        {
            door[x][y][1]=true;
            door[x][y][2]=true;
            door[x][y][3]=true;
            break;
        }
        case 'D':
        {
            door[x][y][0]=true;
            door[x][y][1]=true;
            door[x][y][3]=true;
            break;
        }
    }
}
struct Node
{
    int x,y,d;
    int dist;
    bool operator <(const Node& node) const
    {
        return dist>node.dist;
    }
    Node(int _x=0,int _y=0,int _d=0,int _dist=0):x(_x),y(_y),d(_d),dist(_dist){}
};
void dj()
{
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<m;++j)
        {
            for(int k=0;k<4;++k)
            {

                dis[i][j][k]=INF;
                //cout<<i<<" "<<j<<" "<<dist[i][j]
            }
        }
    }
    dis[sx][sy][0]=0;
    priority_queue<Node>que;
    while(!que.empty()) que.pop();
    que.push(Node(sx,sy,0,0));
    Node tmp;
    int x,y,d;
    int nx,ny,nd;
    int dist;
    while(!que.empty())
    {
        tmp=que.top(); que.pop();
        //cout<<"yes"<<endl;
        x=tmp.x;
        y=tmp.y;
        d=tmp.d;
        //cout<<x<<" "<<y<<" "<<d<<endl;
        dist=tmp.dist;
        if(dis[x][y][d]<dist) continue;
        int lx=(d+1)%4;
        if(dis[x][y][lx]>dist+1)
        {
            dis[x][y][lx]=dist+1;
            que.push(Node(x,y,lx,dis[x][y][lx]));
        }
        for(int l=0;l<4;++l)
                {
                    nx=x+dx[l];
                    ny=y+dy[l];
                    if(nx<0||nx>=n||ny<0||ny>=m) continue;
                    int dr1=(l-d+4)%4;
                    int dr2=(l-d+6)%4;
                    if(door[x][y][dr1]&&door[nx][ny][dr2]&&dis[nx][ny][d]>dist+1)
                    {
                        dis[nx][ny][d]=dist+1;
                        que.push(Node(nx,ny,d,dist+1));
                    }
                }
    }
}
int main()
{
    memset(door,false,sizeof(door));
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;++i) scanf("%s",s[i]);
    scanf("%d%d",&sx,&sy);
    --sx; --sy;
    scanf("%d%d",&ex,&ey);
    --ex; --ey;
    for(int i=0;i<n;++i)
    for(int j=0;j<m;++j) deal(i,j);
    int nx,ny;
    dj();
    //cout<<"yes"<<endl;
    int ans=INF;
    for(int i=0;i<4;++i) ans=min(ans,dis[ex][ey][i]);
    if(ans==INF) printf("-1\n");
    else printf("%d\n",ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章