Uva10047 The Monocycle

原題鏈接:https://vjudge.net/problem/UVA-10047

絞盡腦汁的圖論題,雖然只是bfs但是要求到達的最小時間以及符合到達終點的條件還是需要思考一番。

用優先隊列保證到達時間最小,每一個方塊的訪問方式由進入的方向以及輪子的顏色劃分。

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int sx,sy,ex,ey;
char G[30][30];
bool judge[30][30][4][5];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
typedef pair<int,int> P;
struct node
{
    int x,y;
    int d,c;
    int t;
    vector<P> v;
    friend bool operator<(node a,node b){
        return a.t>b.t;
    }
};
int bfs()
{
    node p;
    bool flag=0;
    p.x=sx;
    p.y=sy;
    p.d=0;
    p.c=0;
    p.t=0;
    p.v.push_back(P(sx,sy));
    memset(judge,true,sizeof(judge));
    judge[sx][sy][0][0]=false;
    priority_queue<node> que;
    que.push(p);
    //printf("x y d c t\n");
    while(!que.empty())
    {
        p=que.top();
        //printf("%d %d %d %d %d\n",p.x,p.y,p.d,p.c,p.t);
        que.pop();
        if(p.x==ex&&p.y==ey&&p.c==0)
        {
            flag=1;
            break;
        }
        for(int i=0;i<4;i++)
        {
            node np;
            if(p.d==i)
            {
                int nx=p.x+dx[i];
                int ny=p.y+dy[i];
                int c=p.c+1;
                if(c==5)
                    c=0;
                if(nx>=0&&nx<n&&ny>=0&&ny<m&&G[nx][ny]=='.'&&judge[nx][ny][i][c])
                {
                    np.x=nx;
                    np.y=ny;
                    np.c=c;
                    np.t=p.t+1;
                    np.d=i;
                    judge[nx][ny][i][c]=false;
                    for(int j=0;j<p.v.size();j++)
                        np.v.push_back(P(p.v[j].first,p.v[j].second));
                    np.v.push_back(P(nx,ny));
                    que.push(np);
                }
            }
            else
            {
                np.t=p.t;
                if(abs(i-p.d)==1||abs(i-p.d)==3)
                    np.t+=1;
                if(abs(i-p.d)==2)
                    np.t+=2;
                np.x=p.x;
                np.y=p.y;
                np.d=i;
                np.c=p.c;
                if(judge[p.x][p.y][i][p.c])
                {
                    judge[p.x][p.y][i][p.c]=false;
                    for(int j=0;j<p.v.size();j++)
                    np.v.push_back(P(p.v[j].first,p.v[j].second));
                    que.push(np);
                }
            }
        }
    }
    if(flag)
    {
//        for(int i=0;i<p.v.size();i++)
//            printf("%d %d\n",p.v[i].first,p.v[i].second);
        return p.t;
    }
    else
        return -1;

}
int main()
{
    int cas=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
            break;
        for(int i=0;i<n;i++)
        {
            getchar();
            for(int j=0;j<m;j++)
            {
                scanf("%c",&G[i][j]);
                if(G[i][j]=='S')
                {
                    sx=i;
                    sy=j;
                    G[i][j]='.';
                }
                if(G[i][j]=='T')
                {
                    ex=i;
                    ey=j;
                    G[i][j]='.';
                }
            }
        }
        int ans=bfs();
         if(cas>1)
            printf("\n");
        printf("Case #%d\n",cas++);
        if(ans!=-1)
            printf("minimum time = %d sec\n",ans);
        else
            printf("destination not reachable\n");
    }
    return 0;
}
發佈了146 篇原創文章 · 獲贊 8 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章