ZOJ 3652 Maze 模拟,bfs,读题 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4842
要注意题目中两点:
1.在踏入妖怪控制的区域那一刹那,先减行动力,然后才能杀妖怪
2.在妖怪控制区域行动力也会恢复
3.妖怪也许不在自己的控制区域
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std;
const int maxn=100;
const int dx[4] = {0,0,-1,1};
const int dy[4] = {1,-1,0,0};
int n,m,l,k,sx,sy,tx,ty;
int maz[maxn][maxn];
int mon[6][2]; int vis[maxn][maxn][maxn];
int ind[maxn][maxn];
struct P{
int x,y,sta,r;
P(){x = y = sta = r = 0;}
P(int _x,int _y,int _sta,int _r){
x = _x;
y = _y;
sta = _sta;
r = _r;
}
bool operator <(P p2)const {
if(vis[x][y][sta] != vis[p2.x][p2.y][p2.sta])
return vis[x][y][sta] > vis[p2.x][p2.y][p2.sta];
return r < p2.r;
}
};
int ok(int x,int y,int sta){
if(x < 1 || y < 1 || x > n || y > m)return -1;
int ki = maz[x][y];
if(ki == -1)return -1;
if(ki > 0){
if(sta & (1 << (ki-1)))return 1;
return 0;
}
return 1;
}
priority_queue <P> que;
int bfs(){
memset(vis ,-1, sizeof vis);
vis[sx][sy][0] = 0;
que.push(P(sx,sy,0,0));
while(!que.empty()){
P tp=que.top();que.pop();
// printf("pop x: %d y: %d sta: %d step %d turn %d\n",tp.x,tp.y,tp.sta,tp.r,vis[tp.x][tp.y][tp.sta]);
if(tx == tp.x && ty == tp.y)return vis[tx][ty][tp.sta];
for(int i = 0;i < 4;i++){
int tmpx = tp.x + dx[i];
int tmpy = tp.y + dy[i];
int sta = tp.sta;
if(ind[tmpx][tmpy] != 0)sta |= (1 << (ind[tmpx][tmpy] - 1));
if(ok(tmpx,tmpy,tp.sta) >= 0 && vis[tmpx][tmpy][sta] == -1){
if(tp.r == 0){
vis[tmpx][tmpy][sta] = vis[tp.x][tp.y][tp.sta] + 1;
// printf("push x: %d y: %d sta: %d step %d turn %d\n",
// tmpx,tmpy,sta,(ok(tmpx,tmpy,sta)==0?0:l-1),vis[tp.x][tp.y][tp.sta]+1);
que.push(P(tmpx,tmpy,sta,(ok(tmpx,tmpy,tp.sta) == 0?0:l-1)));
}
else {
vis[tmpx][tmpy][sta] = vis[tp.x][tp.y][tp.sta];
//printf("push x: %d y: %d sta: %d step %d turn %d\n",
// tmpx,tmpy,sta,(ok(tmpx,tmpy,sta)==0?0:tp.r-1),vis[tp.x][tp.y][tp.sta]);
que.push(P(tmpx,tmpy,sta,(ok(tmpx,tmpy,tp.sta) == 0?0:tp.r-1)));
}
}
}
}
return -1;
} int main(){
while(scanf("%d%d%d",&n,&m,&l) == 3){
while(!que.empty())que.pop();
memset(ind,0,sizeof ind);
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
scanf("%d",maz[i]+j);
}
}
scanf("%d",&k);
for(int i = 1;i <= k;i++){
scanf("%d%d",mon[i],mon[i] + 1);
ind[mon[i][0]][mon[i][1]]=i;
}
scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
int ans = bfs();
if(ans < 0){
puts("We need God's help!");
}
else {
printf("%d\n",ans);
}
}
return 0;
}
ZOJ 3652 Maze 模拟,bfs,读题 难度:2的更多相关文章
- Codeforces 659B Qualifying Contest【模拟,读题】
写这道题题解的目的就是纪念一下半个小时才读懂题...英文一多读一读就溜号... 读题时还时要静下心来... 题目链接: http://codeforces.com/contest/659/proble ...
- LeetCode 2 Add Two Numbers 模拟,读题 难度:0
https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-n ...
- zoj 3652 Maze
Maze Time Limit: 2 Seconds Memory Limit: 65536 KB Celica is a brave person and believer of a Go ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- ZOJ - 3890 Wumpus(BFS基础题)
Wumpus Time Limit: 2 Seconds Memory Limit: 65536 KB One day Leon finds a very classic game call ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- URAL 1830 Help in the RNOS 思路,读题 难度:1
http://acm.timus.ru/problem.aspx?space=1&num=1830 这道题需要理解题目操作的意思, 要更改第i位的状态,第i-1位必须激活为1,0-i-2位必须 ...
- poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
- hdu 3268 09 宁波 现场 I - Columbus’s bargain 读题 最短路 难度:1
Description On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera ...
随机推荐
- redhat 6.4 yum 本地配置简记
准备工作 ----------------------------------------------------------------------------- 1. 加载光驱 将iso镜像文件 ...
- Hello,Akka
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://www.blogbus.com/dreamhead-logs/235916459.html 只要稍微了解过一些Scala, ...
- Mvc4_ActionLink跟@RenderBody ,@RenderPage
. @Html.ActionLink("该链接要显示的文字A","对应的控制器方法B");会生成:<a href="C/B">A ...
- shell中for循环
shell中for循环总结 最常用的就是遍历操作某一类文件,比如批量建索引. for i in `ls` do samtools faidx $i done 注意:for末尾不需要冒号(:),循环的代 ...
- js上下滚屏效果,代码通过测试
这是html代码 <div class="box"> <div class="bcon"> <h1><b>领号实 ...
- 中医与DBA
很多药方,只要吃不死,总有一款适合你.哈哈.我要做西医,先检查身体,再了解每种药的作用,做到对症下药.
- c语言数据结构:递归的替代-------回溯算法
1.要理解回溯就必须清楚递归的定义和过程. 递归算法的非递归形式可采用回溯算法.主要考虑的问题在于: 怎样算完整的一轮操作. 执行的操作过程中怎样保存当前的状态以确保以后回溯访问. 怎样返回至上一次未 ...
- 从别人那淘的知识 深入剖析Java中的装箱和拆箱
(转载的海子的博文 海子:http://www.cnblogs.com/dolphin0520/) 深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来 ...
- IE7浏览器下CSS属性选择器二三事
一.为何专门说起IE7 以前,或者说数年前,我们从事桌面端网页开发的时候,基本上都还要兼顾IE6浏览器, 即使有些特性,IE7支持,我们也会忽略之.于是,我们会不自然地把IE6和IE7浏览器归为一路货 ...
- jmeter笔记6
一.图形报表 图表底部参数的含义如下: 样本数目是总共发送到服务器的请求数. 最新样本是代表时间的数字,是服务器响应最后一个请求的时间. 吞吐量是服务器每分钟处理的请求数. 平均值是总运行时间 ...