HDUOJ----(2612)Find a way
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为中心搜索
代码:
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<deque>
6 using namespace std;
7 const int maxn=201;
8 typedef struct Map
9 {
10 char ss;
11 int x,y;
12 int value;
13 }gmap;
14 unsigned int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; /*·½Ïò*/
15 gmap bb[maxn][maxn];
16 int ans[maxn][maxn];
17 int n,m;
18 void bfs1(gmap a[][201],int x,int y)
19 {
20 deque<gmap>q;
21 gmap temp;
22 q.push_back(a[x][y]);
23 while(!q.empty())
24 {
25 temp=q.front();
26 q.pop_front();
27 for(int i=0;i<4/*&&temp.ss!='@'*/;i++)
28 {
29
30 if((temp.x+dir[i][0])<0||(temp.x+dir[i][0])>=n||(temp.y+dir[i][1])<0||(temp.y+dir[i][1])>=m) continue;
31 if(a[temp.x+dir[i][0]][temp.y+dir[i][1]].ss=='.'||a[temp.x+dir[i][0]][temp.y+dir[i][1]].ss=='@')
32 {
33 a[temp.x+dir[i][0]][temp.y+dir[i][1]].value+=temp.value+1;
34 q.push_back(a[temp.x+dir[i][0]][temp.y+dir[i][1]]);
35 if(a[temp.x+dir[i][0]][temp.y+dir[i][1]].ss=='@')
36 {
37 a[temp.x+dir[i][0]][temp.y+dir[i][1]].ss='d';
38 ans[temp.x+dir[i][0]][temp.y+dir[i][1]]+=a[temp.x+dir[i][0]][temp.y+dir[i][1]].value;
39 }
40 else
41 a[temp.x+dir[i][0]][temp.y+dir[i][1]].ss='r';
42 }
43 }
44 }
45 }
46
47 int main()
48 {
49 int i,j,sx1,sy1,sx2,sy2;
50 while(scanf("%d%d",&n,&m)!=EOF)
51 {
52 getchar();
53 memset(ans,0,sizeof(ans));
54 for(i=0;i<n;i++)
55 {
56 for(j=0;j<m;j++)
57 {
58 scanf("%c",&bb[i][j].ss);
59 bb[i][j].value=0;
60 bb[i][j].x=i;
61 bb[i][j].y=j;
62 if(bb[i][j].ss=='Y')
63 {
64 sx1=i;
65 sy1=j;
66 }
67 else if(bb[i][j].ss=='M')
68 {
69 sx2=i;
70 sy2=j;
71 }
72 }
73 getchar();
74 }
75 bfs1(bb,sx1,sy1);
76 for(i=0;i<n;i++)
77 {
78 for(j=0;j<m;j++)
79 {
80 bb[i][j].value=0;
81 if(bb[i][j].ss=='d')
82 bb[i][j].ss='@';
83 else
84 if(bb[i][j].ss=='r')
85 bb[i][j].ss='.';
86 }
87 }
88 bfs1(bb,sx2,sy2);
89 int minm=INT_MAX;
90 for(i=0;i<n;i++)
91 {
92 for(j=0;j<m;j++)
93 {
94 if(bb[i][j].ss=='d'&&minm>ans[i][j])
95 minm=ans[i][j];
96 }
97 }
98 printf("%dn",minm*11);
99 }
100 return 0;
101 }
- 利用TensorFlow生成图像标题
- 保存并加载您的Keras深度学习模型
- 简单、通用的JQuery Tab实现
- Dubbo源码解析 - 远程暴露
- 使用FastText(Facebook的NLP库)进行文本分类和word representatio...
- 声音分类的迁移学习
- 【死磕Java并发】—– J.U.C之AQS:CLH同步队列
- 使用Python完成你的第一个学习项目
- CA,给了数据库,给了机器,为啥也扩不了容?
- 如何使用Anaconda设置机器学习和深度学习的Python环境
- MQ,互联网架构解耦神器
- 预测随机机器学习算法实验的重复次数
- 服务化了,没想到耦合更加严重?
- 如何在Python中扩展LSTM网络的数据
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法