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;
}
时间: 2024-11-05 03:33:46

ZOJ 3652 Maze 模拟,bfs,读题 难度:2的相关文章

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

https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked

ZOJ - 3890 Wumpus(BFS基础题)

Wumpus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day Leon finds a very classic game called Wumpus.The game is as follow. Once an agent fell into a cave. The legend said that in this cave lived a kind of monster called Wumpus, and there we

POJ3126 Prime Path bfs, 水题 难度:0

题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为e,每步可以做如下操作,把当前的s中的四位数上的某一位改变,比如1009可以变为2009,1008,1309,1049,然后检验结果是否为大于1000的质数,如果是,那就可以把s变为这个数. 思路 质数明显需要先处理出来,然后采用bfs获取结果即可. 感想 下次需要先计算空间复杂度, 1e8的空间复

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

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697   Accepted: 10800 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

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

http://acm.timus.ru/problem.aspx?space=1&num=1830 这道题需要理解题目操作的意思, 要更改第i位的状态,第i-1位必须激活为1,0-i-2位必须为0,如果0-i-1位开始时全为0,那么从0位开始进行操作 一.首先考虑对于0-i-1位都是0,需要更改i位的情况,需要 1.更改i-1位,2.按一下打开下一页 对于更改i-1位,需要1.更改i-2位,2.按一下打开下一页,3.更改i-2位 可以得到一个式子,设f[i]为第0-i-1位均为0时,使得状态成为

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

csu - 1566: The Maze Makers (bfs)

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1566 题意还是蛮难懂的,至少对于我来说,需要认真读题. 输入矩阵的每一个数字换成2进制后,顺时针围一圈,用1表示墙,0表示空,这样就可以表示出一个迷宫,现在就是判断这个迷宫属于4种类型中哪种类型. 参考了 大神博客.构图很难,并且想法跟以往的题都不一样.是一个好题. 1 #include <iostream> 2 #include <cstdio> 3 #include <c

[noip模拟20170921]模版题

  今天考的是一套很基础的模版题,但是我这种蒟蒻竟然还是没有AK,不得不说,蒟蒻和大佬的差别不是一点点啊 1.暴走的猴子(walk.pas/c/cpp) [题目描述] 从前有一个森林,森林里生活着一群猴子,这里猴子有个恶趣味——暴走.现在给你这个森林里的树木描述,你能计算出这只猴子在暴走k步后会蹦达到哪里吗(友情提示:由于你上周帮助猎人写程序打死了猴子父亲,所以今天猴子特别不爽,故意暴走了很多很多步来为难你,从而导致了k非常的大,做好心里准备噢-) [输入数据] 第一行两个数n,m表示树木数和询

CSU1566: The Maze Makers(BFS)

Description The Maze Makers is a publisher of puzzle books. One of their most popular series is maze books. They have a program that generates rectangular two-dimensional mazes like the one shown in Figure 1. The rules for these mazes are: (1) A maze