Problem8369--Wumpus

8369: Wumpus

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 256 MiB

Description

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\times 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.
For each step, there are $6$ 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 $1,000$ 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)$.


有一个 $n*n$ 的迷宫,然后输入怪兽(数字 1 表示),矿井(数字 2 表示),金矿(数字 1 表示)的位置。

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 \leq 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 1 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 1 Output

850
870
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.

Source/Category

基础算法 4.100.BFS