这就是个简单的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. IAR:Error [Li005]:no definition for"***" 问题之连接

    对于 IAR 出现的 Error[Li005] 链接错误,网上已经给出了比较详尽的解决方法,而对于这次记录,主要是记录解决问题的思路. 网上给出的方法:http://blog.csdn.net/yue ...

  2. 51nod1417 天堂里的游戏

    ---恢复内容开始--- 1417 天堂里的游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 多年后,每当Noder看到吉普赛人,就会想起那个遥 ...

  3. google其他入口地址

    http://178.45.251.74/ http://64.233.166.51/ http://61.19.1.117/ http://64.233.166.42/ http://61.19.1 ...

  4. if...else..的错误用法

    1.最近在写js代码完成一个前段DOM操作的函数时,自己错误的使用了if..else..控制体.为什么是错误的呢?看看我的 代码你就明白了: document.getElementsByClassNa ...

  5. Fedora 命令

    1. 更新包 yum clear all yum -y update 2.yum包查找 yum whatprovides xxxx.os.l 3 df 查看磁盘空间 xclip 复制到粘贴板 xcli ...

  6. ems lite 客户端远程连接mysql server

    在本地用ems客户端远程连接虚拟机上的mysql server,弹出客户端没有权限访问mysql server.使用下面方法进行设置:mysql> select host,user,passwo ...

  7. sharepoint One-Time Passwords (windows basic authentication)

    //设计中,未完成 references: http://www.asp.net/web-api/overview/security/basic-authentication http://techn ...

  8. 使用Yeoman搭建 AngularJS 应用 (7) —— 让我们搭建一个网页应用

    原文地址:http://yeoman.io/codelab/preview-inbrowser.html 开启你的服务 运行Grunt任务,通过输入下面的命令来创建一个本地Node的http服务,地址 ...

  9. [转]BluetoothDevice.getType()-一个常常被忽略了的函数。好用的不要不要的

    自动安卓发布了4.0版本的蓝牙协议之后,越来越多的开发者收到了各种针对于BLE蓝牙的开发需求. 而且有很多时候还需要兼容以前的3.0版本,给大家的开发带来了困扰,笔者也遇到了这样的问题,偶然间发现了g ...

  10. EL表达式中如何截取字符串

    EL表达式中如何截取字符串 可以截取,用fn函数:<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/ ...