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. jQuery学习笔记之插件开发(4)

    jQuery学习笔记之插件开发(4) github源码地址 插件:了让原有功能的增强. 1.插件的种类(3种):局部.全局.选择器插件 1.1封装对象方法的插件 这种类型的插件是把一些常用或者重复使用 ...

  2. 压缩映射:简单最邻近搜索-(SLH)Simple Linear Hash

    Compact Projection: Simple and Efficient Near Neighbor Search with Practical memory Requirement Auto ...

  3. Maven常见异常及解决方法

    异常1: [ERROR] Failed to execute goal on project biz_zhuhai: Could not resolve dependencies for projec ...

  4. Qt中采用多线程实现Socket编程

    Socket通常也称作"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求. 本文介绍的是Qt中采用多线程Socket编程,由于工作的需要,开始 ...

  5. 基于日志实现ssh服务防护脚本

    grep -n "Failed password" secure | sed -nr 's/.*from(.*)port.*/\1/gp' | sort -n |uniq -c|s ...

  6. 【剑指Offer】22、从上往下打印二叉树

      题目描述:   从上往下打印出二叉树的每个节点,同层节点从左至右打印.   解题思路:   本题实际上就是二叉树的层次遍历,深度遍历可以用递归或者栈,而层次遍历很明显应该使用队列.同样我们可以通过 ...

  7. 【Shell编程】Shell基本语法

    Shell 语法   Shell程序设计作为一种脚本语言,在Linux系统中有广泛的应用,本文记录了关于Shell程序设计的基础语法知识和常用命令,方便查询,熟练使用shell也需要经常实践,这对于完 ...

  8. Oracle笔记 多表查询

    Oracle笔记  多表查询   本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: 3. 子查询的操作,并且结合限定查询.数据排序.多表查询.统计查 ...

  9. python 实现kmeans聚类

    编程中在做数值相等判断的时候,直接使用==判断并不可靠.实际上经过运算后的两个值(浮点型)并不可能完全一致,可能会因为小数点后的些许差异导致判断为false. 比如: 1 print 1e-5 == ...

  10. mysql优化 explain index

    本文章属于转载,尊重原创:http://www.2cto.com/database/201501/369135.html 实验环境: 1.sql工具:Navicat 2.sql数据库,使用openst ...