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. hdu3124Arbiter(最小圆距离-扫描线)

    链接 详解http://blog.sina.com.cn/s/blog_6e7b12310100qnex.html #include <iostream> #include<cstd ...

  2. ElasticSearch(ES)和solr的关系和区别

    可以参考这篇文章:http://www.cnblogs.com/chowmin/articles/4629220.html Solr 2004年诞生(当时是Solar). ElasticSearch ...

  3. count-the-repetitions

    https://leetcode.com/problems/count-the-repetitions/ 下面是我的方法,结果对的,超时了... package com.company; class ...

  4. 转:c的回归-云风

    C 的回归 周末出差,去另一个城市给公司的一个项目解决点问题.回程去机场的路上,我用手机上 google reader 打发时间.第一眼就看到孟岩大大新的一篇:Linux之父话糙理不糙 .主题是 C ...

  5. 转载: C++ 转换构造函数 和 类型转换函数

    1.对于系统的预定义基本类型数据,C++提供了两种类型转换方式:隐式类型转换和显式类型转换. ,sum; double b=5.55; sum=a+b;//-------(1) std::cout&l ...

  6. COM组件(ATL篇)

    目录 第1章创建进程内组件    1 1.1 目标    1 1.2 创建项目    3 1.2.1 VC++6.0    3 1.2.2 VC++2010    5 1.3 增加COM类    6 ...

  7. Facade 运行机制

    举一个常见的例子在routes.php路由文件中的Route就是使用了laravel的Facade; config/app.php 文件中的aliases数组: 再例如其中的mail: 里面只是简单的 ...

  8. [redis] Redis 配置文件置参数详解

    ################################ 基础配置 ################################# #daemonize no 默认情况下, redis 不 ...

  9. Ubuntu下配置和编译cpp-ethereum客户端

    Ethereum,中文翻译是“以太坊”,是一个公有区块链的开源项目.因为以太坊是基于P2P网络所以没有中心节点,所以用户仅安装Ethereum客户端即可连入Ethereum公共网络或者在自己的test ...

  10. C实现多线程

    #include <stdio.h> #include <pthread.h> #include <unistd.h> #include <iostream& ...