码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3865 Superbot

时间:2015-04-16 21:38:10      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

题目的状态是比较少的,可以BFS一一个单位时间为一步,暴力化的搜索所有状况的。不熟悉这种类型,又被题目的条件吓到了,各种处理,结果却做不出来。

对于路径搜索或是其他采用bfs其每一步的花费可能不同时,可以采用优先队列,将一步分为多步走方法较好,能保持简单情况时的模型。

#include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; const int msize=12; struct node { int h,l; int d,t; }st; node dir[5]={{-1,0},{1,0},{0,-1},{0,1}}; int vis[12][12][5][316]; int n,m,zp; char map[msize][msize]; int bfs(node st) { node to; queue<node> qu; qu.push(st); while(!qu.empty()) { st=qu.front();qu.pop(); int h=st.h,l=st.l,d=st.d,t=st.t; to.t=t+1;//到达时为下一秒 to.h=h;to.l=l; if(to.t>=310)continue; if(st.t!=0&&zp!=0&&st.t%zp==0)//处理这一秒 d=d==0?3:d-1; //不动 to.d=d; if(!vis[to.h][to.l][to.d][to.t]) { vis[to.h][to.l][to.d][to.t]=1; qu.push(to); } //左移 to.d=d==0?3:d-1; if(!vis[to.h][to.l][to.d][to.t]) { vis[to.h][to.l][to.d][to.t]=1; qu.push(to); } //右移 to.d=d==3?0:d+1; if(!vis[to.h][to.l][to.d][to.t]) { vis[to.h][to.l][to.d][to.t]=1; qu.push(to); } //按动 to.h=h+dir[d].h,to.l=l+dir[d].l,to.d=d; if(0<=to.h&&to.h<n&&0<=to.l&&to.l<m &&map[to.h][to.l]!=*&&!vis[to.h][to.l][to.d][to.t]) { if(map[to.h][to.l]==$) return to.t; vis[to.h][to.l][to.d][to.t]=1; qu.push(to); } } return -1; } int main(void) { int t; scanf("%d",&t); while(t--) { memset(vis,0,sizeof(vis)); scanf("%d %d %d",&n,&m,&zp); for(int i=0; i<n; i++) { scanf("%s",map[i]); for(int j=0; j<m; j++) { if(map[i][j]==@) { st.h=i; st.l=j; st.d=2; st.t=0; } } } vis[st.h][st.l][2][0]=1; int ans= bfs(st); if(ans==-1) printf("YouBadbad\n"); else printf("%d\n",ans); } return 0; } /* 34 10 10 20 .******..@ ........** ......**** ....****** .....*.... .....*.... ....*..... ....*.$... .*...*.... .*........ */

 

zoj 3865 Superbot

标签:

原文地址:http://www.cnblogs.com/lgc2583657917/p/4433063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!