【HDOJ】1448 The Treasure
这就是个简单的bfs。真没什么好说的,三维的状态就可以了。每次预处理一下monster的位置,然后再恢复。
1 /* 1924 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #include <bitset> 12 #include <algorithm> 13 #include <cstdio> 14 #include <cmath> 15 #include <ctime> 16 #include <cstring> 17 #include <climits> 18 #include <cctype> 19 #include <cassert> 20 #include <functional> 21 #include <iterator> 22 #include <iomanip> 23 using namespace std; 24 //#pragma comment(linker,"/STACK:102400000,1024000") 25 26 #define sti set<int> 27 #define stpii set<pair<int, int> > 28 #define mpii map<int,int> 29 #define vi vector<int> 30 #define pii pair<int,int> 31 #define vpii vector<pair<int,int> > 32 #define rep(i, a, n) for (int i=a;i<n;++i) 33 #define per(i, a, n) for (int i=n-1;i>=a;--i) 34 #define clr clear 35 #define pb push_back 36 #define mp make_pair 37 #define fir first 38 #define sec second 39 #define all(x) (x).begin(),(x).end() 40 #define SZ(x) ((int)(x).size()) 41 #define lson l, mid, rt<<1 42 #define rson mid+1, r, rt<<1|1 43 44 typedef struct { 45 bool agg; 46 int n; 47 int x[105]; 48 int y[105]; 49 } monster_t; 50 51 typedef struct node_t { 52 int x, y; 53 54 node_t() {} 55 node_t(int x, int y): 56 x(x), y(y) {} 57 58 } node_t; 59 60 const int maxn = 105; 61 char s[maxn][maxn]; 62 char ss[maxn][maxn]; 63 monster_t mon[maxn]; 64 bool visit[maxn][maxn][maxn]; 65 int n, m, mn, mod; 66 int bx, by; 67 int ex, ey; 68 int dir[8][2] = { 69 -1,0, 1,0, 0,-1, 0,1, 70 -1,-1, 1,1, -1,1, 1,-1 71 }; 72 73 bool judge(int x, int y) { 74 return x<=0 || x>n || y<=0 || y>m || s[x][y]=='#'; 75 } 76 77 bool judge2(int x, int y) { 78 return x<=0 || x>n || y<=0 || y>m || ss[x][y]=='#'; 79 } 80 81 void fill(int t) { 82 int idx, x, y; 83 84 rep(i, 0, mn) { 85 idx = t % mon[i].n; 86 s[mon[i].x[idx]][mon[i].y[idx]] = '#'; 87 if (mon[i].agg) { 88 rep(j, 0, 8) { 89 x = mon[i].x[idx] + dir[j][0]; 90 y = mon[i].y[idx] + dir[j][1]; 91 if (judge2(x, y)) 92 continue; 93 s[x][y] = '#'; 94 } 95 } 96 } 97 } 98 99 void restore(int t) { 100 int idx, x, y; 101 102 rep(i, 0, mn) { 103 idx = t % mon[i].n; 104 s[mon[i].x[idx]][mon[i].y[idx]] = '.'; 105 if (mon[i].agg) { 106 rep(j, 0, 8) { 107 x = mon[i].x[idx] + dir[j][0]; 108 y = mon[i].y[idx] + dir[j][1]; 109 if (judge2(x, y)) 110 continue; 111 s[x][y] = '.'; 112 } 113 } 114 } 115 } 116 117 int bfs() { 118 queue<node_t> Q; 119 node_t nd; 120 int x, y, t; 121 int ret = 0, sz; 122 123 memset(visit, false, sizeof(visit)); 124 visit[bx][by][0] = 0; 125 Q.push(node_t(bx, by)); 126 127 while (1) { 128 sz = SZ(Q); 129 if (sz == 0) 130 break; 131 ++ret; 132 133 // set monster 134 t = ret%mod; 135 fill(t); 136 137 while (sz--) { 138 nd = Q.front(); 139 Q.pop(); 140 141 if (s[nd.x][nd.y]=='#') 142 continue; 143 144 // stay 145 if (!visit[nd.x][nd.y][t]) { 146 visit[nd.x][nd.y][t] = true; 147 Q.push(nd); 148 } 149 rep(i, 0, 8) { 150 x = nd.x + dir[i][0]; 151 y = nd.y + dir[i][1]; 152 if (judge(x, y)) 153 continue; 154 155 if (x==ex && y==ey) 156 return ret; 157 158 if (!visit[x][y][t]) { 159 visit[x][y][t] = true; 160 Q.push(node_t(x, y)); 161 } 162 163 x += dir[i][0]; 164 y += dir[i][1]; 165 if (judge(x, y)) 166 continue; 167 168 if (x==ex && y==ey) 169 return ret; 170 171 if (!visit[x][y][t]) { 172 visit[x][y][t] = true; 173 Q.push(node_t(x, y)); 174 } 175 } 176 } 177 178 // restore monster 179 restore(t); 180 } 181 182 return -1; 183 } 184 185 void solve() { 186 int ans = bfs(); 187 if (ans == -1) 188 puts("impossible"); 189 else 190 printf("%d\n", ans); 191 } 192 193 int main() { 194 ios::sync_with_stdio(false); 195 #ifndef ONLINE_JUDGE 196 freopen("data.in", "r", stdin); 197 freopen("data.out", "w", stdout); 198 #endif 199 200 int t = 0; 201 202 while (scanf("%d %d", &n, &m)!=EOF && (n||m)) { 203 if (t++) 204 putchar('\n'); 205 rep(i, 1, n+1) { 206 scanf("%s", s[i]+1); 207 rep(j, 1, m+1) { 208 if (s[i][j] == 'p') { 209 bx = i; 210 by = j; 211 s[i][j] = '.'; 212 } else if (s[i][j] == 't') { 213 ex = i; 214 ey = j; 215 s[i][j] = '.'; 216 } 217 } 218 } 219 memcpy(ss, s, sizeof(ss)); 220 scanf("%d", &mn); 221 mod = 1; 222 rep(i, 0, mn) { 223 scanf("%d", &mon[i].n); 224 mod = max(mod, mon[i].n); 225 rep(j, 0, mon[i].n) 226 scanf("%d %d", &mon[i].x[j], &mon[i].y[j]); 227 mon[i].agg = s[mon[i].x[0]][mon[i].y[0]]=='a'; 228 s[mon[i].x[0]][mon[i].y[0]] = '.'; 229 } 230 231 solve(); 232 } 233 234 #ifndef ONLINE_JUDGE 235 printf("time = %d.\n", (int)clock()); 236 #endif 237 238 return 0; 239 }