这就是个简单的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 ;
}

【HDOJ】1448 The Treasure的更多相关文章

  1. 【HDOJ】5446 Unknown Treasure

    1. 题目描述题目很简单,就是求$C(n,m) % M$. 2. 基本思路这是一道应用了众多初等数论定理的题目,因为数据范围较大因此使用Lucas求$C(n,m) % P$.而M较大,因此通过$a[i ...

  2. 【HDOJ】2416 Treasure of the Chimp Island

    bfs().题目的数据乱码.应该如下: *****#********* *.......$...* *..***.......* *....*****..* *....******37A *****. ...

  3. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  4. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  5. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  6. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  7. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  8. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  9. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

随机推荐

  1. WinForm多线程及委托防止界面假死

    当有大量数据需要计算.显示在界面或者调用sleep函数时,容易导致界面卡死,可以采用多线程加委托的方法解决. using System; using System.Collections.Generi ...

  2. eclipse 分屏

    刚刚一直找不到eclipse分屏功能,查了下发现是可以的. 具体见:http://www.coderanch.com/t/101996/vc/Split-screen-editor-Eclipse E ...

  3. window下安装composer和laravel

    安装composer: 1.在https://getcomposer.org/download/ 中下载 Composer-Setup.exe 2.安装composer步骤如下: 至此,compose ...

  4. 35 个必须有的Bootstrap工具和生成器

    Bootstraptor If you think that bootstrap templates are not enough for you, you should go with bootst ...

  5. DICOM开源库

    转载于 http://blog.csdn.net/jackmacro/article/details/5850142 Developers used to search for libraries , ...

  6. WebForm页面运行周期--页面关系

    1.前台文件类继承于后台文件类 2.当前台文件中包含某个标签runat= server的时候,asp.net就会在编译这个页面前后台文件类的时候,在后台类中添加一个相应的控件对象:当页面被访问,也就是 ...

  7. Kakfa揭秘 Day1 Kafka原理内幕

    Spark Streaming揭秘 Day32 Kafka原理内幕 今天开始,会有几天的时间,和大家研究下Kafka.在大数据处理体系中,kafka的重要性不亚于SparkStreaming.可以认为 ...

  8. 使用Flexbox实现CSS竖向居中

    竖向居中需要一个父元素和一个子元素合作完成. <div class="flexbox-container"> <div>Blah blah</div& ...

  9. js原生removeclass方法

    //如果列表中有存在给定的值就删除 // function removeClass(ele,txt){ // var str = ele.className, // ary = str.split(/ ...

  10. where, group by, having

    where vs having 当一个sql语句中存在where子句,会先执行where,然后执行group by,然后执行having. 一般来说,only use 'having' when yo ...