您的位置:首页 > 其它

HDU - 2612 Find a way

2017-07-31 08:53 411 查看
CCPC网络赛开始报名了~

欢迎参加——阿里云“智慧航空AI大赛”(报名中...)


Find a way

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

Total Submission(s): 16650    Accepted Submission(s): 5343


[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
 

[align=left]Source[/align]
奋斗的年代

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f

using namespace std;

typedef struct
{
int x, y;
int step;
}node;

int s[250][250];
int Y[250][250], M[250][250];
char map[250][250];
int n, m, sum;

void bfs(int x1, int y1, int x2, int y2)
{
node a, b, c, d;
queue<node>q;
queue<node>p;
a.x = x1;
a.y = y1;
a.step = 0;
Y[x1][y1] = 1;
q.push(a);
c.x = x2;
c.y = y2;
c.step = 0;
M[x2][y2] = 1;
p.push(c);
while(!q.empty() || !p.empty())
{
if(!q.empty())
{
a = q.front();
q.pop();
if(map[a.x][a.y] == '@')
{
if(s[a.x][a.y])
sum = min(sum,s[a.x][a.y] + a.step * 11);
else
s[a.x][a.y] = a.step * 11;
}
if(a.x + 1 < n && map[a.x+1][a.y] != '#' && !Y[a.x+1][a.y])
{
b.x = a.x+1;
b.y = a.y;
b.step = a.step+1;
Y[b.x][b.y] = 1;
q.push(b);
}
if(a.x - 1 >= 0 && map[a.x-1][a.y] != '#' && !Y[a.x-1][a.y])
{
b.x = a.x - 1;
b.y = a.y;
b.step = a.step + 1;
Y[b.x][b.y] = 1;
q.push(b);
}
if(a.y + 1 < m && map[a.x][a.y+1] != '#' && !Y[a.x][a.y+1])
{
b.x = a.x;
b.y = a.y + 1;
b.step = a.step + 1;
Y[b.x][b.y] = 1;
q.push(b);
}
if(a.y - 1 >= 0 && map[a.x][a.y-1] != '#' && !Y[a.x][a.y-1])
{
b.x = a.x;
b.y = a.y - 1;
b.step = a.step + 1;
Y[b.x][b.y] = 1;
q.push(b);
}
}
if(!p.empty())
{

4000
c = p.front();
p.pop();
if(map[c.x][c.y] == '@')
{
if(s[c.x][c.y])
sum = min(sum,s[c.x][c.y] + c.step * 11);
else
s[c.x][c.y] = c.step * 11;
}
if(c.x + 1 < n && !M[c.x+1][c.y] && map[c.x+1][c.y] != '#')
{
d.x = c.x + 1;
d.y = c.y;
d.step = c.step + 1;
M[d.x][d.y] = 1;
p.push(d);
}
if(c.x - 1 >= 0 && map[c.x-1][c.y] != '#' && !M[c.x-1][c.y])
{
d.x = c.x - 1;
d.y = c.y;
d.step = c.step + 1;
M[d.x][d.y] = 1;
p.push(d);
}
if(c.y + 1 < m && map[c.x][c.y+1] != '#' && !M[c.x][c.y+1])
{
d.x = c.x;
d.y = c.y + 1;
d.step = c.step + 1;
M[d.x][d.y] = 1;
p.push(d);
}
if(c.y - 1 >= 0 && map[c.x][c.y-1] != '#' && !M[c.x][c.y-1])
{
d.x = c.x;
d.y = c.y - 1;
d.step = c.step + 1;
M[d.x][d.y] = 1;
p.push(d);
}
}
}
cout<<sum<<endl;
}

int main()
{
int x1, y1, x2, y2, i, j;
while(cin>>n>>m)
{
sum = inf;
for(i = 0;i < n;i++)
{
for(j = 0;j < m;j++)
{
cin>>map[i][j];
Y[i][j] = M[i][j] = s[i][j] = 0;
if(map[i][j] == 'Y')
{
x1 = i;
y1 = j;
}
if(map[i][j] == 'M')
{
x2 = i;
y2 = j;
}
}
}
bfs(x1,y1,x2,y2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: