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

求助:用递归写的,用例已经通过,为什么老是报WA呀?请教牛人,万分感激!

Posted by pkwang at 2006-02-27 08:36:28 on Problem 1111
#include <iostream.h>


int image[30][30];
int record[30][30];
int per;
void perimeters(int x,int y,int x1,int y1);

int main()
{
	int i,j;
	int g,h;
	char a;
	int x1,y1,x2,y2;
	cin>>x1>>y1>>x2>>y2;
	while(x1 != 0 && y1 != 0 && x2 != 0 && y2 != 0)
	{
		per = 0;
		for(g = 0;g < 30;g ++)
			for(h = 0;h < 30;h ++)
				record[g][h] = 0;
		for(i = 1;i <= x1;i ++)
			for(j = 1;j <= y1;j ++)
			{
				cin>>a;
				if(a == 'X')
					image[i][j] = 1;
				else
					if(a == '.')
						image[i][j] = 0;
			}
		perimeters(x2,y2,x1,y1);
		cout<<per<<endl;
		cin>>x1>>y1>>x2>>y2;
	}
	return 0;
}

void perimeters(int x,int y,int x1,int y1)
{
	if(image[x][y] == 1 && record[x][y] == 0)
	{
		record[x][y] = 1;
		if(x + 1 <= x1)
		{
			if(image[x + 1][y] == 0)
				per ++;
			else
				perimeters(x + 1,y,x1,y1);
		}
		else
			per ++;
		if(x - 1 >= 1)
			if(image[x -1][y] == 0)
				per ++;
			else
				perimeters(x - 1,y,x1,y1);
		else
			per ++;

		if(y + 1 <= y1)
			if(image[x][y + 1] == 0)
				per ++;
			else 
				perimeters(x,y + 1,x1,y1);
		else 
			per ++;

		if(y - 1 >= 1)
			if(image[x][y - 1] == 0)
				per ++;
			else 
				perimeters(x,y-1,x1,y1);
		else 
			per ++;
		if(x - 1 >= 1 && y - 1 >= 1)
			perimeters(x - 1,y - 1,x1,y1);
		if(x - 1 >= 1 && y + 1 <= y1)
			perimeters(x - 1,y + 1,x1,y1);
		if(x + 1 <= x1 && y - 1 >= 1)
			perimeters(x + 1,y - 1,x1,y1);
		if(x + 1 <= x1 && y + 1 >= 1)
			perimeters(x + 1,y + 1,x1,y1);
	}
}

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