Light OJ 1066(bfs+坑)
来源:互联网 发布:有域名还需要什么 编辑:程序博客网 时间:2024/05/18 01:00
咋一看就是一个裸的bfs,题目要求按字母顺序bfs;
WA了两次,主要有几个注意的地方。
1.准备去搜某个字母的时候,比他大的字母不能碰;
2.每次搜某个字母的时候,要重新把vis数组清0;
3.搜完字母后就应该把这个字母所在的位置改为empty grid,因为摘完了食物的地方可以走;
代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<queue>using namespace std;char G[11][11];bool vis[11][11];int n;int dx[4]={0,0,1,-1};int dy[4]={1,-1,0,0};struct node{ int x,y,dis; node(){} node(int _x,int _y,int _dis):x(_x),y(_y),dis(_dis) {}}ride[26];int bfs(node st,char goal){ queue<node> q; memset(vis,0,sizeof(vis)); vis[st.x][st.y]=1; q.push(st); while(!q.empty()) { node now=q.front(); q.pop(); int nowx=now.x,nowy=now.y; if(G[nowx][nowy]==goal) return now.dis; for(int i=0;i<4;i++) { int tmpx=nowx+dx[i],tmpy=nowy+dy[i]; if(tmpx>=1&&tmpx<=n&&tmpy>=1&&tmpy<=n&&!vis[tmpx][tmpy]&&G[tmpx][tmpy]!='#') { if(G[tmpx][tmpy]!='.'&&G[tmpx][tmpy]!=goal) continue; vis[tmpx][tmpy]=1; q.push(node(tmpx,tmpy,now.dis+1)); } } } return -1;}int main(){ // freopen("in.txt","r",stdin); int T,i,j,ca=0; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=1;i<=n;i++) scanf("%s",G[i]+1); int cnt=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(G[i][j]>='A'&&G[i][j]<='Z') { int tot=G[i][j]-'A'; cnt++; ride[tot].x=i,ride[tot].y=j,ride[tot].dis=0; } int ans=0; bool ok=1; for(int i=1;i<cnt;i++) { G[ride[i-1].x][ride[i-1].y]='.'; int tmp=bfs(ride[i-1],i+'A'); if(tmp==-1) {ok=0;break;} else ans+=tmp; } if(ok) printf("Case %d: %d\n",++ca,ans); else printf("Case %d: Impossible\n",++ca); } return 0;}
0 0
- Light OJ 1066(bfs+坑)
- light oj 1066Gathering Food (bfs 稍微有点小坑)
- !Light oj 1066--BFS--(队列溢出)
- Light OJ 1055 BFS
- Light oj 1046 - Rider (BFS)
- Light OJ 1141--BFS--(隐蔽的BFS)
- Light OJ 1238--BFS(第一道自己做的)
- Light OJ 1377 - Blade and Sword (BFS)
- Light OJ:1141 Number Transformation(BFS+素因子)
- light oj
- light oj
- Light OJ
- Light OJ
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
- Light OJ 1094 - Farthest Nodes in a Tree【树的直径 两次bfs】
- Light OJ 1000
- Light OJ 1001
- Light OJ 1008
- EL表达式+JSTL
- hdu 4865 Peter's Hobby(DP)
- java学习-定义数组
- clog是输出流
- 新的生活,新的起点
- Light OJ 1066(bfs+坑)
- char与string
- Android学习笔记五 为应用程序打包
- 属性与引用计数
- OpenGL红宝石实例程序2-14
- xcode 自动更新资源
- HDU 4864 Task(2014多校--贪心)
- 选择运算符成员
- leetcode之Permutation Sequence解题思路