【HDOJ】1448 The Treasure

时间:2022-09-26 22:09:58

这就是个简单的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. Druid监控Mybatis不显示SQL问题

    一.Web.xml增加如下配置: 1.DruidWebStatFilter.如果没有配置filter信息.session监控,web监控等不可用.没有配置 <filter> <fil ...

  2. light oj 1205 - Palindromic Numbers 数位DP

    思路:搜索的时候是从高位到低位,所以一旦遇到非0数字,也就确定了数的长度,这样就知道回文串的中心点. 代码如下: #include<iostream> #include<cstdio ...

  3. 关于vs2008使用oracleclient链接oracle数据库报报错OCIEnvCreate 失败,返回代码为 -1,但错误消息文本不可用

    用vs2008链接oracle数据库出现问题,报错OCIEnvCreate 失败,返回代码为 -1,但错误消息文本不可用,从网上找了好久方法,有两种oracle客户端文件权限,和运行vs2008以管理 ...

  4. 聊聊dmClock算法

    作者:吴香伟 发表于 2017/01/08 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 人们常常容易忽略一些不起眼但特别重要的事物.曾经跟同事聊Python, ...

  5. &OpenCurlyDoubleQuote;放到桌面”的Servlet实现

    复习下Servlet下载文件, 当response把ContentType设置成application/xxxx的时候呢,浏览器会默认启动下载,而不是试图打开. 通过给httpHeader里面加入内容 ...

  6. css复合选择器的权重

    选择器的权重 标签选择器的权重为0001 class选择器的权重为0010 id选择器的权重为0100 属性选择器的权重为0010 伪类选择器的权重为0010 伪元素选择器的权重为0010 包含选择器 ...

  7. layui&lpar;六&rpar;——upload组件常见用法总结

    layui中提供了非常简单的文件上传组件,这里写了一个上传图片的栗子,上传成功后返回图片在服务器的路径,并设置为页面中img的src属性.因为上传十分简单,没什么可说的,就直接上代码了. html代码 ...

  8. Netty:option和childOption参数设置说明

    Channel配置参数 (1).通用参数 CONNECT_TIMEOUT_MILLIS :   Netty参数,连接超时毫秒数,默认值30000毫秒即30秒. MAX_MESSAGES_PER_REA ...

  9. async源码学习 - 控制流程waterfall函数

    waterfall函数会连续执行数组中的函数,每次通过数组下一个函数的结果.然而,数组任务中的任意一个函数结果传递失败,那么该函数的下一个函数将不会执行,并且主回调函数立马把错误作为参数执行. 以上是 ...

  10. SVM、LR、决策树的对比

    一.LR LR,DT,SVM都有自身的特性,首先来看一下LR,工业界最受青睐的机器学习算法,训练.预测的高效性能以及算法容易实现使其能轻松适应工业界的需求.LR还有个非常方便实用的额外功能就是它并不会 ...