【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 }
时间: 2024-10-25 11:52:18

【HDOJ】1448 The Treasure的相关文章

【HDOJ】5446 Unknown Treasure

1. 题目描述题目很简单,就是求$C(n,m) % M$. 2. 基本思路这是一道应用了众多初等数论定理的题目,因为数据范围较大因此使用Lucas求$C(n,m) % P$.而M较大,因此通过$a[i] = C(n,m)%P_i$再综合中国剩余定理可解.由于数据可能为$10^{18}$,再进行乘法可能超long long.因此,还需要模拟乘法. 3. 代码 1 /* 5446 */ 2 #include <iostream> 3 #include <sstream> 4 #incl

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!

状态压缩+优先级bfs. 1 /* 1818 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXM 105 11 12 typedef struct {

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t

【HDOJ】2425 Hiking Trip

优先级队列+BFS. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 25 8 9 typedef struct node_st { 10 int x, y, t; 11 node_st() {} 12 node_st(int xx, int yy, int tt)

【HDOJ】1686 Oulipo

kmp算法. 1 #include <cstdio> 2 #include <cstring> 3 4 char src[10005], des[1000005]; 5 int next[10005], total; 6 7 void kmp(char des[], char src[]){ 8 int ld = strlen(des); 9 int ls = strlen(src); 10 int i, j; 11 12 total = i = j = 0; 13 while (