Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3177    Accepted Submission(s): 1031

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
简单的搜索,bfs,以Y和M为中心搜索
代码:

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<deque>
using namespace std;
const int maxn=;
typedef struct Map
{
char ss;
int x,y;
int value;
}gmap;
unsigned int dir[][]={{,},{-,},{,},{,-}}; /*·½Ïò*/
gmap bb[maxn][maxn];
int ans[maxn][maxn];
int n,m;
void bfs1(gmap a[][],int x,int y)
{
deque<gmap>q;
gmap temp;
q.push_back(a[x][y]);
while(!q.empty())
{
temp=q.front();
q.pop_front();
for(int i=;i</*&&temp.ss!='@'*/;i++)
{ if((temp.x+dir[i][])<||(temp.x+dir[i][])>=n||(temp.y+dir[i][])<||(temp.y+dir[i][])>=m) continue;
if(a[temp.x+dir[i][]][temp.y+dir[i][]].ss=='.'||a[temp.x+dir[i][]][temp.y+dir[i][]].ss=='@')
{
a[temp.x+dir[i][]][temp.y+dir[i][]].value+=temp.value+;
q.push_back(a[temp.x+dir[i][]][temp.y+dir[i][]]);
if(a[temp.x+dir[i][]][temp.y+dir[i][]].ss=='@')
{
a[temp.x+dir[i][]][temp.y+dir[i][]].ss='d';
ans[temp.x+dir[i][]][temp.y+dir[i][]]+=a[temp.x+dir[i][]][temp.y+dir[i][]].value;
}
else
a[temp.x+dir[i][]][temp.y+dir[i][]].ss='r';
}
}
}
} int main()
{
int i,j,sx1,sy1,sx2,sy2;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
memset(ans,,sizeof(ans));
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
scanf("%c",&bb[i][j].ss);
bb[i][j].value=;
bb[i][j].x=i;
bb[i][j].y=j;
if(bb[i][j].ss=='Y')
{
sx1=i;
sy1=j;
}
else if(bb[i][j].ss=='M')
{
sx2=i;
sy2=j;
}
}
getchar();
}
bfs1(bb,sx1,sy1);
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
bb[i][j].value=;
if(bb[i][j].ss=='d')
bb[i][j].ss='@';
else
if(bb[i][j].ss=='r')
bb[i][j].ss='.';
}
}
bfs1(bb,sx2,sy2);
int minm=INT_MAX;
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(bb[i][j].ss=='d'&&minm>ans[i][j])
minm=ans[i][j];
}
}
printf("%d\n",minm*);
}
return ;
}

最新文章

  1. Zookeeper数据模型及其应用
  2. Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
  3. 《oracle每日一练》Oracle DBLink连接数过多的问题(Ora-02020)
  4. [IOS UICollectionView模版]
  5. 一元三次方程 (codevs 1038)题解
  6. [MODx] 6. Cache &#39;!&#39; with login package
  7. bootstrap 模态框关闭状态怎么获取
  8. Java Fluent Restful API自动化测试框架
  9. 一款很不错的html转xml工具-Html Agility Pack
  10. 在Visual Studio中使用GitHub(使用篇)
  11. Hbase深入学习(一) 什么是hbase
  12. PHP之APC缓存详细介绍(学习整理)
  13. Hibernate与IBatis的优缺点及可行性分析
  14. [Python Study Notes] 变量/编码/注释
  15. 【Unity与23种设计模式】观察者模式(Observer)
  16. Linux 桌面玩家指南:17. 在 Ubuntu 中使用 deepin-wine,解决一些依赖 Windows 的痛点问题
  17. kafka系列六、java管理kafka Topic
  18. Java如何将每个单词的第一个字符转为大写?
  19. 通过junit/TestNG+java 实现自动化测试
  20. ZooKeeper系列(8):ZooKeeper伸缩性

热门文章

  1. JavaScript 面向对象编程之一
  2. mysql的TABLE_SCHEMA的sql和information_schema表, MySQL管理一些基础SQL语句, Changes in MySQL 5.7.2
  3. avi视频格式转yuv格式与播放yuv视频
  4. iOS:UITableViewCell自定义单元格
  5. water-and-jug-problem
  6. Reverse Nodes in k-Group leetcode java
  7. Java中浮点类型的精度问题 double float
  8. 页面的缓存设置与meta的作用详细解释
  9. 【Hibernate】Hibernate3.x独立执行时的Failed to load class &amp;quot;org.slf4j.impl.StaticLoggerBinder&amp;quot;错误
  10. 2013级C++第14周(春)项目——多态性、虚函数和抽象类