ZOJ 3890 Wumpus (BFS)

题目链接:ZOJ 3890 Wumpus

题意:一个人在n*n的地图的左下角,他想逃出地图,并且他想要分数达到最高,他可以做的操作有直走,左转,右转,射击,爬,挖金矿6个操作,每个操作后分数减10,但是挖到金矿分数加1000,问你逃出地图的最大分数是多少。他可能遇到怪兽但是他不射击(也就是说没有射击的操作),金库在地图中也只有一个。

思路:BFS搜一遍就好了

AC代码:

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int mp[25][25];
int xx[4]={-1,1,0,0};
int yy[4]={0,0,-1,1};//0shang 1xia 2zuo 3you
int vis[25][25][10][2];
int n;
struct node {
    int x,y;
    int s,g;
    int dir;//面对的方向
	bool operator<(const node &hh)const {
		return s<hh.s;
	}
}front,rear;

bool judge(int x,int y){
    if(x>=0 && y>=0 && x<n && y<n)
        return true;
    return false;
}

int Change(int dir,int ch){
    if(dir==0 && ch==0) return 2;
    if(dir==0 && ch==1) return 3;

    if(dir==1 && ch==0) return 3;
    if(dir==1 && ch==1) return 2;

    if(dir==2 && ch==0) return 1;
    if(dir==2 && ch==1) return 0;

    if(dir==3 && ch==0) return 0;
    if(dir==3 && ch==1) return 1;
}

//0左转,1右转,2直行
//0shang 1xia 2zuo 3you
int bfs(){
    node sta;
    int dx,dy,i;
    memset(vis,0,sizeof vis);
    if(mp[0][0]==2)
        return -1;
    priority_queue<node> q;
    sta.x=0,sta.y=0,sta.s=0,sta.g=0,sta.dir=1;
    vis[sta.x][sta.y][sta.dir][0]=1;
    q.push(sta);
    while(!q.empty()){
        front=q.top();
        q.pop();
        for(i=0;i<3;i++){
            if(i!=2){
                rear=front;
                rear.dir=Change(front.dir,i);
                rear.s=front.s-10;
                if(vis[rear.x][rear.y][rear.dir][rear.g]==0){
                    vis[rear.x][rear.y][rear.dir][rear.g]=1;
                    q.push(rear);
                }
                continue;
            }
            dx=front.x+xx[front.dir];
            dy=front.y+yy[front.dir];
            if(!judge(dx,dy))
                continue;
            if(dx==0 && dy==0 && front.g==1){
                return front.s-20;
            }
            if(vis[dx][dy][front.dir][front.g]==0 && mp[dx][dy]!=1 && mp[dx][dy]!=2) {
				vis[dx][dy][front.dir][front.g]=1;
                rear.x=dx;
                rear.y=dy;
                rear.s=front.s-10;
                rear.dir=front.dir;
				if(mp[dx][dy]==3) {
                    rear.s=rear.s+990;
                    rear.g=1;
                }
                q.push(rear);
            }
        }
    }
	return -1;//走不出去也是-1
}

int main(){
    int t;
    int op,x,y;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        memset(mp,0,sizeof mp);
        while(1){
            scanf("%d %d %d",&op,&x,&y);
            if(op==-1 && x==-1 && y==-1)
                break;
            mp[x][y]=op;
        }
        int ans=bfs();
        printf("%d\n",ans);
    }
return 0;
}
/*
2
3
1 1 1
2 2 0
3 2 2
2 0 0
-1 -1 -1
1
-1 -1 -1

*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-26 21:09:56

ZOJ 3890 Wumpus (BFS)的相关文章

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

zoj 2913 Bus Pass (BFS)

Bus Pass Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The way the bus syst

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

牛汇(BFS)入金具体流程(图文指导)

牛汇开户流程:bfsforex牛汇入金教程 所谓入金,也就是充值的意思,必须充钱到平台才能进行外汇交易.首先,我们先登录bfsforex牛汇官方网站,在交易办公室功能区域下面,点击账户入金: 为您提供中国各大银行的网银支付解决方案,支持人民币支付,和信用卡入金,入金是实时到账的. 牛汇(BFS)入金具体流程(图文指导),布布扣,bubuko.com

URAL 1930 Ivan&#39;s Car(BFS)

Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are

【判重+广搜(bfs)】魔板

判重+广搜(bfs)]魔板 Time Limit: 1000MS Memory Limit: 32768KB Special Judge 有一个两行四列的魔板,每个格子里有一个1到8的数字(数字唯一),现在我们可以对魔板进行以下操作: 1.交换两行的数字. 2.将第一列移到第二列,第二列到第三列,第三列到第四列,第四列到第一列. 3.将中间四个数顺时针转一次. 现给你初始状态,我末状态请你用最小的步数将它从初始状态变到末状态. 输入: 前两行,每行4个数表示初状态. 后两行,每行4个数表示末状态

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig