您的位置:首页 > 其它

Find a way(HDU-2612)

2017-11-01 23:30 211 查看


Find a way

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

Total Submission(s): 17685    Accepted Submission(s): 5707


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

 
题目大意:
给你一张M x N的矩阵,里边Y是小y的位置,M是小m的位置,@是肯德基的位置,.是可以经过的道路的位置,#是不可以经过的墙的位置,两个人相约去肯德基见面,两人走一格都消耗11分钟。。。找一个最近的肯德基,使得两人所用的时间总和(T1+T2)最小。
解题思路:
map[ ] [ ] 数组存下地图。
way[ ] [ ] [ 2 ] 三维数组存下地图上每个点距离Y和M的距离(用广搜找到)。
注意事项:
没到过的点可以用一个非常大的数代替,到过之后用对应的步数代替证明来过,判断最小距离的时候应该注意,如果有一个肯德基两人都到不了(比如被墙围死),两人的距离和会溢出为负数,造成WA。
代码如下:
import java.util.*;
public class Main
{
public static Scanner in = new Scanner(System.in);
public static final int MAX = Integer.MAX_VALUE/2;
public static int hang,lie,startx,starty,answer = 0,flag;
public static int[][][] way = new int[201][201][2];
public static char[][] map = new char[201][201];
public static int x_go[] = {0,0,1,-1};
public static int y_go[] = {1,-1,0,0};
public static void main(String[] args)
{
while(in.hasNext())
{
inint();
check();
}
System.exit(0);
}
public static void check()
{
for(int i=0 ; i<hang ; i++)
{
for(int j=0 ; j<lie ; j++)
{
if(map[i][j]=='Y')
{
flag = 0;
bfs(i,j);
}
if(map[i][j]=='M')
{
flag = 1;
bfs(i,j);
}
}
}
int answer = MAX;
for(int i=0 ; i<hang ; i++)
{
for(int j=0 ; j<lie ; j++)
{
if(map[i][j]=='@')
{
answer = Math.min(answer, way[i][j][0]+way[i][j][1]);
}
}
}
System.out.println(answer*11);
}
public static void bfs(int x,int y)
{
Queue<room> q = new LinkedList();
q.add(new room(x,y,0));
while(!q.isEmpty())
{
room r = q.remove();
for(int i=0 ; i<4 ; i++)
{
int temx = r.x + x_go[i];
int temy = r.y + y_go[i];
if(temx<0||temx==hang)continue;
if(temy<0||temy==lie)continue;
if(map[temx][temy]=='#')continue;
if(way[temx][temy][flag]<MAX)continue;
way[temx][temy][flag] = r.type+1;
q.add(new room(temx,temy,r.type+1));
}
}
q.clear();
}
public static boolean inint()
{
if(!in.hasNext())return false;
hang = in.nextInt();
lie = in.nextInt();
String str;
for(int i=0 ; i<hang ; i++)
{
str = in.next();
for(int j=0 ; j<lie ; j++)
{
map[i][j] = str.charAt(j);
way[i][j][0] = MAX;
way[i][j][1] = MAX;
}
}
return true;
}
}
class room
{
int x,y,type;
public room(int x,int y,int type)
{
this.x = x;
this.y = y;
this.type = type;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: