hdu1044(记忆化搜索+广搜)

Collect More Jewels

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3530    Accepted Submission(s): 702


Problem Description
It is written in the Book of The Lady: After the Creation, the cruel god Moloch rebelled against the authority of Marduk the Creator.Moloch stole from Marduk the most powerful of all the artifacts of the gods, the Amulet of Yendor, and he hid it in the dark cavities of Gehennom, the Under World, where he now lurks, and bides his time.

Your goddess The Lady seeks to possess the Amulet, and with it to gain deserved ascendance over the other gods.

You, a newly trained Rambler, have been heralded from birth as the instrument of The Lady. You are destined to recover the Amulet for your deity, or die in the attempt. Your hour of destiny has come. For the sake of us all: Go bravely with The Lady!

If you have ever played the computer game NETHACK, you must be familiar with the quotes above. If you have never heard of it, do not worry. You will learn it (and love it) soon.

In this problem, you, the adventurer, are in a dangerous dungeon. You are informed that the dungeon is going to collapse. You must find the exit stairs within given time. However, you do not want to leave the dungeon empty handed. There are lots of rare jewels in the dungeon. Try collecting some of them before you leave. Some of the jewels are cheaper and some are more expensive. So you will try your best to maximize your collection, more importantly, leave the dungeon in time.
 

Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.

The first line of each test case contains four integers W (1 <= W <= 50), H (1 <= H <= 50), L (1 <= L <= 1,000,000) and M (1 <= M <= 10). The dungeon is a rectangle area W block wide and H block high. L is the time limit, by which you need to reach the exit. You can move to one of the adjacent blocks up, down, left and right in each time unit, as long as the target block is inside the dungeon and is not a wall. Time starts at 1 when the game begins. M is the number of jewels in the dungeon. Jewels will be collected once the adventurer is in that block. This does not cost extra time.

The next line contains M integers,which are the values of the jewels.

The next H lines will contain W characters each. They represent the dungeon map in the following notation:
> [*] marks a wall, into which you can not move;
> [.] marks an empty space, into which you can move;
> [@] marks the initial position of the adventurer;
> [<] marks the exit stairs;
> [A] - [J] marks the jewels.
 

Output
Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.

If the adventurer can make it to the exit stairs in the time limit, print the sentence "The best score is S.", where S is the maximum value of the jewels he can collect along the way; otherwise print the word "Impossible" on a single line.
 

Sample Input
   
   
   
   
3 4 4 2 2 100 200 **** *@A* *B<* **** 4 4 1 2 100 200 **** *@A* *B<* **** 12 5 13 2 100 200 ************ *B.........* *.********.* *@...A....<* ************
 

Sample Output
   
   
   
   
Case 1: The best score is 200. Case 2: Impossible Case 3: The best score is 300.
 
本题我是用的比较裸的bfs,由于状态总数为格子数*10(A-J),所以可以开辟一个三维数组存储状态,用广搜可以方便保证时间符合题意
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;

const int MAX=55;
char map[MAX][MAX];
bool visited[MAX][MAX][1024+10];
int move[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int val[12];
int sx,sy;
int n,w,h,l,m;
int ans,sum;
struct node
{
	int x,y,sta,val,tim;
};

void bfs()
{
	queue<node>qq;
	node a,qa;
	int i;
	a.x=sx,a.y=sy,a.sta=0,a.val=0,a.tim=l;
	qq.push(a);
	while(!qq.empty())
	{
		qa=qq.front(),qq.pop();
		for(i=0;i<4;i++)
		{
			a.x=qa.x+move[i][0],a.y=qa.y+move[i][1],a.sta=qa.sta,a.val=qa.val,a.tim=qa.tim-1;
			if(!(a.x>0&&a.x<=h&&a.y>0&&a.y<=w&&map[a.x][a.y]!='*'))
				continue;
			if(a.tim<0)
				return ;
			else if(a.val==sum)
			{
				ans=sum;
				return ;
			}
			if(map[a.x][a.y]>='A'&&map[a.x][a.y]<='J')
			{
				int ch=map[a.x][a.y]-'A'+1;
				a.sta=a.sta|(1<<ch);
				if(!visited[a.x][a.y][a.sta])
				{
					visited[a.x][a.y][a.sta]=true;
					a.val+=val[ch];
					qq.push(a);
				}
			}
			else if((map[a.x][a.y]=='.'||map[a.x][a.y]=='@')&&!visited[a.x][a.y][a.sta])
			{
				visited[a.x][a.y][a.sta]=true;
				qq.push(a);
			}
			else if(map[a.x][a.y]=='<'&&!visited[a.x][a.y][a.sta])
			{
				visited[a.x][a.y][a.sta]=true;
				qq.push(a);
				if(a.val>ans)
					ans=a.val;
			}
		}
	}
}

int main()
{
	int i,j,tag;
//	freopen("in.txt","r",stdin);
	scanf("%d",&n);
	tag=1;
	while(n--)
	{
		sum=0;
		scanf("%d%d%d%d",&w,&h,&l,&m);
		for(i=1;i<=m;i++)
			scanf("%d",&val[i]),sum+=val[i];
		for(i=1;i<=m;i++)
			scanf("%d",&val[i]),sum+=val[i];
        for(i=1;i<=h;i++)
		{
            scanf("%s",map[i]+1);
            for(j=1;j<=m;j++)
			{
                if(map[i][j]=='@')
				{
					sx=i,sy=j;
				}
            }
        }
		memset(visited,0,sizeof(visited));
		printf("Case %d:\n",tag++);
		ans=-1;
		bfs();
		if(ans!=-1)
			printf("The best score is %d.\n",ans);
		else
			printf("Impossible\n");
		if(n)
			printf("\n");
	}
	return 0;
}

你可能感兴趣的:(搜索,图论)