hdu 2612(Find a way)(简单广搜)
Find a way
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 30 Accepted Submission(s) : 14
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
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
Sample Input
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
Sample Output
66
88
66
#include <iostream>
#include<cstring>
#include<cstdio>
#include<deque>
#include<algorithm>
using namespace std;
int dr[][]={{,},{,},{-,},{,-} };
int n,m,i,j,num,x1,x2,y1,y2,ans;
int vis[][],a[][];
char mp[][];
struct node
{
int x,y,ti;
};
deque<node> s;
void bfs(int x,int y)
{
int t=num;
node p;
p.x=x;
p.y=y;
p.ti=;
s.clear();
s.push_back(p);
vis[x][y]=;
while(!s.empty())
{
node q=s.front();
for(i=;i<;i++)
{
int xx=q.x+dr[i][];
int yy=q.y+dr[i][];
if(xx>= && xx<n && yy>= && yy<m && mp[xx][yy]!='#')
if (!vis[xx][yy])
{
p.x=xx;
p.y=yy;
p.ti=q.ti+;
s.push_back(p);
vis[xx][yy]=;
if(mp[xx][yy]=='@')
{
a[xx][yy]+=p.ti;
ans=min(ans,a[xx][yy]);
t--;
}
if(t==) return;
}
}
s.pop_front();
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(i=;i<n;i++)
{
scanf("%s",&mp[i]);
for(j=;j<m;j++)
if (mp[i][j]=='Y') x1=i,y1=j;
else if(mp[i][j]=='M') x2=i,y2=j;
else if (mp[i][j]=='@') {num++; a[i][j]=;}
}
memset(vis,,sizeof(vis));
bfs(x1,y1);
ans=;
memset(vis,,sizeof(vis));
bfs(x2,y2);
printf("%d\n",ans*);
}
return ;
}
hdu 2612(Find a way)(简单广搜)的更多相关文章
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- hdu 1195 Open the Lock(广搜,简单)
题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...
- hdu 1180 诡异的楼梯(广搜,简单)
题目 挺简单的一道广搜题,只要用判断时间是偶数还是奇数就可以判断楼梯的方位,但是我这傻逼居然写了那么久啊那么久,我果然秀逗了,,,, #define _CRT_SECURE_NO_WARNINGS # ...
- hdu 1253 胜利大逃亡(广搜,队列,三维,简单)
题目 原来光搜是用队列的,深搜才用栈,我好白痴啊,居然搞错了 三维的基础的广搜题 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #in ...
- HDU 1548 A strange lift (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
- POJ 3126 Prime Path 简单广搜(BFS)
题意:一个四位数的质数,每次只能变换一个数字,而且变换后的数也要为质数.给出两个四位数的质数,输出第一个数变换为第二个数的最少步骤. 利用广搜就能很快解决问题了.还有一个要注意的地方,千位要大于0.例 ...
- POJ1376简单广搜
题意: 给你一个n*m的矩阵,然后给你机器人的起点和终点,还有起点的方向,然后每次机器人有两种操作,左右旋转90度,或者是朝着原来的方向走1,2或者3步,机器人再走的过程中不能碰到格子,也 ...
- (简单广搜) Ice Cave -- codeforces -- 540C
http://codeforces.com/problemset/problem/540/C You play a computer game. Your character stands on so ...
- HDU 1180 诡异的楼梯 (广搜)
题目链接 Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一 ...
随机推荐
- Leetcode016 3Sum Closest
public class S016 { //借鉴S015的思想,只是稍微有点慢 public int threeSumClosest(int[] nums, int target) { Arrays. ...
- LeetCode OJ 153. Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- VS2010编译错误 LNK 2019 unresolved external symbol错误解决办法
Link错误有很多种,主要是没有在连接中加入lib文件路径,或者lib配置正确,传参错误 一个solution里面多个project之间引用其他project函数会出现这个错误,由于包含了头文件而没处 ...
- WinSnap屏幕截图 V4.5.6 官方最新版
软件名称: WinSnap屏幕截图软件语言: 多国语言授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 2.7MB图片预览: 软件简介:WinS ...
- CentOS安装配置JDK-7(.rpm)
声明:本文转自:http://www.cnblogs.com/zhoulf/archive/2013/02/04/2891608.html 安装说明 系统环境:centos-6.3安装方式:rpm安装 ...
- php 分页类(3)
<?php class Page { private $total; //总记录 private $pagesize; //每页显示多少条 private $limit; //limit pri ...
- 数组按时间(字符串->Date)排序
不说了 ,直接上代码.Talk is cheap , show me the code. NSArray *stortedArray = [wkSelf.dataArray sortedArrayUs ...
- html5--基础笔记
1.对于<a>标签 以前: <h2><a href="index.html">The home page</a></h2> ...
- C# 读取二进制文件
using UnityEngine; using System.Collections; using System; using System.IO; public class Test : Mono ...
- linux脚本Shell之awk详解
一.基本介绍1.awk: awk是一个强大的文本分析工具,在对文本文件的处理以及生成报表,awk是无可替代的.awk认为文本文件都是结构化的,它将每一个输入行定义为一个记录,行中的每个字符串定义为一个 ...