Find a way
来源:互联网 发布:网络监控系统设计说明 编辑:程序博客网 时间:2024/06/05 16:42
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
题意:用了两次BFS,求Y和M到@的最小距离
#include<cstdio>#include<queue>#include<iostream>#include<cstring>using namespace std;char ch[205][205];int a[205][205], b[205][205];int dir[4][2] = {1,0,-1,0,0,1,0,-1};int n, m;struct node{ int x, y; int sign;}s,ss, v1, f, ff,v2;void bfs1(node v){ queue <node>q; q.push(v); while(!q.empty()) { f = q.front(); q.pop(); for(int i = 0; i < 4; ++i) { v1.x = f.x +dir[i][0]; v1.y = f.y +dir[i][1]; if(v1.x < n&&v1.x>=0&&v1.y<m&&v1.y>=0) { if(ch[v1.x][v1.y]!='#'&&a[v1.x][v1.y]==0) { v1.sign = f.sign+1; a[v1.x][v1.y] = v1.sign; q.push(v1); } } } }}void bfs2(node vv){ queue <node>q; q.push(vv); while(!q.empty()) { ff = q.front(); q.pop(); for(int i = 0; i < 4; ++i) { v2.x = ff.x +dir[i][0]; v2.y = ff.y +dir[i][1]; if(v2.x < n&&v2.x>=0&&v2.y<m&&v2.y>=0) { if(ch[v2.x][v2.y]!='#'&&b[v2.x][v2.y]==0) { v2.sign = ff.sign+1; b[v2.x][v2.y] = v2.sign; q.push(v2); } } } }}int main(){ int i, j, k, t; int yx, yy, mx, my; while(~scanf("%d%d", &n, &m)) { for(i = 0; i < n; ++i) { scanf("%s", ch[i]); } for(i = 0; i < n; ++i) { for(j = 0; j < m; ++j) { if(ch[i][j] == 'Y') { s.x = i; s.y = j; s.sign = 1; memset(a, 0, sizeof(a)); a[i][j] = 1; bfs1(s); } if(ch[i][j]=='M') { ss.x = i; ss.y = j; ss.sign = 1; memset(b, 0, sizeof(b)); b[i][j] = 1; bfs2(ss); } } } int maxn = 2147483647; for(i = 0; i < n; ++i) { for(j = 0; j < m; ++j) { if(ch[i][j] == '@') { if(a[i][j]+b[i][j]<maxn&&a[i][j]!=0&&b[i][j]!=0) { maxn = a[i][j]+b[i][j]; } } } } printf("%d\n", (maxn-2)*11); } return 0;}
0 0
- Find a way
- Find a way hdu2612
- Find a way
- Find a way 2612
- HDU2612---Find a way
- Find a way
- hdu2612---Find a way
- Find a way
- N. Find a way
- HDU2612 Find a way
- Find a way--hdoj
- hdu Find a way
- Find a way
- Find a way
- hdu Find a way
- Find a way
- Find a way<hdoj2612>
- HDU:Find a way
- 操作系统实验一(进程管理)
- PO VO BO DTO POJO DAO之间的关系
- 使用C语言删除文本文档中的一行数据
- 排序算法之快速排序
- lucene
- Find a way
- 使用FastJSON,将对象或数组和JSON串互转
- hdu2084——数塔
- MAC OS X Yosemite 10.10.5 转发80(HTTP)端口的方法
- Verilog 编程实验(4)-7位译码器的设计与实现
- React Native学习
- java中输入与输出(I/O)和流的概念:
- Unbias Writing
- 用SourceTree轻巧Git项目图解