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. git的作用和原理(待续)

    先选定一个目录作为Git仓库,假定是/srv/sample.git,在/srv目录下输入命令: $ sudo git init --bare sample.git Git就会创建一个裸仓库,裸仓库没有 ...

  2. Java 默认/缺省 server 还是 client 模式

    不多说,复制官方文档,适用于 Java 5 6 7 Architecture OS Default client VM if server-class, server VM; otherwise, c ...

  3. 【转载】PHP运行模式的深入理解

    PHP运行模式的深入理解 作者: 字体:[增加 减小] 类型:转载 时间:2013-06-03我要评论 本篇文章是对PHP运行模式进行了详细的分析介绍,需要的朋友参考下   PHP运行模式有4钟:1) ...

  4. easyui 上传文件代码

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;usi ...

  5. Android Studio新建Jni工程

    2.2版本的Android Studio支持新建Jni工程,不用再像以前自己构建工程目录,首先把自己的升级自己的AS到2.2以上 然后打开Tools->Andorid->SDK manag ...

  6. java 多线程8(join)

    join():  加入.一个线程如果执行了join语句,那么就有新的线程加入,执行该语ij 句的线程必须要让步给新加入的线程先完成任务,然后才能继续执行. import java.util.Scann ...

  7. 【BZOJ 2243】染色 - 树链剖分+线段树

    #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; const ...

  8. DispatcherServlet中使用的特殊的Bean

    DispatcherServlet默认使用WebApplicationContext作为上下文,因此我们来看一下该上下文中有哪些特殊的Bean: 1.Controller:处理器/页面控制器,做的是M ...

  9. 【CITE】 C#中实现拖动无边框Form窗体

    首先建一个Windows应用程序 将Form1的 FormBorderStyle属性设置为None 主要是在Form1窗体触发三个事件:Form4_MouseDown,Form4_MouseMove, ...

  10. selenium+python笔记5

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ @desc: 登陆126邮箱 """ f ...