您的位置:首页 > 其它

HDU2612 find a way

2016-01-29 21:01 316 查看
题目链接:Find a way

bfs水题。

/*
只有两个人啊。分别以两个人为起点bfs,计算出每个人到每个KFC 的时间。两个人都能到达的KFC的较大时间的最小值、就是ans。好水。T_T
*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <queue>
#define maxn 1000000
using namespace std;

char mp[210][210];
int vis[210][210];
int step[210][210], step1[210][210], step2[210][210];
int n, m;

struct Node{
int x, y;
}now, temp, a, b;

queue<Node>que;

int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

bool check(Node a) {
if (a.x >= 0 && a.x < n && a.y >= 0 && a.y < m && !vis[temp.x][temp.y] && mp[temp.x][temp.y] != '#')
return true;
return false;
}

void bfs1(Node t) {
while(!que.empty()) {
que.pop();
}
memset(vis, 0, sizeof(vis));
que.push(t);
vis[t.x][t.y] = 1;
step1[t.x][t.y] = 0;

while(!que.empty()) {
now = que.front();
que.pop();
for (int i=0; i<4; ++i) {
temp.x = now.x + dir[i][0];
temp.y = now.y + dir[i][1];
if (check(temp)) {
vis[temp.x][temp.y] = 1;
step1[temp.x][temp.y] = step1[now.x][now.y] + 1;
que.push(temp);
}
}
}
}

void bfs2(Node t) {
while(!que.empty()) {
que.pop();
}
memset(vis, 0, sizeof(vis));
que.push(t);
vis[t.x][t.y] = 1;
step2[t.x][t.y] = 0;

while(!que.empty()) {
now = que.front();
que.pop();
for (int i=0; i<4; ++i) {
temp.x = now.x + dir[i][0];
temp.y = now.y + dir[i][1];
if (check(temp)) {
vis[temp.x][temp.y] = 1;
step2[temp.x][temp.y] = step2[now.x][now.y] + 1;
que.push(temp);
}
}
}
}

int main() {
while(cin >> n >> m) {
memset(vis, 0, sizeof(vis));
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
step[i][j] = maxn;
step1[i][j] = maxn;
step2[i][j] = maxn;
}
}
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
cin >> mp[i][j];
if (mp[i][j] == 'M') {
a.x = i, a.y = j;
}
else if (mp[i][j] == 'Y') {
b.x = i, b.y = j;
}
}
}

bfs1(a);
bfs2(b);
int ans = maxn;
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
if (mp[i][j] == '@') {
step[i][j] = step1[i][j] + step2[i][j];
}
ans = min(ans, step[i][j]);
}
}
cout << ans*11 << endl;
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: