您的位置:首页 > 其它

hdu 【2612】 Find a way

2016-04-15 16:14 363 查看

Find a way

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

Total Submission(s): 9068 Accepted Submission(s): 2908



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#


[align=left]Sample Output[/align]

66
88
66


[align=left]Author[/align]
yifenfei

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 205;
const int INF = 0x3f3f3f3f;
int n,m,sx,sy,ex,ey;

char maze[maxn][maxn];
int vis[maxn][maxn];
int time[maxn][maxn];
int dir[4][2] = {{1,0},{-1,0},{0,-1},{0,1}};

struct Node
{
int x,y,t;
}pre,cur;

void bfs(int x,int y)
{
memset(vis,0,sizeof(vis));
queue <Node> que;
while(!que.empty()) que.pop();
pre.x = x;
pre.y = y;
pre.t = 0;
vis[x][y] = 1;
que.push(pre);
while(!que.empty())
{
pre = que.front(); que.pop();
if(maze[pre.x][pre.y] == '@')
time[pre.x][pre.y] += pre.t;
for(int i = 0; i < 4; i++)
{
int xx = pre.x + dir[i][0];
int yy = pre.y + dir[i][1];
if(xx < 0 || xx >= n || yy < 0 || yy >= m)
continue;
if(!vis[xx][yy] && maze[xx][yy] != '#')
{
vis[xx][yy] = 1;
cur.x = xx;
cur.y = yy;
cur.t = pre.t+11;
que.push(cur);
}
}
}
}

int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
memset(time,0,sizeof(time));
for(int i = 0; i < n; i++)
scanf("%s", maze[i]);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(maze[i][j] == 'Y')
sx = i, sy = j;
else if(maze[i][j] == 'M')
ex = i, ey = j;
bfs(sx,sy);
bfs(ex,ey);
int Min = INF;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(time[i][j] && time[i][j] < Min)
Min = time[i][j];
printf("%d\n", Min);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: