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

zoj 1671 Walking Ant

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

zoj 1671 Walking Ant

#include<cstdio>#include<queue>#include<cstring>int dx[4]={0,0,1,-1};int dy[4]={1,-1,0,0};int nx,ny,m,n,ex,ey,vis[10][10],a[10][10];using namespace std;struct node{int x,y,v,time;friend bool operator < (node a,node b){return a.v>b.v;}}p,temp;void bfs(int c,int y){p.x=c;p.y=y;p.time=0;p.v=6;queue<node>q;q.push(p);while(!q.empty()){p=q.front();q.pop();for(int i=0;i<4;i++){temp.y=p.y+dy[i];temp.x=p.x+dx[i];temp.time=p.time+1;temp.v=p.v-1;if(temp.x<m&&temp.x>=0&&temp.y<n&&temp.y>=0&&!vis[temp.x][temp.y]&&temp.v>=1){if(temp.x==ex&&temp.y==ey){printf("%dn",temp.time);return;}if(a[temp.x][temp.y]==4){temp.v=6;vis[temp.x][temp.y]=1;}if(temp.v==1)continue;q.push(temp);}}}printf("-1n");}int main(){int sx,sy,i,j;while(scanf("%d%d",&n,&m),m||n){memset(vis,0,sizeof(vis));for(i=0;i<m;i++)for(j=0;j<n;j++){scanf("%d",&a[i][j]);if(a[i][j]==2){sx=i;sy=j;}else if(a[i][j]==3){ex=i;ey=j;}else if(a[i][j]==0)vis[i][j]=1;}bfs(sx,sy);}return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369305.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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