Find a way

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

Total Submission(s): 4772    Accepted Submission(s): 1624

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<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define min(a,b)(a>b?b:a)
char map[1010][1010];
int vis1[1010][1010],vis2[1010][1010],ans1[1010][1010],ans2[1010][1010];
int m,n;
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
struct node
{
int x,y;
int step;
}p,temp;
int judge(node s,int vis[1010][1010])
{
if(s.x<0||s.x>=m||s.y<0||s.y>=n)
return 1;
if(vis[s.x][s.y])
return 1;
if(map[s.x][s.y]=='#')
return 1;
return 0;
}
void dfs(int x,int y,int ans[1010][1010],int vis[1010][1010])
{
p.x=x;
p.y=y;
p.step=0;
vis[x][y]=1;
ans[x][y]=p.step;
queue<node>q;
q.push(p);
while(!q.empty())
{
p=q.front();
q.pop();
for(int i=0;i<4;i++)
{
temp.x=p.x+dx[i];
temp.y=p.y+dy[i];
if(judge(temp,vis))
continue;
temp.step=p.step+1;
ans[temp.x][temp.y]=temp.step;
q.push(temp);
vis[temp.x][temp.y]=1;
}
}
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
int i,j,x1,y1,x2,y2;
for(i=0;i<m;i++)
{
scanf("%s",map[i]);
for(j=0;j<n;j++)
{
if(map[i][j]=='Y')
{
x1=i;y1=j;
}
if(map[i][j]=='M')
{
x2=i;y2=j;
}
}
}
memset(vis1,0,sizeof(vis1));
memset(ans1,0,sizeof(ans1));
dfs(x1,y1,ans1,vis1);
memset(vis2,0,sizeof(vis2));
memset(ans2,0,sizeof(ans2));
dfs(x2,y2,ans2,vis2);
int ans=0xfffffff;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(map[i][j]=='@'&&vis1[i][j]&&vis2[i][j])
{
ans=min(ans,ans1[i][j]+ans2[i][j]);
}
}
printf("%d\n",ans*11);
}
return 0;
}

Find a way--hdoj的更多相关文章

  1. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  5. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

  6. HDOJ(2056)&HDOJ(1086)

    Rectangles    HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...

  7. 继续node爬虫 — 百行代码自制自动AC机器人日解千题攻占HDOJ

    前言 不说话,先猛戳 Ranklist 看我排名. 这是用 node 自动刷题大概半天的 "战绩",本文就来为大家简单讲解下如何用 node 做一个 "自动AC机&quo ...

  8. 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design

    题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...

  9. BFS(八数码) POJ 1077 || HDOJ 1043 Eight

    题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...

  10. HDOJ(1728)逃离迷宫

    HDOJ 1728 http://acm.hdu.edu.cn/showproblem.php?pid=1728 BFS求最少转过的弯 #include <stdio.h> #includ ...

随机推荐

  1. @PathVariable注解的使用和@Requestparam

    一. @PathVariable @PathVariable这是一个路径映射格式的书写方式注解,在类映射路径的后加上/{对应方法参数中属性@PathVariable("code") ...

  2. CSS制作简单loading动画

    曾经以为,loading的制作需要一些比较高深的web动画技术,后来发现大多数loading都可以用“障眼法”做出来.比如一个旋转的圆圈,并不都是将gif图放进去,有些就是画个静止图像,然后让它旋转就 ...

  3. JavaScript中变量的类型

    变量的类型是指变量的值所属的数据类型,可以是数值型.字符串型和布尔型等,因为JavaScript是一种弱类型的程序语言,所以可以把任意类型的数据赋值给变量. 下面是一个关于变量类型的例子.在这个例子中 ...

  4. view在使用shape属性加圆角的同时,用代码修改其他background属性(例如颜色)不生效

    项目中一个TextView控件设置了shape属性,给其加了圆角,如下: houlder.mtxtGovernmentType.setBackgroundResource(R.drawable.tv_ ...

  5. MySQL NULL 值如何处理?

    我们已经知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作. 为了处理这种情况,MySQL提 ...

  6. BZOJ 4278: [ONTAK2015]Tasowanie 后缀数组 + 贪心 + 细节

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in", "r", stdin ...

  7. 【ownCloud】添加信任域

    如果在安装ownCloud后,更换了访问方式,比如刚开始是http://127.0.0.1/owncloud,变成了http://1.1.1.1/owncloud,那么在访问时可能得到这样的页面: 您 ...

  8. springboot框架嵌入netty

    1.pom.xml添加依赖 <dependency> <groupId>io.netty</groupId> <artifactId>netty-all ...

  9. JS控制全屏,监听退出全屏事件

    实现方案 //进入全屏 function requestFullScreen(de) { if(de.requestFullscreen){ //W3C de.requestFullscreen(); ...

  10. Codeforces 902D/901B - GCD of Polynomials

    传送门:http://codeforces.com/contest/902/problem/D 本题是一个数学问题——多项式整除. 对于两个整数a.b,求最大公约数gcd(a,b)的辗转相除法的函数如 ...