LightOJ-1066-bfs
来源:互联网 发布:linux 输出文件夹大小 编辑:程序博客网 时间:2024/06/06 00:59
题目大意:一张坐标图上有若干个食物,每个都用一个英文字母代表,现在需要按字典序一次寻找每个食物问最少需要多少路程;
题目解析:bfs即可,依次寻找,每次寻找成功之后就把上一个置位‘.’,表示可以通过;
AC代码:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<queue>using namespace std;int graph[11][11];bool vis[11][11];int dx[4]={-1,0,0,1};int dy[4]={0,1,-1,0};struct code{int x,y,num;code(int a,int b,int c){x=a;y=b;num=c;}};queue<code>q;int main(){int cas,c,i,j;char s1[17];scanf("%d",&cas);for(c=1;c<=cas;c++){while(!q.empty())q.pop();int n,cnt=0;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%s",&s1[1]);for(j=1;j<=n;j++){if(s1[j]=='.')graph[i][j]=0;else if(s1[j]=='#')graph[i][j]=-1;else {graph[i][j]=s1[j]-'A'+1;cnt++;if(graph[i][j]==1){q.push(code(i,j,0));}}}}int flag=1,tar=2,ans=0;for(i=1;i<=cnt-1;i++){memset(vis,0,sizeof(vis));int temp1,temp2;temp1=q.front().x;temp2=q.front().y;vis[temp1][temp2]=1;int f=0;int tar_x,tar_y;while(!q.empty()){code pos=q.front();q.pop();for(j=0;j<4;j++){int xx=pos.x+dx[j];int yy=pos.y+dy[j];//cout<<xx<<" "<<yy<<endl;if(xx<1||xx>n||yy<1||yy>n)continue;if(graph[xx][yy]==tar&&!vis[xx][yy]){ans+=pos.num+1;f=1;tar_x=xx;tar_y=yy;break;}else if(graph[xx][yy]==0&&!vis[xx][yy]){q.push(code(xx,yy,pos.num+1));vis[xx][yy]=1;}}if(f==1)break;}if(f==1){while(!q.empty())q.pop();tar++;q.push(code(tar_x,tar_y,0));graph[temp1][temp2]=0;}else {flag=0;break;}}if(flag)printf("Case %d: %d\n",c,ans);else printf("Case %d: Impossible\n",c);}return 0;}
0 0
- LightOJ-1066-bfs
- Lightoj 1066 Gathering Food(BFS)
- Lightoj 1141 BFS
- Lightoj 1009 BFS
- LightOJ 1174--Commandos (BFS)
- LightOJ 1175 (BFS)
- LightOJ-1046-BFS
- lightoj 1099【dijkstra/BFS】
- LightOJ-1094-图论,bfs
- lightoj-1141【bfs】
- LightOJ——1066Gathering Food(BFS)
- LightOJ 1141 Number Transformation bfs
- LightOJ 1012(基础图bfs)
- LightOJ - 1039 A Toy Company(BFS)
- LightOJ 1238 Power Puff Girls(BFS)
- 【LightOJ】1122 - Digit Count(bfs)
- LightOJ 1111 Best Picnic Ever 简单bfs
- LightOJ - 1150 Ghosts!(二分图+bfs+枚举)
- 读取联系人
- 大型网站技术架构
- java常用的第三方组件
- 深入理解ThreadLocal
- 设计模式之单例模式(java语言)
- LightOJ-1066-bfs
- 开发ROS 程序包控制机械臂
- 调用 IRtlKey
- 关于MapReduce传参数
- Spring 与 ActiveMq的集成配置使用
- 【Android - 进阶】之图片三级缓存的原理及实现
- [数据结构与算法]#ifndef/#define/#endif使用详解
- dpkg详解
- Android中的Handler的机制与用法详解