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 042775 at 2006-03-30 22:30:41 on Problem 1088
我用的是回溯法,但是总是出错,但我有找不出来,请大侠们帮忙看看,小弟不胜感激!程序如下:
#include<stdio.h>
#include<stdlib.h>
int sum=0,max=0;
int a[100][100];
void search(int m,int N);
int islegal(int m,int N,int i);
void myscanf(int N);
void search(int m,int N)
{
	if(m>=N*N)
	{
		if(sum>=max)
			max=sum;
	}
	else
	{
		search(m+1,N);
		if(islegal(m-1,N,0))
		{
			sum++;
			search(m-1,N);
			sum--;
		}
		if(islegal(m+N,N,1))
		{
			sum++;
			search(m+N,N);
			sum--;
		}
		if(islegal(m+1,N,2))
		{
			sum++;
			search(m+1,N);
			sum--;
		}
		if(islegal(m-N,N,3))
		{
			sum++;
			search(m-N,N);
			sum--;
		}
	}
}
int islegal(int m,int N,int i)
{
	int row,col;
	row=m/N;
	col=m%N;
	if(row<0||row>=N||col<0||col>=N)
		return 0;
	else 
		switch(i)
	{
		case 0: 
				if(col+1<N)
				{
					if(a[row][col]<a[row][col+1])
				    return 1;
				}
				else
				    return 0;
			        break;
        case 1:   if(row-1>=0)
				  {
					  if(a[row][col]<a[row-1][col])//*************
					  return 1;
				  }
				else
					return 0;
			        break;
        case 2: if(col-1>=0)
				{
					if(a[row][col]<a[row][col-1])
				    return 1;
				}
			    else
					return 0;
			        break;
        case 3: 
			if(row+1<N)
			{
				if(a[row][col]<a[row+1][col])
					return 1;
			}
			else
				return 0;
			break;
	}
	return 0;
}
void myscanf(int N)
{
	int i,j;
	for(i=0;i<N;i++)
		for(j=0;j<N;j++)
			scanf("%d",&a[i][j]);
}
void main()
{
	int N,M;
	scanf("%d%d",&M,&N);
	myscanf(N);
	search(0,N);
	printf("%d",max);
}

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