ZOJ 3890 Wumpus
2015-09-05 17:21
218 查看
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
Sample Output
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:
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:
#include <iostream> #include <queue> #include <string.h> #include <math.h> #include <stdio.h> #include <algorithm> using namespace std; int n,t,x,y; int ma[25][25]; int vis[25][25][4][2]; int d[4][2]={{1,0},{0,-1},{-1,0},{0,1}}; struct pp { int x,y,point,dir,gold; }; int bfs() { int sum=0; pp you,start,next; queue<pp>q; you.x=0; you.y=0; you.point=0; you.dir=0; you.gold=0; q.push(you); while(!q.empty()) { start=q.front(); if(start.x==0&&start.y==0) sum=max(sum,start.point); q.pop(); for(int i=-1;i<=1;i++) { next=start; next.point=start.point-10; if(i==0) { next.x=start.x+d[next.dir][0]; next.y=start.y+d[next.dir][1]; } else next.dir=(next.dir+i+4)%4; if(next.x<0||next.y<0||next.x>=n||next.y>=n||ma[next.x][next.y]==1||ma[next.x][next.y]==2||vis[next.x][next.y][next.dir][next.gold]) continue; vis[next.x][next.y][next.dir][next.gold]=1; if(ma[next.x][next.y]==3&&next.gold==0) { next.point+=990; next.gold=1; } q.push(next); } } return sum-10; } int main() { int k; cin>>k; while(k--) { cin>>n; memset(ma,0,sizeof ma); memset(vis,0,sizeof vis); int flag=0; for(int i=0;i<500;i++) { cin>>t>>x>>y; if(t==-1) break; else if(t==1) ma[x][y]=1; else if(t==2) { ma[x][y]=2; if(x==0&&y==0) flag=1; } else if(t==3) ma[x][y]=3; } int ans=bfs(); if(flag || ans<0) printf("-1\n"); else printf("%d\n",ans); } return 0; }
相关文章推荐
- Android 控件之一:Button 按钮
- 密码学3——Java BASE64加密解密
- 设计模式
- 四层和七层负载均衡的区别
- C++和C语言混编
- as和is类型转换
- 密码学2——Java byte数组与十六进制字符串互转
- 操作系统内存管理——分区、页式、段式管理
- DBGrid1
- 小解C++模板特化
- iOS Simulator运行慢悠悠,但是APP的功能又很正常
- 有关性能和误删的注意
- 密码学1——Java 加密解密基础
- 进程调度算法
- php-fpm 在centos 7下的安装配置
- 查看nginx cache命中率
- Android中Handler使用不当引起的内存泄露
- 网络游戏中的主循环
- c语言之单链表的创建及排序
- 1063. Set Similarity (25)