熟练一下搜索问题…
POJ 3669
两种思路,一种是比较直接的想法,先把所有的袭击事件按时间排序,在BFS的过程中处理袭击事件;另一种是先预处理,再BFS。
写第一种时遇到的问题:在BFS中循环的结束条件是que.empty() || k >= M
,若在队列为空时结束,则可能还有后续的袭击会摧毁之前能够到达的地方,因此最后需要处理完所有的袭击。
排序后BFS
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAX_X = 300 + 10;
const int MAX_Y = MAX_X;
const int MAX_M = 50000 + 10;
struct Point { int x, y; };
struct Event { int x, y, t; };
int M;
int field[MAX_X][MAX_Y];
// -1 never gone
// -2 destroyed
// >= 0 min time you can reach
Event A[MAX_M];
bool comp(const Event& e1, const Event& e2) {
return e1.t < e2.t;
}
int nextX[4] = {0, 1, 0, -1}, nextY[4] = {1, 0, -1, 0};
bool inside(int x, int y) {
return x >= 0 && x < MAX_X && y >= 0 && y < MAX_Y;
}
void strike(int x, int y) {
field[x][y] = -2;
for (int i = 0; i < 4; i++) {
int nx = x + nextX[i], ny = y + nextY[i];
if (inside(nx, ny)) {
field[nx][ny] = -2;
}
}
}
void solve() {
memset(field, -1, sizeof(field));
queue<Point> que;
int k = 0, t = 0;
// k points to the destroy event that
// we are gonna process
// process the strike of 0 time
field[0][0] = 0;
while (k < M && A[k].t == t) {
strike(A[k].x, A[k].y);
k++;
}
if (field[0][0] == 0) que.push((Point){0, 0});
while (!que.empty() && k < M) {
t++;
while (k < M && A[k].t == t) {
strike(A[k].x, A[k].y);
k++;
}
int size = que.size();
while (size--) {
Point p = que.front(); que.pop();
for (int i = 0; i < 4; i++) {
int nx = p.x + nextX[i], ny = p.y + nextY[i];
if (inside(nx, ny) && field[nx][ny] == -1) {
field[nx][ny] = t;
que.push((Point){nx, ny});
}
}
}
}
// forget here !!! process all the strikes
// if que.empty(), then there may be some strikes
// that would destroy the places you can go
while (k < M) {
strike(A[k].x, A[k].y);
k++;
}
int res = -1;
for (int i = 0; i < MAX_X; i++) {
for (int j = 0; j < MAX_Y; j++) {
if (field[i][j] >= 0 && (res == -1 || field[i][j] < res)) {
res = field[i][j];
}
}
}
printf("%d\n", res);
}
int main() {
scanf("%d", &M);
for (int i = 0; i < M; i++) {
scanf("%d %d %d", &A[i].x, &A[i].y, &A[i].t);
}
sort(A, A + M, comp);
solve();
return 0;
}
预处理后BFS
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAX_X = 300 + 10;
const int MAX_Y = MAX_X;
struct Point { int x, y, t; };
int M;
int field[MAX_X][MAX_Y];
bool used[MAX_X][MAX_Y];
// -1 never gone
// -2 destroyed
int nextX[4] = {0, 1, 0, -1}, nextY[4] = {1, 0, -1, 0};
int bfs() {
if (field[0][0] == 0) return -1;
if (field[0][0] == -1) return 0;
queue<Point> que;
que.push((Point){0, 0, 0});
used[0][0] = true;
while (!que.empty()) {
Point p = que.front(); que.pop();
for (int i = 0; i < 4; i++) {
int nx = p.x + nextX[i], ny = p.y + nextY[i];
if (nx >= 0 && nx < MAX_X && ny >= 0 && ny < MAX_Y) {
if (field[nx][ny] == -1) return p.t + 1;
if (field[nx][ny] > p.t + 1 && !used[nx][ny]) {
que.push((Point){nx, ny, p.t + 1});
used[nx][ny] = true;
}
}
}
}
return -1;
}
int main() {
// freopen("in.txt", "r", stdin);
memset(field, -1, sizeof(field));
memset(used, 0, sizeof(used));
scanf("%d", &M);
int x, y, t;
for (int i = 0; i < M; i++) {
scanf("%d %d %d", &x, &y, &t);
field[x][y] = field[x][y] == -1 ? t : min(field[x][y], t);
for (int j = 0; j < 4; j++) {
int nx = x + nextX[j], ny = y + nextY[j];
if (nx >= 0 && nx < MAX_X && ny >= 0 && ny < MAX_Y) {
field[nx][ny] = field[nx][ny] == -1 ? t : min(field[nx][ny], t);
}
}
}
printf("%d\n", bfs());
return 0;
}
POJ 3009 DFS
#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
const int MAX_N = 20 + 3;
int W, H;
int field[MAX_N][MAX_N];
int si, sj, ti, tj;
int nextX[4] = {0, 1, 0, -1}, nextY[4] = {1, 0, -1, 0};
bool inside(int x, int y) {
if (x >= 0 && x < H && y >= 0 && y < W) {
return true;
} else {
return false;
}
}
int dfs(int x, int y, int s) {
if (s >= 10) {
return -1;
}
int step = -1;
for (int i = 0; i < 4; i++) {
int nx = x, ny = y, tx = nx + nextX[i], ty = ny + nextY[i];
while (inside(tx, ty) && field[tx][ty] != 1) {
nx = tx;
ny = ty;
tx = nx + nextX[i];
ty = ny + nextY[i];
if (nx == ti && ny == tj) break;
}
if (nx == ti && ny == tj) {
step = 1;
continue;
}
if ((nx != x || ny != y) && inside(tx, ty) && field[tx][ty] == 1) {
field[tx][ty] = 0;
int d = dfs(nx, ny, s + 1);
if (d != -1 && (step == -1 || d + 1 < step)) step = d + 1;
field[tx][ty] = 1;
}
}
return step;
}
int main() {
while (scanf("%d %d", &W, &H) != EOF && (W || H)) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
scanf("%d", &field[i][j]);
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (field[i][j] == 2) {
si = i;
sj = j;
} else if (field[i][j] == 3) {
ti = i;
tj = j;
}
}
}
printf("%d\n", dfs(si, sj, 0));
}
return 0;
}