POJ3322Bloxorz I

POJ3322 Bloxorz I 

暴搜,next数组与处理一下(小技巧)

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <queue>
  6 #include <algorithm>
  7 using namespace std;
  8 
  9 #define res register int
 10 const int N=510;
 11 char s[N][N];
 12 int n,m,d[N][N][3];
 13 struct node{int x,y,lie;};//lie=0 立 lie=1 横向躺 lie=2 纵向躺 
 14 node st,ed;
 15 queue<node> q;
 16 const int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
 17 
 18 inline bool valid(int x,int y){
 19     return x>0&&x<=n&&y>0&&y<=m;
 20 }
 21 
 22 inline void read()
 23 {
 24     for(res i=1 ; i<=n ; i++)
 25         for(res j=1 ; j<=m ; j++) cin>>s[i][j];
 26     for(res i=1 ; i<=n ; i++)
 27         for(res j=1 ; j<=m ; j++)
 28         {
 29             if(s[i][j]=='O') ed.x=i,ed.y=j,ed.lie=0,s[i][j]='.';
 30             else if(s[i][j]=='X')
 31             {
 32                 for(res k=0 ; k<4 ; k++)
 33                 {
 34                     int x=i+dx[k],y=j+dy[k];
 35                     if(valid(x,y) && s[x][y] == 'X') {
 36                         st.x=min(i,x),st.y=min(j,y),st.lie=k<2?1:2;
 37                         s[i][j]=s[x][y]='.';
 38                         break;
 39                     } 
 40                     if(s[i][j]=='X') st.x=i,st.y=j,st.lie=0,s[i][j]='.';
 41                 } 
 42             }
 43         }
 44 }
 45 
 46 const int next_x[3][4]={{0,0,-2,1},{0,0,-1,1},{0,0,-1,2}};
 47 const int next_y[3][4]={{-2,1,0,0},{-1,2,0,0},{-1,1,0,0}};
 48 const int next_lie[3][4]={{1,1,2,2},{0,0,1,1},{2,2,0,0}};
 49 
 50 inline bool valid(node t)
 51 {
 52     if(!valid(t.x,t.y)) return false;
 53     int nx=t.x,ny=t.y;
 54     if(s[nx][ny]=='#') return false;
 55     if(t.lie==0 && s[nx][ny]!='.') return false;
 56     if(t.lie==1 && s[nx][ny+1]=='#') return false;
 57     if(t.lie==2 && s[nx+1][ny]=='#') return false;
 58     return true;
 59 }
 60 
 61 inline int bfs()
 62 {
 63     for(res i=1 ; i<=n ; i++)
 64         for(res j=1 ; j<=m ; j++) 
 65             for(res k=0 ; k<3 ; k++)    d[i][j][k]=-1;
 66     while(q.size()) q.pop();
 67     d[st.x][st.y][st.lie]=0; q.push(st);
 68     while(q.size())
 69     {
 70         node now=q.front(); q.pop();
 71         for(res i=0 ; i<4 ; i++)
 72         {
 73             node t;
 74             t.x=now.x+next_x[now.lie][i];
 75             t.y=now.y+next_y[now.lie][i];
 76             t.lie=next_lie[now.lie][i];
 77             if(!valid(t)) continue;
 78             if(d[t.x][t.y][t.lie]==-1)
 79             {
 80                 d[t.x][t.y][t.lie]=d[now.x][now.y][now.lie]+1;
 81                 q.push(t);
 82                 if(t.x==ed.x && t.y==ed.y && t.lie==ed.lie) 
 83                     return d[t.x][t.y][t.lie];
 84             }
 85         }
 86     }
 87     return -1;    
 88 }
 89 
 90 int main()
 91 {
 92     while(scanf("%d %d",&n,&m)==2 && n && m)
 93     {
 94         read();
 95         int ans=bfs();
 96         if(ans==-1) puts("Impossible");
 97         else printf("%d\n",ans);
 98     }
 99     
100     return 0;
101 }
View Code

 

posted @ 2019-02-15 08:39  孑行  阅读(171)  评论(0编辑  收藏  举报