两次搜索找最小路径和—— Find a way
来源:互联网 发布:郑知文人妖的忠诚歌词 编辑:程序博客网 时间:2024/04/28 15:21
Find a way
Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format: Java class name:Submit Status
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
Sample Input
4 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
Sample Output
668866
题意:给一幅图,有墙,有KFC,有路。两个人要去KFC约会,有很多个KFC,问两个人去一间KFC总共走的最少步数。
由于输入判断 Y M 时打错了,呜呜~~~找了两个小时的错误
方案一:题目两个人到KFC见面,两个人在不同的位置,KFC也不只一个,对每个人BFS广搜,求出每个人到各个KFC的最短距离,最后对于多个KFC地点,求出两个人到这的最小步数的和,取最小值就可以了。
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3f3f#include<queue>using namespace std;int n,m;char Map[210][210];int dis_a[210][210],dis_b[210][210];const int c[4][2]={-1,0,0,-1,1,0,0,1};struct node{int x,y;};bool check(int i,int j){if(i<n && i>=0 && j<m && j>=0 && Map[i][j]!='#')return true;return false;}void bfs(int xx,int yy,int num[210][210]){node t,tmp;t.x=xx;t.y=yy;num[xx][yy]=0;queue<node> q;q.push(t);while(!q.empty()){t=q.front();q.pop();for(int i=0;i<4;i++){tmp.x=t.x+c[i][0];tmp.y=t.y+c[i][1];if(num[tmp.x][tmp.y]==0 && check(tmp.x,tmp.y)){num[tmp.x][tmp.y]=num[t.x][t.y]+1;q.push(tmp);}}}}int main(){int x1,y1,x2,y2;while(~scanf("%d%d",&n,&m)){memset(Map,0,sizeof(Map));memset(dis_a,0,sizeof(dis_a));memset(dis_b,0,sizeof(dis_b));for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>Map[i][j];if(Map[i][j]=='Y'){x1=i;y1=j;}if(Map[i][j]=='M'){x2=i;y2=j;}}}bfs(x1,y1,dis_a);bfs(x2,y2,dis_b);int minn=INF;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(Map[i][j]=='@' && dis_a[i][j]!=0)minn=min(minn,dis_a[i][j]+dis_b[i][j]);}}printf("%d\n",minn*11);}return 0;}
方案二:
#include<iostream>#include<stdio.h>#include<string.h>#define INF 0x3f3f3f3f#include<queue>using namespace std;int n,m;char Map[210][210];int vis[210][210],dis[210][210];const int c[4][2]={-1,0,0,-1,1,0,0,1};struct node{int x,y;int step;};bool check(int i,int j){if(i<n && i>=0 && j<m && j>=0 && Map[i][j]!='#')return true;return false;}void bfs(int xx,int yy,int num){memset(vis,0,sizeof(vis));node t,tmp;t.x=xx;t.y=yy;t.step=0;queue<node> q;q.push(t);vis[t.x][t.y]=1;while(!q.empty()){t=q.front();q.pop();for(int i=0;i<4;i++){tmp=t;tmp.x=t.x+c[i][0];tmp.y=t.y+c[i][1];tmp.step++;if(!vis[tmp.x][tmp.y] && check(tmp.x,tmp.y)){vis[tmp.x][tmp.y]=1;q.push(tmp);if(Map[tmp.x][tmp.y]=='@'){if(num==1)dis[tmp.x][tmp.y]=tmp.step;elsedis[tmp.x][tmp.y]+=tmp.step;}}}}}int main(){int x1,y1,x2,y2;while(~scanf("%d%d",&n,&m)){memset(Map,0,sizeof(Map));memset(dis,INF,sizeof(dis));for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>Map[i][j];if(Map[i][j]=='Y'){x1=i;y1=j;}if(Map[i][j]=='M'){x2=i;y2=j;}}}bfs(x1,y1,1);bfs(x2,y2,2);int minn=INF;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(Map[i][j]=='@' && dis[i][j]<minn)minn=dis[i][j];}}printf("%d\n",minn*11);}return 0;}
0 0
- 两次搜索找最小路径和—— Find a way
- HDU2612(Find a way)搜索-两次BFS
- HDU 2612 Find a way(两次BFS)
- HDOJ Find a way(两次bfs)
- HDU 2612 Find a way(两次bfs)
- hdu2612-Find a way(两次bfs)
- hdu2612——Find a way
- HDU2612 Find a way —— BFS
- hdu 2612 Find a way(搜索)
- ACM 搜索 hdu 2612 Find a way
- 杭电搜索 2612 Find a way
- Find a way bfs搜索 容易出错
- hdu 2612 Find a way(两次BFS)
- HDU 2612 Find a way(两次BFS)
- HDU 2612 Find a way (两次bfs)
- hdu 2612Find a way 两次bfs+存表
- ACM-bfs之Find a way——hdu2612
- HDU——2612 Find a way(广搜)
- PopUpWindow使用详解(二)——进阶及答疑
- Linux获取网卡MAC地址命令
- 【控件操作】DataGridView绑定数据源
- 搭建Java开发环境傻瓜式教程(多图,流量党慎点!)
- 装完Centos7提示Initial setup of CentOS Linux 7 (core)
- 两次搜索找最小路径和—— Find a way
- 模型树------构建模型树
- java Script入门
- LeetCode 283 Move Zeroes
- UML图中标志
- Android studio常见问题
- C++中引用的详解
- spring IOC原理
- STL map 用法