【hdu】 Find a way (分别从两个点出发BFS,再计算最小值)
来源:互联网 发布:mac 下载器 编辑:程序博客网 时间:2024/05/01 00:00
Find a way
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 6
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
Author
yifenfei
Source
奋斗的年代
// 664.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<cstdio>#include<cstring>#include<queue>#define INF 0x3f3f3f3f#define MAX 210#define MEM(arr,w) memset(arr,w,sizeof(arr));using namespace std;struct node{int x,y;}st1,st2,ss,tt;int dir[4][2]={0,1,1,0,0,-1,-1,0};int n,m,MAXT;char map[MAX][MAX];int visit[MAX][MAX];int step1[MAX][MAX],step2[MAX][MAX];void Init(){MEM(visit,false);MAXT=INF;MEM(step1,0);MEM(step2,0);}void BFS(node st,int step[][210]){queue<node> Q;Q.push(st);visit[st.x][st.y]=true;while(!Q.empty()){ss=Q.front();Q.pop();for(int i=0;i<4;i++){tt.x=ss.x+dir[i][0];tt.y=ss.y+dir[i][1];if(tt.x>=0&&tt.x<n&&tt.y>=0&&tt.y<m&&map[tt.x][tt.y]!='#'&&!visit[tt.x][tt.y])//最基本的条件必须符合{step[tt.x][tt.y]=step[ss.x][ss.y]+1;visit[tt.x][tt.y]=true;Q.push(tt);}}}return ;}int main(){int i,j;while(scanf("%d%d",&n,&m)!=EOF){for(i=0;i<n;i++){scanf("%s",map[i]);for(j=0;j<m;j++){if(map[i][j]=='Y') st1.x=i,st1.y=j;if(map[i][j]=='M') st2.x=i,st2.y=j;}}Init();BFS(st1,step1);MEM(visit,false);BFS(st2,step2);for(i=0;i<n;i++)for(j=0;j<m;j++)if(map[i][j]=='@'&&step1[i][j]!=0&&step2[i][j]!=0)if(step1[i][j]+step2[i][j]<MAXT) MAXT=step1[i][j]+step2[i][j];printf("%d\n",MAXT*11);}}
- 【hdu】 Find a way (分别从两个点出发BFS,再计算最小值)
- 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)
- HDU 2612 Find a way (BFS)
- HDU 2612 Find a way(BFS)
- hdu 2612 Find a way (BFS)
- Find a way(bfs)HDU
- 利用MetaProducts Inquiry 制作离线版网页chm电子书
- Linux下的MAC地址更改(转)--部分验证
- mysql允许从外部连接
- 磁带没被遗忘!现在的小孩更喜欢在YouTube上听音乐
- android 中管理短信
- 【hdu】 Find a way (分别从两个点出发BFS,再计算最小值)
- c++学习之:根据GetLastError()返回值获取错误信息
- 在SAP Netweaver ABAP Trial 上 安装 BI Content
- JavaScript世界的一等公民
- 移动互联网趋势观察
- 详解自动挂载文件系统autofs
- set相关操作源码
- iOS中正确处理dealloc方法
- 关于sizeof 和 strlen的区别