hdu-2612-find a way(bfs)
来源:互联网 发布:成都行知幼稚园收费 编辑:程序博客网 时间:2024/05/11 04:51
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8468 Accepted Submission(s): 2729
Problem Description
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
题意:就是Y和M在@相遇的最短的时间。
思路:基本的bfs,先Y搜一次,然后M搜一次,纪录到每一个@的时间,然后求出Y和M在@相遇的最短的时间。
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <queue>#define INF 0x3f3f3f3f#define CSH(a, b) memset(a, (b), sizeof(a))#define M 221//数组大小#define N 221//数组大小using namespace std;char s[N][M];int dir[4][2]={-1,0, 1,0, 0,-1, 0,1};int vis1[N][M],vis2[N][M];int m,n;int flag[N][M],flag1[N][M];struct node{ int x; int y; int ti;};node fr1,fr2;void intt(){ CSH(vis1,0); CSH(vis2,0); for(int i=0;i<n;i++) { getchar(); for(int j=0;j<m;j++) { scanf("%c",&s[i][j]); if(s[i][j]=='Y') { fr1.x=i; fr1.y=j; fr1.ti=0; vis1[i][j]=1; } else if(s[i][j]=='M') { fr2.x=i; fr2.y=j; fr2.ti=0; vis2[i][j]=1; } } }}void bfs(){ queue<node> q; while(!q.empty()) { q.pop(); } q.push(fr1); while(!q.empty()) { node ne; ne=q.front(); q.pop(); for(int i=0;i<4;i++) { node no; no.x=ne.x+dir[i][0]; no.y=ne.y+dir[i][1]; no.ti=ne.ti; if(no.x<n&&no.x>=0&&no.y<m&&no.y>=0&&s[no.x][no.y]!='#'&&!vis1[no.x][no.y]) { no.ti++; if(s[no.x][no.y]=='@') { flag[no.x][no.y]=no.ti; } vis1[no.x][no.y]=1; q.push(no); } } }}void bfs1(){ queue<node> q; while(!q.empty()) { q.pop(); } q.push(fr2); while(!q.empty()) { node ne; ne=q.front(); q.pop(); for(int i=0;i<4;i++) { node no; no.x=ne.x+dir[i][0]; no.y=ne.y+dir[i][1]; no.ti=ne.ti; if(no.x<n&&no.x>=0&&no.y<m&&no.y>=0&&s[no.x][no.y]!='#'&&!vis2[no.x][no.y])//判断边界 { no.ti++; if(s[no.x][no.y]=='@') { flag1[no.x][no.y]=no.ti; } vis2[no.x][no.y]=1; //操作.. q.push(no); } } }}int main(){ while(~scanf("%d%d",&n,&m)) { CSH(flag,-1); CSH(flag1,-1); intt(); bfs(); bfs1(); int min=INF; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(flag[i][j]+flag1[i][j]<min&&flag[i][j]+flag1[i][j]>0) { min=flag[i][j]+flag1[i][j]; } } printf("%d\n",min*11); }}
0 0
- hdu 2612 Find a way(BFS)
- HDU 2612 Find a way(两次BFS)
- HDU 2612 Find a way(BFS)
- HDU 2612 Find a way BFS
- Hdu 2612 Find a way(bfs)
- HDU 2612 Find a way (BFS)
- HDU 2612--Find a way【BFS】
- HDU 2612 Find a way(双BFS)
- hdu 2612 Find a way(双向bfs)
- HDU 2612 Find a way (bfs)
- HDU 2612 Find a way(bfs)
- HDU - 2612 - Find a way (BFS)
- HDU 2612 Find a way【bfs】
- BFS-HDU-2612-Find a Way
- HDU 2612 Find a way (BFS)
- hdu 2612 Find a way (BFS)
- HDU 2612 Find a way(两次bfs)
- HDU 2612 Find a way(BFS)
- c++单例 通过cocos2dx重现c++单例—— c++语法复习
- python csv模块
- void类型指针
- Hadoop介绍
- 关于毕业设计
- hdu-2612-find a way(bfs)
- 前端优化那点事
- 【解题报告】食物链[codevs 1074][rqnoj 455]
- Maximum Likelihood(ML) 和 Maximum a posterior(MAP)的直观理解
- hbase问题
- 文本分类学习笔记(6)- 贝叶斯
- linux学习笔记 一些快捷键
- 免安装mysql环境配置、完全卸载和无法启动问题详解
- 7.1 RecyclerView 实现瀑布流