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的更多相关文章

  1. Codeforces 659B Qualifying Contest【模拟,读题】

    写这道题题解的目的就是纪念一下半个小时才读懂题...英文一多读一读就溜号... 读题时还时要静下心来... 题目链接: http://codeforces.com/contest/659/proble ...

  2. LeetCode 2 Add Two Numbers 模拟,读题 难度:0

    https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-n ...

  3. zoj 3652 Maze

    Maze Time Limit: 2 Seconds      Memory Limit: 65536 KB Celica is a brave person and believer of a Go ...

  4. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Descrip ...

  5. ZOJ - 3890 Wumpus(BFS基础题)

    Wumpus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day Leon finds a very classic game call ...

  6. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  7. URAL 1830 Help in the RNOS 思路,读题 难度:1

    http://acm.timus.ru/problem.aspx?space=1&num=1830 这道题需要理解题目操作的意思, 要更改第i位的状态,第i-1位必须激活为1,0-i-2位必须 ...

  8. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  9. hdu 3268 09 宁波 现场 I - Columbus’s bargain 读题 最短路 难度:1

    Description On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera ...

随机推荐

  1. EF中使用SQL语句或存储过程(小笔记)

    1.无参数查询 var model = db.Database.SqlQuery<UserInfo>("select* from UserInfoes ").ToLis ...

  2. 【浏览器渲染原理】渲染树构建之渲染树和DOM树的关系(转载 学习中。。。)

    在DOM树构建的同时,浏览器会构建渲染树(render tree).渲染树的节点(渲染器),在Gecko中称为frame,而在webkit中称为renderer.渲染器是在文档解析和创建DOM节点后创 ...

  3. Centos7 PHP7 编译安装 开机自启动

    1.PHP7.0.13下载 wget http://cn2.php.net/get/php-7.0.13.tar.gz/from/this/mirror 2.解压 .tar.gz 3. 进入目录 cd ...

  4. form作为module name 悲剧了

    爆出很无语的错误,也怪我,没有实地的debug. 所以,module name应该是不能碰关键词类似,最好custom一点好.

  5. OpneCV 二值图像区域处理

    //--------------------------------------[程序说明]------------------------------------------- // 在图像处理中总 ...

  6. c++ string 与 char 互转 以及base64

    c++ string 与 char 互转 很简单如下 ] = {'A','B','C','D','E'}; printf("%s\n",bts); //char to string ...

  7. ios 开发 收起键盘的小技巧

    在UIViewController中收起键盘,除了调用相应控件的resignFirstResponder方法外,还有另外三种方法: 1.重载UIViewController中的touchesBegin ...

  8. robotframework笔记21

    创建输出 当执行测试,创建多个输出文件和所有的 都以某种方式相关测试结果. 本节讨论什么 输出创建.如何配置创建,以及如何 调整他们的内容. 不同的输出文件 本节解释了不同可以创建和输出文件 如何配置 ...

  9. Growing转化的每一步(笔记整理)

    渠道流量监控中,如何将劣质流量和优质流量区分开来? 劣质流量总会有有一些不同于其他正常渠道的特征,比如在同一个时间中大量集中访问.使用的硬件设备比较固定.使用特定的浏览器等. 实际案例:去年有一些 A ...

  10. 用过的正则(更新ing)

    http://www.debuggex.com/   这个很好用20120912 //十六进制颜色值的正则表达式 var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6} ...