【BFS】uva10047The Monocycle

來源:互聯網
上載者:User

標籤:des   os   io   for   ar   時間   amp   size   

/*本題的特殊之處,到達一個格子時,因為朝向不同,以及接觸地面的顏色不同,會處於不同的狀態;;;;;;;;;把(x, y, d, c)作為一個結點,表示所在位置(x, y),方向為d,顏色為c;;;;;------------------------------------------------------------------------在方向上我們把前,左,右編號為0,1,2;;;;顏色,從藍色開始編號為0,1,2,3;;;;;;;;;;--------------------------------------------------------------------------vis[x1][y1][0][0] = 1;起點S進入隊列node a, b;a.x = x1;a.y = y1;a.d = 0;a.c = 0;a.t = 0;q.push(a);-----------------------------------------------------------------------b.t = a.t + 1;前進方向;;;時間加1b.c = (a.c + 1)%5;擴充後的顏色b.x = a.x + d[a.d][0];擴充後的位置b.y = a.y + d[a.d][1];b.d = a.d;方向不變if(judge(b)){    if(b.x == x2 && b.y == y2 && b.c == 0)是否到達終點        return b.t;    vis[b.x][b.y][b.d][b.c] = 1;    q.push(b);}----------------------------------------------------------------------------------b = a;向右轉b.t ++;b.d = (b.d + 1)%4;擴充後的方向if(judge(b)){    vis[b.x][b.y][b.d][b.c] = 1;    q.push(b);}b = a;向左轉b.t ++;b.d = (b.d - 1 + 4)%4;if(judge(b)){    vis[b.x][b.y][b.d][b.c] = 1;    q.push(b);}-------------------------------------------------------------------for(int i=0; i<m; i++)尋找始末點S和T點{    for(int j=0; j<n; j++)    {        if(g[i][j] == 'S')        {            x1 = i;            y1 = j;            break;        }    }}for(int i=0; i<m; i++){    for(int j=0; j<n; j++)    {        if(g[i][j] == 'T')        {            x2 = i;            y2 = j;            break;        }    }}------------------------------------------------------------------------------*/#include <iostream>#include <cstdio>#include <queue>#include <cstring>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;const int MAXN = 30;struct node{    int x, y, d, c, t;};int m, n;char g[MAXN][MAXN];int vis[MAXN][MAXN][6][6];int x1, x2, y1, y2;int d[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};int judge(node b){    if(b.x < 0 || b.x >= m || b.y < 0 || b.y >= n)        return false;    if(g[b.x][b.y] == '#')        return false;    if(vis[b.x][b.y][b.d][b.c])        return false;    return true;}int bfs(){    queue<node> q;    vis[x1][y1][0][0] = 1;    node a, b;    a.x = x1;    a.y = y1;    a.d = 0;    a.c = 0;    a.t = 0;    q.push(a);    while(!q.empty())    {        a = q.front();        q.pop();        b.t = a.t + 1;        b.c = (a.c + 1)%5;        b.x = a.x + d[a.d][0];        b.y = a.y + d[a.d][1];        b.d = a.d;        if(judge(b))        {            if(b.x == x2 && b.y == y2 && b.c == 0)                return b.t;            vis[b.x][b.y][b.d][b.c] = 1;            q.push(b);        }        b = a;        b.t ++;        b.d = (b.d + 1)%4;        if(judge(b))        {            vis[b.x][b.y][b.d][b.c] = 1;            q.push(b);        }        b = a;        b.t ++;        b.d = (b.d - 1 + 4)%4;        if(judge(b))        {            vis[b.x][b.y][b.d][b.c] = 1;            q.push(b);        }    }    return -1;}int main(){    //freopen("input.txt","r",stdin);    int kase = 1;    while(scanf("%d%d",&m,&n) != EOF && (n+m))    {        if(kase != 1)            printf("\n");        printf("Case #%d\n",kase++);        getchar();        for(int i=0; i<m; i++)            gets(g[i]);        for(int i=0; i<m; i++)        {            for(int j=0; j<n; j++)            {                if(g[i][j] == 'S')                {                    x1 = i;                    y1 = j;                    break;                }            }        }        for(int i=0; i<m; i++)        {            for(int j=0; j<n; j++)            {                if(g[i][j] == 'T')                {                    x2 = i;                    y2 = j;                    break;                }            }        }        memset(vis, 0, sizeof(vis));        int time = bfs();        if(time == -1)            printf("destination not reachable\n");        else            printf("minimum time = %d sec\n",time);    }    return 0;}

----------------------------

wuwuwuwuwuwuwuwu...................................

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.