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);
}
}
ZOJ - 3890 Wumpus(BFS基础题)的更多相关文章
- ZOJ 3890 Wumpus
Wumpus Time Limit: 2 Seconds Memory Limit: 65536 KB One day Leon finds a very classic game call ...
- ZOJ 1654 二分匹配基础题
题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...
- POJ 2251 三维BFS(基础题)
Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! Th ...
- bfs简单题-poj2251
宽搜基础题 思路很简单,注意细节. 走过的节点一定要打上标记//tag数组 三维字符串输入一定要注意 #include <stdio.h> #include <iostream> ...
- Android测试基础题(三)
今天接着给大家带来的是Android测试基础题(三). 需求:定义一个排序的方法,根据用户传入的double类型数组进行排序,并返回排序后的数组 俗话说的好:温故而知新,可以为师矣 packag ...
- 小试牛刀3之JavaScript基础题
JavaScript基础题 1.让用户输入两个数字,然后输出相加的结果. *prompt() 方法用于显示可提示用户进行输入的对话框. 语法: prompt(text,defaultText) 说明: ...
- 小试牛刀2:JavaScript基础题
JavaScript基础题 1.网页中有个字符串“我有一个梦想”,使用JavaScript获取该字符串的长度,同时输出字符串最后两个字. 答案: <!DOCTYPE html PUBLIC &q ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- nyist oj 79 拦截导弹 (动态规划基础题)
拦截导弹 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...
随机推荐
- 【bzoj2527】[Poi2011]Meteors 整体二分+树状数组
题目描述 有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下陨石雨.BIU已经预测了接下来K场陨石雨的情况.BI ...
- Canvas与Image互相转换示例以及利用该技术实现微信长按自动识别二维码功能
现在扫描二维码已经很普遍,微信扫一扫即可,但是如果二维码是在自己的手机上呢?那就要用到微信里的一个功能了,手指长按二维码,会弹出自动识别的选项,点确定就可以看到二维码的内容了.那么怎么通过前端实现这个 ...
- webstorm不能使用stylus
1.https://stackoverflow.com/questions/23583514/webstorm-8-showing-errors-for-a-correct-html-tag 2. & ...
- NodeJS学习(1)--- 安装配置介绍
Node.js 安装配置 本章节我们将向大家介绍在window和Linux上安装Node.js的方法. 本安装教程以Node.js v6.10.1 LTS(长期支持版本)版本为例. Node.js安装 ...
- TCP/IP协议详解笔记——IP协议
简介 TCP/IP协议族中最核心的协议,提供不可靠.无连接的数据报传输服务. 不可靠:不能保证IP数据报能成功送达. 无连接:并不维护后续数据报的状态信息,每个数据报的处理都是相互独立.数据报可能不会 ...
- 在vscode中使用pylint-django插件解决pylint的一些不必要的错误提示【转】
转自:http://www.cnblogs.com/chaojihexiang/p/6417835.html 微软的vscode编辑器是一个好东西,通过vscode编辑python程序非常的方便.推荐 ...
- Scrapy学习-21-信号量
scrapy信号量 定义 Scrapy使用信号来通知事情发生.您可以在您的Scrapy项目中捕捉一些信号(使用 extension)来完成额外的工作或添加额外的功能,扩展Scrapy. 虽然信号提供了 ...
- Python学习杂记_11_函数(一)
函数也叫方法,就是把实现某种功能的一组代码封装起来,当你需要这个功能时直接调用函数即可. 定义函数:定义函数时要注意 “def”关键字,“:”,“函数体缩进”:用“return”使函数有具体返回值,没 ...
- Java使用apache的开源数据处理框架commons-dbutils完成增删改
主要使用这个开源jar包的QueryRunner类的update方法来完成数据库的增删改操作. package demo; import java.sql.Connection; import jav ...
- 2018年东北农业大学春季校赛 I wyh的物品【01分数规划/二分】
链接:https://www.nowcoder.com/acm/contest/93/I来源:牛客网 题目描述 wyh学长现在手里有n个物品,这n个物品的重量和价值都告诉你,然后现在让你从中选取k个, ...