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. ORACLE性能优化之SQL语句优化

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   操作环境:AIX +11g+PLSQL 包含以下内容: 1.  SQL语句执行过程 2.  优化器及执行计划 3.  合 ...

  2. D3.js 选择元素和绑定数据/使用数据

    选择元素和绑定数据是 D3 最基础的内容,本文将对其进行一个简单的介绍. 一.如何选择元素 在 D3 中,用于选择元素的函数有两个: d3.select():是选择所有指定元素的第一个 d3.sele ...

  3. python字符串替换的2种有效方法

    python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'我把a字符串里的word替换为python1用字符串本身 ...

  4. Windows 内存架构

    理解 Virtual Memory, Physical Memory, Committed Memory, Page File, Working Set, Modified Pages, Standb ...

  5. openid4java 使用记录[转载]

    [文章来源:http://r-j-r-a5438-163-com.iteye.com/blog/611351] 在项目中使用了openid4java进行开发,在开发过程中碰到过一些问题,在网上也找了很 ...

  6. python 图实现

    #coding:utf-8 __author__ = 'similarface' class Graph: def __init__(self,label,extra=None): #节点是类实例 s ...

  7. commonJS — 数字操作(for Number)

    for Number github: https://github.com/laixiangran/commonJS/blob/master/src/forNumber.js 代码 /** * Cre ...

  8. SwitchCompat 修改颜色

    Ok, so I'm sorry but most of these answers are incomplete or have some minor bug in them. The very c ...

  9. ios 企业证书 ipa 重新签名发布

    提示:暂时不能用了,企业证书滥用 ios 企业证书 ipa 重新签名发布 1. 应用场景 当前有一个 未用企业证书签名的 ipa 文件,默认是不可以直接安装到设备上的:我们需要用企业版证书签名: 当前 ...

  10. 8.3 ContosoMVCWeb官方案例学习

    1. 分页案例学习 2. 排序搜索案例学习 3.使用Configuration.cs中的Seed方法 在数据库迁移过程中,使用update-database,会运行seed方法.seed方法能够将初始 ...