ZOJ 3890 Wumpus (2015年7月浙大月赛、广搜)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5560


题面:

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)

ZOJ 3890 Wumpus (2015年7月浙大月赛、广搜)_第1张图片
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:

ZOJ 3890 Wumpus (2015年7月浙大月赛、广搜)_第2张图片
Author: JIANG, Kairong
Source: ZOJ Monthly, July 2022


解题:

    先MLE,再TLE,再WA。就差RE、CE、PE了。比赛的时候改了又改,最后因为ans小于等于0没考虑到,一直wa到比赛结束....

    这道题也不算坑,学了很多东西。如下:

1.需加vis数组,因为若是一个封闭区间,而目标点又不可达,那么就会一直在那个区间内绕圈造成MLE。

2.同时要考虑不能从转过来的方向转回去,这会MLE。

3.同时不能转超过3次,要不然就在原地转圈了,这会TLE。

4.因为没考虑到1000这么小,减减就没了,当答案小于等于0时输出-1(题目貌似说错了,0应该也是-1)


代码:

#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
int map[25][25],dire[4][2]={1,0,0,1,-1,0,0,-1};
bool vis[25][25];
struct node
{
   int x,y,dir,step,cnt,typ;
};
queue <node> qe;
int n,goldx,goldy,golddir;
int bfs(int xx,int yy,int fl,int dir)
{
  node tmp,cur; 
  while(!qe.empty())
	  qe.pop();
  int x,y;
  vis[xx][yy]=1;
  tmp.x=xx;
  tmp.y=yy;
  tmp.dir=dir;
  tmp.step=0;
  tmp.cnt=0;
  tmp.typ=-1;
  qe.push(tmp);
  while(!qe.empty())
  {
    cur=qe.front();
	qe.pop();
    x=cur.x;
	y=cur.y;
	vis[x][y]=1;
	if(map[x][y]==fl)
	{
        goldx=x;
		goldy=y;
		golddir=cur.dir;
		return cur.step;
	}
	dir=cur.dir;
	x=x+dire[dir][0];
	y=y+dire[dir][1];
      //当且仅当一个点没越界,没走过,且不是坑和怪兽,才走
        if(x>=0&&x<n&&y>=0&&y<n&&!vis[x][y]&&map[x][y]!=1&&map[x][y]!=2)
	{
		vis[x][y]=1;
		tmp.x=x;
		tmp.y=y;
        tmp.dir=dir;
		tmp.step=cur.step+1;
		tmp.cnt=0;
		tmp.typ=-1;
		qe.push(tmp);
	}
	if(cur.typ!=1&&cur.cnt<4)
	{
       tmp=cur;
	   tmp.step++;
	   tmp.dir=(cur.dir+3)%4;
	   tmp.cnt++;
	   tmp.typ=2;
	   qe.push(tmp);
	}
	if(cur.typ!=2&&cur.cnt<4)
	{
       tmp=cur;
	   tmp.step++;
	   tmp.dir=(cur.dir+1)%4;
	   tmp.cnt++;
	   tmp.typ=1;
	   qe.push(tmp);
	}
  }
  return -1;
}
int main()
{
    int t,x,y,v,ans;
	scanf("%d",&t);
	while(t--)
	{
      scanf("%d",&n);
	  memset(map,0,sizeof(map));
      while(1)
	  {
        scanf("%d%d%d",&v,&x,&y);
		if(x==-1)break;
		map[x][y]=v;
	  }
	  if(map[0][0]==2)
	  {
		  printf("-1\n");
		  continue;
	  }
	  memset(vis,0,sizeof(vis));
         //两遍搜索 
          ans=bfs(0,0,3,0);
	  if(ans==-1)
	  {
		  printf("-1\n");
		  continue;
	  }
	  map[0][0]=4;
	  memset(vis,0,sizeof(vis));
	  ans+=bfs(goldx,goldy,4,golddir);
	  ans+=2;
	  ans=1000-ans*10;
	  if(ans>0)
	  printf("%d\n",ans);
	  else printf("-1\n");
	}
	return 0;
}


你可能感兴趣的:(搜索,ZOJ,浙大月赛)