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. 5.26 Quartz任务调度图解

  2. lua 10进制转换成其它进制table表示

    -- params@num integer -- ~) 默认为10 -- NOTE:先不输出符号 function NumberToArray(num, radix) if type(num) ~= ...

  3. 4 Things I Wish I Would Have Known When I Started My Software Development Career【当我最开始从事软件工程师的时候我希望我知道的四件事】

    英文原文:http://simpleprogrammer.com/2013/08/19/software-development-career/ My software development car ...

  4. java反射_01

    为什么要用反射? 举个栗子: package com.imooc.reflect; public class Work { // 定义一个word方法 public void word() { Sys ...

  5. Java-Class-Test:Test-1

    ylbtech-Java-Class-Test:Test-1 1.返回顶部 1.1. package com.ylbtech.api; import com.y;btech.WxApiApplicat ...

  6. Django 框架入门

    1.创建虚拟环境.(如果你想在你的服务器中运行多个项目,那么装虚拟环境是最好的选择) pip install virtualenv pip install virtualenvwrapper 安装好后 ...

  7. sql积累

    mysql 修改一列自增长 set @rownum=0; update a SET id = ( select @rownum := @rownum +1 as nid) WHERE id < ...

  8. SpringBoot背景

    1.SpringBoot的产生背景:随着spring日益发展,丰富的框架和组件大大简化了java项目开发过程,经过多年的实践,得到了很多人的认可,但是Spring的严重依赖XML配置文件,严重导致项目 ...

  9. 当样式中存在!important时无法使用show()或hide() 2017-06-11 22:25 15人阅读 评论(0) 收藏

    如果使用!important在你的样式中,比如display: none !important,此时就不能用show()了 但是我在查阅资料时发现有这样的解释, If using !important ...

  10. Python之scrapy linkextractors使用错误

    1.环境及版本 python3.7.1+scrapy1.5.1 2.问题及错误代码详情 优先贴上问题代码,如下: import scrapy from scrapy.linkextractors im ...