您的位置:首页 > 其它

HDU 2782 The Worm Turns 暴力深搜DFS

2014-11-14 22:33 316 查看

The Worm Turns

Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 676 Accepted Submission(s): 256


Problem Description
Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divided into cells, and each cell contains either food or a rock. Winston wanders aimlessly for a while until he gets hungry; then he immediately
eats the food in his cell, chooses one of the four directions (north, south, east, or west) and crawls in a straight line for as long as he can see food in the cell in front of him. If he sees a rock directly ahead of him, or sees a cell where he has already
eaten the food, or sees an edge of the rectangular patch, he turns left or right and once again travels as far as he can in a straight line, eating food. He never revisits a cell. After some time he reaches a point where he can go no further so Winston stops,
burps and takes a nap.

For instance, suppose Winston wakes up in the following patch of earth (X's represent stones, all other cells contain food):



If Winston starts eating in row 0, column 3, he might pursue the following path (numbers represent order of visitation):



In this case, he chose his path very wisely: every piece of food got eaten. Your task is to help Winston determine where he should begin eating so that his path will visit as many food cells as possible.

Input
Input will consist of multiple test cases. Each test case begins with two positive integers, m and n , defining the number of rows and columns of the patch of earth. Rows and columns are numbered starting at 0, as in the figures above.
Following these is a non-negative integer r indicating the number of rocks, followed by a list of 2r integers denoting the row and column number of each rock. The last test case is followed by a pair of zeros. This should not be processed. The value m×n will
not exceed 625.

Output
For each test case, print the test case number (beginning with 1), followed by four values:

amount row column direction
where amount is the maximum number of pieces of food that Winston is able to eat, (row, column) is the starting location of a path that enables Winston to consume this much food, and direction is one of E, N, S, W, indicating the
initial direction in which Winston starts to move along this path. If there is more than one starting location, choose the one that is lexicographically least in terms of row and column numbers. If there are optimal paths with the same starting location and
different starting directions, choose the first valid one in the list E, N, S, W. Assume there is always at least one piece of food adjacent to Winston's initial position.

Sample Input
5 5 
3 
0 4 3 1 3 2 
0 0


Sample Output
Case 1: 22 0 3 W


/*
HDU 2782 暴力深搜DFS 

刚开始用check函数的 超时,然后不用过了
函数调用是要压栈的,要先保存环境变量,然后执行函数,完成后还要恢复,肯定要多些时间 
*/
#include<iostream>
#include<stdio.h>
#include<string.h> 
using namespace std;
#define N 626
int map

,vis

;
int n,m,maxStep;
int sx,sy,sd,xt,yt,zt;
int dir[4][2]={0,1,-1,0,1,0,0,-1};
char direc[]={'E','N','S','W'};
//方向问题:x行相当于y轴 y列相当与x轴 
//上,北,y轴-1,x轴不变:-1,0 =>N
//左,西,x轴-1,y轴不变:0,-1 =>W 
 
bool check(int x,int y)
{
	if(x>=0&&x<m&&y>=0&&y<n&&!vis[x][y]&&!map[x][y])
		return true;
	return false;
}

void DFS(int a,int b,int d,int step)//坐标信息X,Y,D(方向),步数 
{
	int ex,ey,i;
	if(step>maxStep)
	{
		maxStep=step;
		sx=xt;sy=yt;sd=zt;//保存起始坐标信息 
	}
	ex=a+dir[d][0],ey=b+dir[d][1];
//	if(check(ex,ey))//  用check函数慢? 该方向可以走,继续深搜 
	if(ex>=0&&ex<m&&ey>=0&&ey<n&&!vis[ex][ey]&&!map[ex][ey])
	{
		vis[ex][ey]=1;
		DFS(ex,ey,d,step+1);
		vis[ex][ey]=0;
	}
	else
	{
		if(step==0) return;//第一步不许转向
		for(i=0;i<4;i++)
		{
			if(i==d) continue;
			ex=a+dir[i][0];
			ey=b+dir[i][1];
			if(ex>=0&&ex<m&&ey>=0&&ey<n&&!vis[ex][ey]&&!map[ex][ey])
			{
				vis[ex][ey]=1;
				DFS(ex,ey,i,step+1);
				vis[ex][ey]=0;
			}
		} 
	}
}
int main()
{
	int t,i,j,z,x,y,cas=1;
	
	//freopen("test.txt","r",stdin);
	while(scanf("%d%d",&m,&n),n||m)
	{
		memset(map,0,sizeof(map));
		scanf("%d",&t);
		for(i=0;i<t;i++)
		{
			scanf("%d%d",&x,&y);
			map[x][y]=1;
		}
		maxStep=0;
		memset(vis,0,sizeof(vis));
		for(i=0;i<m;i++)
			for(j=0;j<n;j++)
				for(z=0;z<4;z++)
				{
					if(!map[i][j])
					{
						xt=i;yt=j;zt=z;//记录起始坐标 方向 
						vis[i][j]=1;
						DFS(i,j,z,0);
						vis[i][j]=0;
					}
				}
		printf("Case %d: %d %d %d %c\n",cas++,maxStep+1,sx,sy,direc[sd]);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: