hpuoj 1668: Problem F Greedy Snake

1668: Problem F Greedy Snake

时间限制: 1 Sec   内存限制: 128 MB
提交: 13   解决: 7
[ 提交][ 状态][ 讨论版]

题目描述

(Standard Input / Standard Output)
    A greedy snake, Peter, walks on n x m grids. Once a time, he can only walks from a grid to a neighborhood grid. Here, we define two grids are neighborhood if and only if there is a common edge between them.
    In several grids, there are some apples. When Peter walks to a grid that has apples, he will pick up all the apples on that grid, and after that, the grid has no apples any more. Peter is so greedy that he never walks to a grid that has no apples. Once he goes home, he will store the apples that he picked. He wants to store apples as many as possible. So can you help him to calculate the maximum number of apples that Peter can pick? Notice that, once Peter has reached his home, he cannot walk away from his home anyway.

输入

    The input contains several test cases. The first line of the input contains a positive integer T < 11, denoting the number of test cases. Then T test cases follow.
    The first line of a test case contains two positive space-separated integers n, m (1 <= n, m <= 6), denoting the number of rows and columns respectively. Then followed by n lines, each contains m integers. A non-negative integer which is less than 20 means the number of apples on that grid, a negative integer “-1” means the Peter’s position and a negative integer “-2” means the position of Peter’s home. We promise that there are exactly one “-1” and one “-2” in the grids description. You can assume that in each test case, there are at most 30 grids that have non-zero apples and of course, there is no apple on Peter’s position, either Peter’s home.

输出

    For each test case, output an integer which means the maximum apples that Peter can store. Output “-1” if Peter can’t reach his home.

样例输入

1
4 4
7 0 4 18 
4 0 1 1 
15 7 11 -1 
0 12 -2 0 

样例输出

54
dfs:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<stack>
#include<algorithm>
#define max(a,b)(a>b?a:b)
using namespace std;
int map[7][7];
int visit[7][7];//标记 
int n,m;
int sum;//最多摘果数目 
int move[4][2]={0,1,0,-1,1,0,-1,0};
int startx,starty,endx,endy;//起点坐标 终点坐标 
int exist;//记录是否可以到达终点 
void getmap()
{
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			scanf("%d",&map[i][j]);
			if(map[i][j]==-1)
			{
				startx=i;
				starty=j;
			}
			if(map[i][j]==-2)
			{
				endx=i;
				endy=j;
			}
		}
	}
}
int judge(int x,int y)
{
	if(x>=0&&x<n&&y>=0&&y<m&&map[x][y])
	return 1;
	else
	return 0;
}
void dfs(int have,int x,int y)//have为当前已摘数目 
{
	int k;
	int next_x,next_y;//下一移动坐标
	if(x==endx&&y==endy)//到达终点 更新 
	{
		exist=1;
		sum=max(sum,have+2);
		return ;
	} 
	else
	{
		for(k=0;k<4;k++)
		{
			next_x=x+move[k][0];
			next_y=y+move[k][1];
			if(!visit[next_x][next_y]&&judge(next_x,next_y))
			{
				visit[next_x][next_y]=1;
				dfs(have+map[next_x][next_y],next_x,next_y);
				visit[next_x][next_y]=0;
			}
		}
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		getmap();
		memset(visit,0,sizeof(visit));
		visit[startx][starty]=1;
		sum=0;exist=0;
		dfs(0,startx,starty);
		if(exist)
		printf("%d\n",sum);
		else
		printf("-1\n"); 
	}
	return 0;
}



你可能感兴趣的:(hpuoj 1668: Problem F Greedy Snake)