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 were horrible pits which could lead to death everywhere. However, there were also a huge amount of gold in the cave. The agent must be careful
and sensitive so that he could grab all of the gold and climb out of the cave safely.

The cave can be regarded as a n*n board. In each square there could be a Wumpus, a pit, a brick of gold, or nothing. The agent would be at position (0,0) at first and headed right.(As the picture below)

For each step, there are six possible movements including going forward, turning left, turning right, shooting, grabbing the gold, and climbing out of the cave. If the agent steps
into a square containing a pit or Wumpus, he will die. When the agent shoots, the Wumpus in front of him will die. The goal of the agent is to grab all of the gold and return to the starting position and climb out(it‘s OK if any Wumpus is still living).When
a brick of gold is grabbed successfully, you will gain 1000 points. For each step you take, you will lose 10 points.

Your job is to help him compute the highest point he can possibly get.

For the purpose of simplification, we suppose that there is only one brick of gold and the agent
cannot shoot the Wumpus.

If there is a pit at (0, 0), the agent dies immediately. There will not be a Wumpus at (0, 0).

Input

There are multiple cases. The first line will contain one integer k that indicates the number of cases.

For each case:

The first line will contain one integer n (n <= 20).

The following lines will contain three integers, each line shows a position of an object. The first one indicates the type of the object. 1 for Wumpus, 2 for pit and 3 for gold. Then the next two integers show the x and y coordinates of the object.

The input end with -1 -1 -1. (It is guaranteed that no two things appear in one position.)

Output

The output contains one line with one integer, which is the highest point Leon could possibly get. If he cannot finish the game with a non-negative score, print "-1".

Sample Input

2
3
1 1 1
2 2 0
3 2 2
-1 -1 -1
3
1 1 1
3 2 2
-1 -1 -1

Sample Output

850
870

Hint

For the sample 1, the following steps are taken:

turn left, forward, forward, turn right, forward, forward, grab, turn left, turn left, forward, forward, turn left, forward, forward, climb.

There are in all 15 steps, so the final score is 840. For the sample 2 , the path is as follow:


Author: JIANG, Kairong

Source: ZOJ Monthly, July 2022

大致题意:人在迷宫中,有一个宝藏,需要找到这个宝藏然后离开出口,求最短花费的时间,每秒可以做这几件事:

拿宝藏,离开出口,向左转,向右转,向前走

大致思路:

BFS,每个格子对应了四个状态,方向状态,然后就是bfs辣。

acm半年了,又写了一次基础的搜索,莫名感概

//Accepted	3890	C++	0	280
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<ll,ll> pii;

const int N = 25;
int mp[N][N];
int n;

bool vis[N][N][8];
struct node{
        int x,y,dir,t;
        node(int x = 0,int y = 0,int dir = 0,int t= 0):x(x),y(y),dir(dir),t(t){}
};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
queue<node>que;
int bfs(){
        que.push(node(1,1,0,0));
        vis[1][1][0] = 1;
        while(!que.empty()){
                node cur = que.front();
                que.pop();
                int d = 0;
                if( mp[cur.x][cur.y] == 3 && cur.dir < 4){
                        if(vis[cur.x][cur.y][cur.dir+4] == 0){
                                que.push(node(cur.x,cur.y,cur.dir+4,cur.t+1));
                                vis[cur.x][cur.y][cur.dir+4] = 1;
                        }
                        continue;
                }
                if( cur.x == 1 && cur.y == 1 && cur.dir > 3) return 1000-(cur.t+1)*10;
                if( cur.x < 1 || cur.y < 1 || cur.x > n || cur.y > n) continue;
                if( mp[cur.x][cur.y] == 1 || mp[cur.x][cur.y] == 2) continue;
                if(cur.dir > 3){
                        d += 4;
                        cur.dir -= 4;
                }
                if(vis[cur.x][cur.y][(cur.dir+1+4)%4+d] == 0){
                        que.push(node(cur.x,cur.y,(cur.dir+1+4)%4+d,cur.t+1));
                        vis[cur.x][cur.y][(cur.dir+1+4)%4+d] = 1;
                }
                if(vis[cur.x][cur.y][(cur.dir-1+4)%4+d] == 0){
                        que.push(node(cur.x,cur.y,(cur.dir-1+4)%4+d,cur.t+1));
                        vis[cur.x][cur.y][(cur.dir-1+4)%4+d] = 1;
                }
                if(vis[cur.x+dx[cur.dir]][cur.y+dy[cur.dir]][cur.dir+d] == 0){
                        que.push(node(cur.x+dx[cur.dir],cur.y+dy[cur.dir],cur.dir+d,cur.t+1));
                        vis[cur.x+dx[cur.dir]][cur.y+dy[cur.dir]][cur.dir+d] = 1;
                }
        }
        return -1;
}
void ini(){
        memset(vis,0,sizeof(vis));
        memset(mp,0,sizeof(mp));
        while(!que.empty()) que.pop();
}
int main(){

        int T;
        cin>>T;
        while(T--){
                scanf("%d",&n);
                ini();
                while(true){
                        int a,x,y;
                        scanf("%d%d%d",&a,&x,&y);
                        if( a == -1 && x == -1 && y == -1) break;
                        mp[++x][++y] = a;
                }
                if(mp[1][1] == 2) {
                        puts("-1");
                        continue;
                }
                int ans = bfs();
                if(ans < 0) puts("-1");
                else printf("%d\n",ans);
        }
}

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

时间: 2024-08-24 00:45:33

ZOJ - 3890 Wumpus(BFS基础题)的相关文章

zoj 3890 Wumpus bfs

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3890 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

ZOJ 3890 Wumpus (BFS)

题目链接:ZOJ 3890 Wumpus 题意:一个人在n*n的地图的左下角,他想逃出地图,并且他想要分数达到最高,他可以做的操作有直走,左转,右转,射击,爬,挖金矿6个操作,每个操作后分数减10,但是挖到金矿分数加1000,问你逃出地图的最大分数是多少.他可能遇到怪兽但是他不射击(也就是说没有射击的操作),金库在地图中也只有一个. 思路:BFS搜一遍就好了 AC代码: #include <stdio.h> #include <string.h> #include <queu

ZOJ 3890 Wumpus

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 3890 Wumpus(zoj 2015年7月月赛)

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 3203 Light Bulb,三分基础题

Light Bulb Time Limit: 1 Second      Memory Limit: 32768 KB Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house is narrow and he has only one light bulb in his house. Every night, he is wandering in his incommodio

【坑】这些天刷基础题犯的诡异错误大集合

这些天刷基础题犯的诡(sha)异(bi)错误大集合 by pkl ———其中可能会有部分资料引用,引用会表明链接,如果没有标明敬请指出QAQ抱歉QAQ---------------------------------- 首先安利一发帖子:OI中有哪些常数优化的小技巧 ps:注意是基础题.所以嘛错误nc需要原谅..毕竟我也是蒟蒻QAQAQ大蒟蒻QAQ · 循环里的临时变量出了循环便无效· 递归的临时变量不要定成全局变量· 赋值的对象不要一不小心手抖写反了…比如b = a写成a = b[估计也只有我

1、基础题

基础题: 1.表单中 get与post提交方法的区别? 答:get是发送请求HTTP协议通过url参数传递进行接收,而post是实体数据,可以通过表单提交大量信息. 2.session与cookie的区别? 答:session:储存用户访问的全局唯一变量,存储在服务器上的php指定的目录中的(session_dir)的位置进行的存放 cookie:用来存储连续訪問一个頁面时所使用,是存储在客户端,对于Cookie来说是存储在用户WIN的Temp目录中的. 两者都可通过时间来设置时间长短 3.数据

【HDU1232】畅通工程(并查集基础题)

裸敲并查集,很水一次AC 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 #include <string> 1

linux 基础题整理

基础题: 1.查看系统内核版本号及系统名称 2.查看smb服务所用的端口号 3.禁ping 4.查出22端口现在运行什么程序 5.登录提示符前的输出信息"you are welcome!!!" 6.成功登录后自动输出信息"距离全国比赛还剩1天!!!" 7.确认安全终端为tty1 8.取消普通用户的控制台访问的三个权限:reboot.halt.shutdown 9.只允许组ID为10的成员通过su命令改变为root用户 10.禁止Control-Alt-Delete键