22 poj2935 Basic Wall Maze,poj2935maze

來源:互聯網
上載者:User

22 poj2935 Basic Wall Maze,poj2935maze

對於每個點有四個方向可以走,把牆兩邊的點對應牆的方向能不能走給預先處理一下,用vis[x][y][0..3]表示點(x,y)的該方向可不可以走。

然後bfs就好了,記錄路徑就把每個結點的前一個點記錄下來,遞迴輸出就可以了。



#include<cstdio>#include<queue>#include<cstring>using namespace std;int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};bool vis[7][7][4],vvis[7][7];char di[4]={'S','N','E','W'};int sx,sy,ex,ey;struct node{    int x,y,dir;}pre[10][10];void print(int xx,int yy){    if(xx==sx&&yy==sy) return ;    print(pre[xx][yy].x,pre[xx][yy].y);    printf("%c",di[pre[xx][yy].dir]);//輸出該點方向}void bfs(){    queue<node> q;    memset(vvis,0,sizeof vvis);//標記這個點走沒走過    vvis[sx][sy]=1;    q.push((node){sx,sy,-1});//從起點開始走    while(!q.empty())    {        node now=q.front();q.pop();        if(now.x==ex&&now.y==ey)//到終點就輸出 題目保證有解        {            print(ex,ey);            puts("");            return ;        }        for(int i=0;i<4;i++)        {            if(!vis[now.x][now.y][i])//該方向可以走            {                int xx=now.x+dx[i];                int yy=now.y+dy[i];                if(!vvis[xx][yy]&&xx>0&&xx<=6&&yy>0&&yy<=6)//該點沒走過,且沒有越界                {                    vvis[xx][yy]=1;                    pre[xx][yy]=(node){now.x,now.y,i};                    q.push((node){xx,yy,i});                }            }        }    }}int main(){    int i,j,a,b,c,d;    while(scanf("%d%d",&sx,&sy)&&(sx||sy))    {        scanf("%d%d",&ex,&ey);        memset(vis,0,sizeof vis);        for(i=0;i<3;i++)        {            scanf("%d%d%d%d",&a,&b,&c,&d);            if(a==c)//橫著的牆            {                for(j=b+1;j<=d;j++)                {                    if(a!=0) vis[a][j][2]=1;                    if(a!=6) vis[a+1][j][3]=1;                }            }            else//豎著的牆            {                for(j=a+1;j<=c;j++)                {                    if(b!=0) vis[j][b][0]=1;                    if(b!=6) vis[j][b+1][1]=1;                }            }        }        bfs();    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.