Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

擦,我明白了,第一个下标没越界,后面两个越界了,囧啊。。。

Posted by woLaiDaJiangYou at 2012-03-09 20:45:17 on Problem 2964 and last updated at 2012-03-09 21:19:25
In Reply To:太诡异了,数组开dp[2][105][105]会RE,开dp[3][105][105]则AC,我保证数组下标没越界 Posted by:woLaiDaJiangYou at 2012-03-09 20:40:50
这个才是正确的代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;

#define N 105

int w,h;
char city[N][N];
int dp[2][N][N];

void DP()
{
	int m,i,j,k,nsteps=w+h-2;
	if(city[0][0]=='*') dp[0][0][0]=1;
	else dp[0][0][0]=0;
	for(k=1;k<=nsteps;k++)
	{
		for(i=0;i<=k && i<h;i++)
		{
			if(k-i>=w) continue;
			for(j=0;j<=k && j<h;j++)
			{
				if(k-j>=w) continue;
				if(city[i][k-i]=='#' || city[j][k-j]=='#')
				{
					dp[1][i][j]=-1;
					continue;
				}
				m=-1;
				if(i<=k-1 && j<=k-1) m=max(m,dp[0][i][j]);
				if(j<=k-1 && i-1>=0) m=max(m,dp[0][i-1][j]);
				if(i<=k-1 && j-1>=0) m=max(m,dp[0][i][j-1]);
				if(i-1>=0 && j-1>=0) m=max(m,dp[0][i-1][j-1]);
				if(m==-1)
				{
					dp[1][i][j]=-1;
					continue;
				}
				dp[1][i][j]=m;
				if(city[i][k-i]=='*') dp[1][i][j]++;
				if(i!=j && city[j][k-j]=='*') dp[1][i][j]++;
			}
		}
		for(i=0;i<=k && i<h;i++)
		{
			if(k-i>=w) continue;
			for(j=0;j<=k && j<h;j++)
			{
				if(k-j>=w) continue;
				dp[0][i][j]=dp[1][i][j];
			}
		}
	}
}

int main()
{
	int cases,i;
	scanf("%d",&cases);
	while(cases--)
	{
		scanf("%d%d",&w,&h);
		for(i=0;i<h;i++)
			scanf("%s",city[i]);
		DP();
		printf("%d\n",dp[1][h-1][h-1]);
	}
return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator