【HDOJ】1448 The Treasure

这就是个简单的bfs。真没什么好说的,三维的状态就可以了。每次预处理一下monster的位置,然后再恢复。

 /* 1924 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
bool agg;
int n;
int x[];
int y[];
} monster_t; typedef struct node_t {
int x, y; node_t() {}
node_t(int x, int y):
x(x), y(y) {} } node_t; const int maxn = ;
char s[maxn][maxn];
char ss[maxn][maxn];
monster_t mon[maxn];
bool visit[maxn][maxn][maxn];
int n, m, mn, mod;
int bx, by;
int ex, ey;
int dir[][] = {
-,, ,, ,-, ,,
-,-, ,, -,, ,-
}; bool judge(int x, int y) {
return x<= || x>n || y<= || y>m || s[x][y]=='#';
} bool judge2(int x, int y) {
return x<= || x>n || y<= || y>m || ss[x][y]=='#';
} void fill(int t) {
int idx, x, y; rep(i, , mn) {
idx = t % mon[i].n;
s[mon[i].x[idx]][mon[i].y[idx]] = '#';
if (mon[i].agg) {
rep(j, , ) {
x = mon[i].x[idx] + dir[j][];
y = mon[i].y[idx] + dir[j][];
if (judge2(x, y))
continue;
s[x][y] = '#';
}
}
}
} void restore(int t) {
int idx, x, y; rep(i, , mn) {
idx = t % mon[i].n;
s[mon[i].x[idx]][mon[i].y[idx]] = '.';
if (mon[i].agg) {
rep(j, , ) {
x = mon[i].x[idx] + dir[j][];
y = mon[i].y[idx] + dir[j][];
if (judge2(x, y))
continue;
s[x][y] = '.';
}
}
}
} int bfs() {
queue<node_t> Q;
node_t nd;
int x, y, t;
int ret = , sz; memset(visit, false, sizeof(visit));
visit[bx][by][] = ;
Q.push(node_t(bx, by)); while () {
sz = SZ(Q);
if (sz == )
break;
++ret; // set monster
t = ret%mod;
fill(t); while (sz--) {
nd = Q.front();
Q.pop(); if (s[nd.x][nd.y]=='#')
continue; // stay
if (!visit[nd.x][nd.y][t]) {
visit[nd.x][nd.y][t] = true;
Q.push(nd);
}
rep(i, , ) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
if (judge(x, y))
continue; if (x==ex && y==ey)
return ret; if (!visit[x][y][t]) {
visit[x][y][t] = true;
Q.push(node_t(x, y));
} x += dir[i][];
y += dir[i][];
if (judge(x, y))
continue; if (x==ex && y==ey)
return ret; if (!visit[x][y][t]) {
visit[x][y][t] = true;
Q.push(node_t(x, y));
}
}
} // restore monster
restore(t);
} return -;
} void solve() {
int ans = bfs();
if (ans == -)
puts("impossible");
else
printf("%d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t = ; while (scanf("%d %d", &n, &m)!=EOF && (n||m)) {
if (t++)
putchar('\n');
rep(i, , n+) {
scanf("%s", s[i]+);
rep(j, , m+) {
if (s[i][j] == 'p') {
bx = i;
by = j;
s[i][j] = '.';
} else if (s[i][j] == 't') {
ex = i;
ey = j;
s[i][j] = '.';
}
}
}
memcpy(ss, s, sizeof(ss));
scanf("%d", &mn);
mod = ;
rep(i, , mn) {
scanf("%d", &mon[i].n);
mod = max(mod, mon[i].n);
rep(j, , mon[i].n)
scanf("%d %d", &mon[i].x[j], &mon[i].y[j]);
mon[i].agg = s[mon[i].x[]][mon[i].y[]]=='a';
s[mon[i].x[]][mon[i].y[]] = '.';
} solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
上一篇:Linux中的grep命令


下一篇:awt