本题思路还是很简单的,只要方法用对。
先用结构体把KFC的坐标存好,然后用一个二维数组将两人的时间花费和加到该点,最后遍历每个KFC点求最小值。
ps:今天周五,明后两天可是开学的周末,大好时光啊!可是今天晚上做题欲望突然变特差,看了好多不该看的东西。吾日三省吾身,一个人好好想想自己是谁,控制好自己的时间!
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int N = 209;
const int INF = 1000000;
char Map[N][N];
bool vis[N][N];
int a[N][N];
int m, n;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct node
{
int x, y;
int step;
}f[5000];
node f1, f2;
queue <node> q;
bool check(int x, int y)
{
if(x >= 0 && x < n && y >= 0 && y < m && Map[x][y] != '#' && !vis[x][y]) return true;
else return false;
}
void bfs()
{
while(!q.empty())
{
node tmp = q.front();
q.pop();
if(Map[tmp.x][tmp.y] == '@')
{
a[tmp.x][tmp.y] += tmp.step; //两次bfs和存储至该数组,便于最后比较
}
for(int i = 0; i < 4; i ++)
{
node tmp2;
tmp2 = tmp;
tmp2.x += dir[i][0];
tmp2.y += dir[i][1];
tmp2.step += 1;
if(check(tmp2.x, tmp2.y))
{
vis[tmp2.x][tmp2.y] = 1;
q.push(tmp2);
}
}
}
}
void Solve()
{
memset(a, 0, sizeof(a));
memset(vis, 0, sizeof(vis));
while(!q.empty()) q.pop();
vis[f1.x][f1.y] = 1;
q.push(f1);
bfs();
memset(vis, 0, sizeof(vis));
while(!q.empty()) q.pop();
vis[f2.x][f2.y] = 1;
q.push(f2);
bfs();
}
int main()
{
int num, minn;
// freopen("in.txt", "r", stdin);
while(~scanf("%d%d", &n, &m))
{
num = 0;
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
{
cin >> Map[i][j];
if(Map[i][j] == 'Y')
{
f1.x = i;
f1.y = j;
f1.step = 0;
}
if(Map[i][j] == 'M')
{
f2.x = i;
f2.y = j;
f2.step = 0;
}
if(Map[i][j] == '@')
{
f[num].x = i;
f[num].y = j;
num ++;
}
}
Solve();
minn = INF;
for(int i = 0; i < num; i ++)
{
if(a[f[i].x][f[i].y] != 0)
minn = min(minn, a[f[i].x][f[i].y]);
}
printf("%d\n", minn * 11);
}
return 0;
}