栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1361 Holedox Moving

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1361 Holedox Moving

#include<cstdio>#include<queue>#include<cstring>#include<cstdlib>using namespace std;const int maxn =21;int n,m,l,find,ans,move;bool map[maxn][maxn];int dx[]={1,-1,0,0};int dy[]={0,0,1,-1};bool vis[maxn][maxn][1<<14];bool check(int x,int y){   return x>=1&&x<=n&&y>=1&&y<=m;  }struct node{   int head[2],tail[2];   bool map[maxn][maxn];   int step,val;}s_pos;void bfs(){     memset(vis,false,sizeof(vis));     queue<node > q;  q.push(s_pos);     vis[s_pos.head[0]][s_pos.head[1]][ s_pos.val] =true;     while(!q.empty()){         node now = q.front();  q.pop();         if(now.head[0]==1&&now.head[1]==1){  find=1;  ans=now.step;  return ;         }         for(int i=0;i<4;i++){  node next = now;  next.step+=1;  int x=now.head[0]+dx[i];  int y=now.head[1]+dy[i];  if(check(x,y)){      if(next.map[x][y])  continue;      next.head[0]=x;  next.head[1]=y;      next.map[x][y]=true;      next.map[now.tail[0]][now.tail[1]]=false;      int t= next.val&3;      next.tail[0]=now.tail[0]+dx[t];      next.tail[1]=now.tail[1]+dy[t];      next.val=next.val>>2;      int sum=i<<move;      next.val+=sum;      if(!vis[x][y][next.val]){          vis[x][y][next.val]=true;          q.push(next);      }  }         }     }}int main(){    int t,x,y,ca=1;    int nx[10],ny[10];    while(scanf("%d%d%d",&n,&m,&l)!=EOF,(n+m+l)){        memset(s_pos.map,false,sizeof(s_pos.map));        memset(map,false,sizeof(map));        s_pos.val=0;  move=((l<<1)-4);        for(int i=0;i<l;i++) { scanf("%d%d",&x,&y); nx[i]=x;ny[i]=y; s_pos.map[x][y]=true; if(i>0){    for(int j=0;j<4;j++){        if(x+dx[j]==nx[i-1]&&y+dy[j]==ny[i-1]){    s_pos.val=(s_pos.val<<2)+j;    break;         }    } }        }        s_pos.head[0]=nx[0],  s_pos.head[1]=ny[0];        s_pos.tail[0]=nx[l-1];s_pos.tail[1]=ny[l-1];        scanf("%d",&t);   s_pos.step=0;        for(int i=0;i<t;i++){ scanf("%d%d",&x,&y); map[x][y]=true; s_pos.map[x][y]=true;        }        find=0;   bfs();  printf("Case %d: ",ca++);        if(find){  printf("%dn",ans);        }        else printf("-1n");    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378671.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号