10913 - Walking on a Grid

1

2

3

 

5

4

 

 

 

1

2 
 3 
   
/*
这题要开4维数组,如果你只开3维数组请看上面的那两个表格,12345代表走的路。
如果开3维的他们的表示有可能是相同的,因为负数的原因也可能不相同。无论你先
走到哪一个状态,当你的程序走到另一个状态的时候,你的程序就会直接返回前一个状态的值。
这个值就有可能是错误的。
开四维数组d[i][j][k][l] i,j代表当前所在位置k代表之前已经走过多少负数,l代表
从哪个方向走进这个位置的。 
*/
#include <stdio.h>
#include <string.h>
#define maxn 80
long long n,ne,d[maxn][maxn][7][3],g[maxn][maxn],vis[maxn][maxn][7][3];
int dir[3][2]={0,-1,0,1,1,0};
long long max(long long  a,long long  b)
{
	if(a>b) return a;
	else return b;
}
long long dp(int a,int b,int c,int e)
{
	long long &res=d[a][b][c][e];
	long long &flag=vis[a][b][c][e];
	if(flag) return res;
	flag=1;res=-((long long)1<<60);
	if(c>ne) return res;
	if(a==n&&b==n) 
	{
		if(g[a][b]<0)
		c++;
		if(c<=ne)
		res=g[a][b];
		return res;
	}
	for(int i=0;i<3;i++)
	{
		int newr=a+dir[i][0];
		int neww=b+dir[i][1];
		int newc=c;
		if(g[a][b]<0) newc++;
		if(newr>=1&&newr<=n&&neww>=1&&neww<=n&&g[newr][neww]!=((long long)1<<60))
		{
			int t=g[a][b];
			g[a][b]=((long long)1<<60);
			long long temp=dp(newr,neww,newc,i);
			if(temp!=-((long long)1<<60))
			res=max(res,temp+t);
			g[a][b]=t;
		}
	}
	return res;
}
int main()
{
//	freopen("C:\\Users\\WT\\Desktop\\测试数据.txt","r",stdin);
	int tt=0;
	while(scanf("%lld%lld",&n,&ne)&&!(n==0&&ne==0))
	{
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		scanf("%lld",&g[i][j]);
		memset(d,0,sizeof(d));
		memset(vis,0,sizeof(vis));
		printf("Case %d: ",++tt);
		long long temp=dp(1,1,0,0);
		if(temp>-((long long)1<<60))
		printf("%lld\n",temp);
		else
		printf("impossible\n");				
	}
	return 0;
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值