您的位置:首页 > 其它

HDU 2612.Find a way

2017-12-21 13:01 288 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2612

AC代码(C++):

#include <iostream>
#include <queue>
#include <algorithm>

#define INF 0x3f3f3f3f

using namespace std;

struct NODE {
short x, y, step;
};

int n, m;
char map[205][205];
bool vis[205][205];
int dist1[205][205];
int dist2[205][205];
int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };

void bfs(NODE s, int dist[][205]) {
queue<NODE> q;
q.push(s);
NODE node, tmp;
while (!q.empty()) {
node = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
tmp.x = node.x + dir[i][0];
tmp.y = node.y + dir[i][1];
tmp.step = node.step + 1;
if (map[tmp.x][tmp.y] != '#'&&!vis[tmp.x][tmp.y]) {
vis[tmp.x][tmp.y] = true;
dist[tmp.x][tmp.y] = tmp.step;
q.push(tmp);
}
}
}
}

int main() {
while (cin >> n >> m) {
NODE s1, s2;
s1.step = 0;
s2.step = 0;
for (int i = 0; i <= n + 1; i++)for (int j = 0; j <= m + 1; j++)map[i][j] = '#';
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> map[i][j];
if (map[i][j] == 'Y') {
s1.x = i;
s1.y = j;
}
else if (map[i][j] == 'M') {
s2.x = i;
s2.y = j;
}
}
}
memset(vis, false, sizeof(vis));
vis[s1.x][s1.y] = true;
bfs(s1, dist1);
memset(vis, false, sizeof(vis));
vis[s2.x][s2.y] = true;
bfs(s2, dist2);
int MIN = INF;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (map[i][j] == '@')MIN = min(MIN, dist1[i][j] + dist2[i][j]);
}
}
cout << MIN * 11 << endl;
}

//system("pause");
}

总结: 广搜, 先搜出Y和M到所有点的距离, 然后比较哪个kfc的距离最小.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: