博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 2612 Find a way
阅读量:5283 次
发布时间:2019-06-14

本文共 3745 字,大约阅读时间需要 12 分钟。

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 19320    Accepted Submission(s): 6234

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.
 

 

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
 

 

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 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
 

 

Sample Output
66 88 66
 
#include 
#include
using namespace std;char a[205][205];int y[205][205],m[205][205];int d[4][2]={-1,0,1,0,0,-1,0,1};int n,k;int yx,yy,mx,my;queue
q;queue
qy;queue
qm;bool safe(int x,int y){ if(x>n||y>k||x<=0||y<=0) return 0; return 1;}int main(){ while(cin>>n>>k) { int i,j; long long min=9999999; for(i=1;i<=n;i++) { for(j=1;j<=k;j++) { cin>>a[i][j]; y[i][j]=-1; m[i][j]=-1; if(a[i][j]=='Y') { qy.push(i); qy.push(j); y[i][j]=0; } if(a[i][j]=='M') { qm.push(i); qm.push(j); m[i][j]=0; } if(a[i][j]=='@') { q.push(i); q.push(j); } } } while(!qy.empty()||!qm.empty()) { if(!qy.empty()) { yx=qy.front();qy.pop(); yy=qy.front();qy.pop(); int vy=y[yx][yy]; for(i=0;i<4;i++) { if(safe(yx+d[i][0],yy+d[i][1])&&a[yx+d[i][0]][yy+d[i][1]]!='#'&&y[yx+d[i][0]][yy+d[i][1]]==-1) { y[yx+d[i][0]][yy+d[i][1]]=vy+1; qy.push(yx+d[i][0]); qy.push(yy+d[i][1]); } } } if(!qm.empty()) { mx=qm.front();qm.pop(); my=qm.front();qm.pop(); int vm=m[mx][my]; for(i=0;i<4;i++) { if(safe(mx+d[i][0],my+d[i][1])&&a[mx+d[i][0]][my+d[i][1]]!='#'&&m[mx+d[i][0]][my+d[i][1]]==-1) { m[mx+d[i][0]][my+d[i][1]]=vm+1; qm.push(mx+d[i][0]); qm.push(my+d[i][1]); } } } } int kx,ky; while(!q.empty()) { kx=q.front();q.pop(); ky=q.front();q.pop(); if(y[kx][ky]==-1||m[kx][ky]==-1) continue; long long s=y[kx][ky]+m[kx][ky]; if(s

 

转载于:https://www.cnblogs.com/caiyishuai/p/8411170.html

你可能感兴趣的文章
如何将数据库中的表导入到PowerDesigner中(转)
查看>>
汇编总结一
查看>>
html5-表单常见操作
查看>>
String = ""和String = null的区别
查看>>
C#测试题若干,都是基础阿
查看>>
NetWork——关于TCP协议的三次握手和四次挥手
查看>>
An easy problem
查看>>
MauiMETA工具的使用(一)
查看>>
LeetCode: Anagrams 解题报告
查看>>
用cookie登录慕课网络教学中心刷评论
查看>>
Qt 中获取本机IP地址
查看>>
基本数据类型(int, bool, str)
查看>>
070102_赌博设计:概率的基本概念,古典概型
查看>>
IT人生的价值和意义 感觉真的有了
查看>>
Linux命令之df
查看>>
JS DOM对象
查看>>
python正则表达式
查看>>
OGR – Merging Multiple SHP files
查看>>
创业公司该不该被收购?(转)
查看>>
sqlserver 行转列、列转行[转]
查看>>