题目链接:【TopCoder 1570】DesertWind
题目大意: 的地图上有空地,障碍物和绿洲,每天有一个风向,在得知今天风向后你可以去周围 个格子,如果逆风行走需要耗费 点体力,否则只要耗费 点。求最坏情况下从起点走到绿洲耗费体力的最小值。
表示从 走到绿洲最坏情况下耗费体力的最小值。则 。用最短路更新即可。
// [TopCoder 1570] DesertWind
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 55;
const int inf = 0x3f3f3f3f;
struct DesertWind {
int n, m, sx, sy;
int dp[maxn][maxn];
char a[maxn][maxn];
bool valid(int x, int y) {
return 1 <= x && x <= n && 1 <= y && y <= m;
}
int daysNeeded(vector<string> vs) {
n = vs.size(), m = vs[0].size();
memset(dp, 0x3f, sizeof(dp));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = vs[i - 1][j - 1];
if (a[i][j] == '*') {
dp[i][j] = 0;
} else if (a[i][j] == '@') {
sx = i, sy = j;
}
}
}
int c, t[9];
for (int k = 1; k < n * m; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == 'X') {
continue;
}
c = 0;
memset(t, 0x3f, sizeof(t));
for (int ni = i - 1; ni <= i + 1; ni++) {
for (int nj = j - 1; nj <= j + 1; nj++) {
if (valid(ni, nj) && (i != ni || j != nj)) {
t[++c] = dp[ni][nj];
}
}
}
sort(t + 1, t + c + 1);
dp[i][j] = min(dp[i][j], min(t[1] + 3, t[2] + 1));
}
}
}
return dp[sx][sy] >= inf ? -1 : dp[sx][sy];
}
};
#ifndef ONLINE_JUDGE
DesertWind temp;
int n, m;
string s;
vector<string> vs;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> s;
vs.push_back(s);
}
cout << temp.daysNeeded(vs) << endl;
return 0;
}
#endif