# Find a way HDU - 2612

vj网址点击打开链接

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <queue>
using namespace std;
char p[300][300];
struct ppx
{
int x, y, z;
int k[5] = { -1, 0, 0, 1};
int kk[5] = {0, 1, -1, 0};
int ass[300][300];
bool bj[300][300];
int n, m, maxx;
void bfs()
{
memset(bj, 0, sizeof(bj));
queue<ppx>q;
bj[as.x][as.y]=1;
q.push(as);
while(!q.empty())
{
af = q.front();
q.pop();
//printf("%d %d\n",af.x,af.y);
if(p[af.x][af.y] == '@')
{
//  printf("%c\n",p[af.x][af.y]);
//  printf("%d %d    0 \n",af.x,af.y);
ass[af.x][af.y] = af.z;
}
for(int i = 0; i < 4; i++)
{
ag = af;
ag.x = af.x + k[i];
ag.y = af.y + kk[i];
if(ag.x >= 0 && ag.x < n && ag.y >= 0 && ag.y < m && p[ag.x][ag.y] != '#' && !bj[ag.x][ag.y])
{
bj[ag.x][ag.y] = 1;
ag.z = af.z + 1;
q.push(ag);
}
}
}
memset(bj, 0, sizeof(bj));
queue<ppx>qq;
while(!qq.empty())
{
af = qq.front();
qq.pop();
if(p[af.x][af.y] == '@')
{
}
for(int i = 0; i < 4; i++)
{
ag = af;
ag.x = af.x + k[i];
ag.y = af.y + kk[i];
if(ag.x >= 0 && ag.x < n && ag.y >= 0 && ag.y < m && p[ag.x][ag.y] != '#' && ! bj[ag.x][ag.y])
{
bj[ag.x][ag.y] = 1;
ag.z = af.z + 1;
qq.push(ag);
}
}
}
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
maxx =1000000+1 ;
memset(bj, 0, sizeof(bj));
for(int i = 0; i < n; i++)
{
scanf("%s", p+i);
for(int j = 0; j < m; j++)
{
ass[i][j]=1000000;
if(p[i][j] == 'Y')
{
as.x = i, as.y = j, as.z = 0;
}
if(p[i][j] == 'M')
{
}
}
}
bfs();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(ass[i][j] == 1000000|| add[i][j] == 1000000)
continue;
maxx = min(maxx, ass[i][j] + add[i][j]);
}
}
printf("%d\n", maxx*11);
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120