zoj 1249 | poj 1475 Pushing Boxes

來源:互聯網
上載者:User

 題目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249

又是推箱子。。題目求的是推箱子所要的最少次數。  那麼以箱子為開始點 進行BFS。每次判斷人(BFS)能不能到達箱子所需推到的反方向。如果能救如隊列。有幾個細節需要注意。1,箱子移動時,箱子可以移動到人當前所在的位置。2,人移動時,人不能移動到箱子未改變狀態時所在的位置。。然後類比即可。

下面是AC代碼:

#include<iostream>#include<vector>#include<cstdio>#include<string>#include<queue>#include<cstring>using namespace std;int n,m;char map[25][25];int       dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};int other_dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};int ex,ey,flag;bool vis[25][25][25][25];bool mark[25][25];char op[]="nswe";char big_op[]="NSWE";string ans;struct node{    int b_x,b_y;    int p_x,p_y;    int step;    string ans;}s_pos;bool cheack(int x,int y){     return x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#';     return false;}bool people_cango(node &cur,node last,int e_x,int e_y){     queue<node > q;  node per;per=cur;   per.step=0;  per.ans="";     memset(mark,false,sizeof(mark));     q.push(per);     mark[cur.p_x][cur.p_y]=true;     while(!q.empty()){         node now=q.front();  q.pop();          if(now.p_x==e_x&&now.p_y==e_y){             cur.ans+=now.ans;             return true;           }         for(int i=0;i<4;i++){             node next=now;     next.step+=1;             next.p_x+=dir[i][0];   next.p_y+=dir[i][1];             if(cheack(next.p_x,next.p_y)&&!mark[next.p_x][next.p_y]){                 if(next.p_x==last.b_x&&next.p_y==last.b_y)  continue;  //遇見箱子                 mark[next.p_x][next.p_y]=true;                 next.ans+=op[i];                 if(next.p_x==e_x&&next.p_y==e_y){                    cur.ans+=next.ans;                    return true;                 }                 q.push(next);             }         }     }    return false;}void bfs(){     queue<node > q;     memset(vis,false,sizeof(vis));     q.push(s_pos);     vis[s_pos.b_x][s_pos.b_y][s_pos.p_x][s_pos.p_y]=true;     while(!q.empty()){         node now = q.front(); q.pop();         for(int i=0;i<4;i++){             node next = now;   next.step+=1;             next.b_x+=dir[i][0];  next.b_y+=dir[i][1];             int x=now.b_x+other_dir[i][0];                  //人要到達箱子的反面             int y=now.b_y+other_dir[i][1];             if(cheack(next.b_x,next.b_y)&&cheack(x,y)&&!vis[next.b_x][next.b_y]                [now.b_x][now.b_y]){            //     if(next.b_x==now.p_x&&next.b_y==now.p_y)  continue;              //    cout<<next.p_x<<" "<<next.p_y<<endl;                 if(people_cango(next,now,x,y)){              //      cout<<x<<" "<<y<<endl;                    next.p_x=now.b_x;                    next.p_y=now.b_y;                    next.ans+=big_op[i];                     if(next.b_x==ex&&next.b_y==ey){                        flag=1;                        ans=next.ans;                        return ;                    }                    vis[next.b_x][next.b_y][next.p_x][next.p_y]=true;                    q.push(next);                 }             }         }     }}int main(){    int ca=1;    while(scanf("%d%d",&n,&m)!=EOF,n+m){          for(int i=0;i<n;i++)  scanf("%s",map[i]);          for(int i=0;i<n;i++){              for(int j=0;j<m;j++){              if(map[i][j]=='T'){                  ex=i,ey=j;              }              if(map[i][j]=='B'){                  s_pos.b_x=i;  s_pos.b_y=j;              }              if(map[i][j]=='S'){                  s_pos.p_x=i;  s_pos.p_y=j;              }              }          }                    flag=0; s_pos.step=0;  s_pos.ans="";          bfs();          cout<<"Maze #"<<ca++<<endl;          if(flag){             cout<<ans<<endl;          }          else          cout<<"Impossible."<<endl;          cout<<endl;              }    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.