[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612
InputThe 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 OutputFor 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 开始的时候想都没想就先找出所有的中间点,然后计算每个中间点,Y和M到他们的最短距离,然后求出最短的。
果然超时
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 40005
int n,m,dx[]={,-,,},dy[]={,,,-},num,sum[maxn],vis[][];
char mapn[][];
struct point{
int x,y;
};
point p[maxn];
struct node{
int x,y,step;
};
void bfs(int a,int b,int c,int d,int cnt){
node p;
p.x = a,p.y = b,p.step = ;
queue<node> q;
q.push(p);
while(!q.empty()){
node tmp = q.front();
q.pop();
if(tmp.x == c && tmp.y == d){
sum[cnt] += tmp.step;
return;
}
for(int i=;i<;i++){
int xx = tmp.x + dx[i];
int yy = tmp.y + dy[i];
if(xx> && xx<=n && yy> && yy<=m && !vis[xx][yy] && mapn[xx][yy]!='#'){
vis[xx][yy] = ;
node tp;
tp.x = xx,tp.y = yy,tp.step = tmp.step + ;
q.push(tp);
}
}
}
}
int main()
{
while(cin >> n >> m){
num = ;
memset(sum,,sizeof(sum));
if(!n || !m){
break;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin >> mapn[i][j];
if(mapn[i][j] == '@'){
p[num].x = i,p[num].y = j,num++;
}
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == 'Y' || mapn[i][j] == 'M'){
for(int k=;k<num;k++){
memset(vis,,sizeof(vis));
bfs(i,j,p[k].x,p[k].y,k);
}
}
}
}
sort(sum,sum+num);
cout << sum[] * << endl;
}
return ;
}
后面是先计算好Y,M到每个点的最短距离,这样只需要循环Y,M的个数次数就行,不需再套上中间点个数的循环。
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 40005
#define inf 0x1f1f1f1f
int n,m,dx[]={,-,,},dy[]={,,,-},num,sum[][][],vis[][],z;
char mapn[][];
struct node{
int x,y,step;
};
void bfs(int a,int b){
memset(vis,,sizeof(vis));//在这里重置vis数组
node p;
p.x = a,p.y = b,p.step = ;
vis[p.x][p.y] = ;
queue<node> q;
q.push(p);
while(!q.empty()){
node tmp = q.front();
q.pop();
for(int i=;i<;i++){
int xx = tmp.x + dx[i];
int yy = tmp.y + dy[i];
if(xx> && xx<=n && yy> && yy<=m && !vis[xx][yy] && mapn[xx][yy]!='#'){
vis[xx][yy] = ;
node tp;
tp.x = xx,tp.y = yy,tp.step = tmp.step + ;
sum[tp.x][tp.y][z] = tp.step;
q.push(tp);
}
}
}
}
int main()
{
while(cin >> n >> m){
num = ;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
sum[i][j][] = inf;
sum[i][j][] = inf;//注意记录距离的sum数组一定要初始化为一个很大的数
}
}
if(!n || !m){
break;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin >> mapn[i][j];
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == 'Y'){
z = ;
bfs(i,j);
}
if(mapn[i][j] == 'M'){
z = ;
bfs(i,j);
}
}
}
int ans = inf;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == '@'){
ans = min(ans,sum[i][j][] + sum[i][j][]);
}
}
}
cout << ans* << endl;
}
return ;
}
[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612的更多相关文章
- [kuangbin带你飞]专题一 简单搜索 题解报告
又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...
- [kuangbin带你飞]专题一 简单搜索(回顾)
A - 棋盘问题 POJ - 1321 注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了. AC代码: #include<iostream> #include< ...
- [kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple
//Memory Time //2236K 32MS #include<iostream> using namespace std; ]; //保存每次mod n的余数 //由于198的余 ...
- [kuangbin带你飞]专题一 简单搜索 棋盘问题
题来:链接https://vjudge.net/problem/OpenJ_Bailian-132 J - 棋盘问题 1.题目: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别. ...
- [kuangbin带你飞]专题一 简单搜索
ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题 328 / 854 Problem B POJ 2251 Dungeon Ma ...
- [kuangbin带你飞]专题一 简单搜索 回顾总结
第二题:bfs,忘了将queue清空. 第三题:bfs,记得使用vis数组,防止重复入队
- 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
随机推荐
- Apache NiFi 核心概念和关键特性
本文来源于官方文档翻译 NiFi 的核心概念 NiFi 最早是美国国家安全局内部使用的工具,用来投递海量的传感器数据.后来由 apache 基金会开源.天生就具备强大的基因.NiFi基本设计理念与 F ...
- c#小灶——数据类型
C#中有许多数据类型,存储不同的数据要用不同的数据类型.我们这里面向初学只介绍值类型,引用类型和指针类型在后续的学习中会有接触. 整型 int是最常用的整型,用来存储整数.除了int之外,还有其他不常 ...
- oracle 断电启动失败:ORA-00600: internal error code, arguments
转载地址: http://www.2cto.com/database/201312/261602.html 由于服务器断电,启动 oracle 时报 ORA-00600 错误 查看 oracle tr ...
- 把Python项目打包成exe文件
我们很多时候,写好的程序需要打包成.exe文件才可以发给客户,那么今天我就来谈一谈,如何将一个写好的Python程序打包成exe文件! 首先,我们我们使用到的工具是python 3.7 和 Pyins ...
- 《机器学习基石》---VC维
1 VC维的定义 VC维其实就是第一个break point的之前的样本容量.标准定义是:对一个假设空间,如果存在N个样本能够被假设空间中的h按所有可能的2的N次方种形式分开,则称该假设空间能够把N个 ...
- php Basic HTTP与Digest HTTP 应用
Basic HTTP 认证范例 <?php //Basic HTTP 认证 if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authen ...
- 四、Ansible的Galaxy包管理器
一.什么是Ansible Galaxy? Ansible Galaxy是Ansible的第三方插件管理和安装工具,其实就是包管理软件.作用类似于Ubuntu的apt,Centos的yum,Python ...
- python之web自动化验证码识别解决方案
验证码识别解决方案 对于web应用程序来讲,处于安全性考虑,在登录的时候,都会设置验证码,验证码的类型种类繁多,有图片中辨别数字字母的,有点击图片中指定的文字的,也有算术计算结果的,再复杂一点就是滑动 ...
- 2015-11-13 linux基础笔记
1.安装linux 使用光盘,版本CENSOS6.6 2.命令过长请使用\ 后enter键换行转义 直到不需要转义后回车运行 3.linux 大小写敏感 4.显示terminal 输出语言 ec ...
- aabccd统计每个字符出现的次数,结果显示{ a: 2, b: 1, c: 2, d: 1 };去掉重复的字符,使结果显示abcd
遍历字符串的方式和遍历数组的方式有点相似,或者说就是相同的.在学习数组的遍历方法之前,可以通过for循环去遍历数组,同样,字符串也可以:字符串跟数组都有一个length的属性.下面代码奉上,个人思路! ...