蛇的爬行,蛇出洞

来源:互联网 发布:java投票管理系统 编辑:程序博客网 时间:2024/04/30 04:23

这道bfs,嗯,长见识了。关键是如何判重。。做这道题时刚学bfs,弱的不知道从何下手。。看了两天还怀疑是不是对的。。。

[cpp] view plaincopyprint?
  1. #include<cstdio>  
  2. #include<queue>  
  3. #include<cstring>  
  4. #include<cstdlib>  
  5. using namespace std;  
  6. const int maxn =21;  
  7. int n,m,l,find,ans,move;  
  8. bool map[maxn][maxn];  
  9. int dx[]={1,-1,0,0};  
  10. int dy[]={0,0,1,-1};  
  11. bool vis[maxn][maxn][1<<14];  
  12. bool check(int x,int y){   return x>=1&&x<=n&&y>=1&&y<=m;  }  
  13. struct node{  
  14.    int head[2],tail[2];  
  15.    bool map[maxn][maxn];  
  16.    int step,val;  
  17. }s_pos;  
  18. void bfs(){  
  19.      memset(vis,false,sizeof(vis));  
  20.      queue<node > q;  q.push(s_pos);  
  21.      vis[s_pos.head[0]][s_pos.head[1]][ s_pos.val] =true;  
  22.      while(!q.empty()){  
  23.          node now = q.front();  q.pop();  
  24.          if(now.head[0]==1&&now.head[1]==1){  
  25.              find=1;  ans=now.step;  
  26.              return ;  
  27.          }  
  28.          for(int i=0;i<4;i++){  
  29.              node next = now;  next.step+=1;  
  30.              int x=now.head[0]+dx[i];  int y=now.head[1]+dy[i];  
  31.   
  32.              if(check(x,y)){  
  33.                  if(next.map[x][y])  continue;  
  34.                  next.head[0]=x;  next.head[1]=y;  
  35.                  next.map[x][y]=true;  
  36.                  next.map[now.tail[0]][now.tail[1]]=false;  
  37.                  int t= next.val&3;  
  38.                  next.tail[0]=now.tail[0]+dx[t];  
  39.                  next.tail[1]=now.tail[1]+dy[t];  
  40.   
  41.                  next.val=next.val>>2;  
  42.                  int sum=i<<move;  
  43.                  next.val+=sum;  
  44.                  if(!vis[x][y][next.val]){     //这道题是以整个蛇身为基准,“走过”是指完全一样的蛇身在这里出现过
  45.                      vis[x][y][next.val]=true;    //map上的障碍物只有 此刻的 蛇身和石头
  46.                      q.push(next);                //也就是说,map上的点为true也不一定没走过
  47.                  }  
  48.              }  
  49.          }  
  50.   
  51.      }  
  52.   
  53. }  
  54. int main(){  
  55.     int t,x,y,ca=1;  
  56.     int nx[10],ny[10];  
  57.     while(scanf("%d%d%d",&n,&m,&l)!=EOF,(n+m+l)){  
  58.         memset(s_pos.map,false,sizeof(s_pos.map));  
  59.         memset(map,false,sizeof(map));  
  60.   
  61.         s_pos.val=0;  move=((l<<1)-4);  
  62.         for(int i=0;i<l;i++) {  
  63.             scanf("%d%d",&x,&y);  
  64.             nx[i]=x;ny[i]=y;  
  65.             s_pos.map[x][y]=true;  
  66.             if(i>0){  
  67.                for(int j=0;j<4;j++){  
  68.                    if(x+dx[j]==nx[i-1]&&y+dy[j]==ny[i-1]){  
  69.                           s_pos.val=(s_pos.val<<2)+j;  
  70.                           break;  
  71.                     }  
  72.                }  
  73.             }  
  74.         }  
  75.         s_pos.head[0]=nx[0],  s_pos.head[1]=ny[0];  
  76.         s_pos.tail[0]=nx[l-1];s_pos.tail[1]=ny[l-1];  
  77.         scanf("%d",&t);   s_pos.step=0;  
  78.   
  79.         for(int i=0;i<t;i++){  
  80.             scanf("%d%d",&x,&y);  
  81.             map[x][y]=true;  
  82.             s_pos.map[x][y]=true;  
  83.         }  
  84.         find=0;   bfs();  
  85.              printf("Case %d: ",ca++);  
  86.         if(find){  
  87.              printf("%d\n",ans);  
  88.         }  
  89.         else printf("-1\n");  
  90.     }  
  91.     return 0; 

原创粉丝点击