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

我采用回朔加深度优先遍历求入度为0到指定点的走法的总和,为什么总是错呢,麻烦高人指点

Posted by jsjhoubo at 2007-07-20 15:36:40 on Problem 3272
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 5020
bool graph[max][max];
bool visited[max];
int sum=0;
bool in[max];
int n;
void backtrack(int t)
{
	int i;
	if(t==n)
	{
		sum++;
		return;
	}
	for(i=1;i<=n;i++)
	{
		if(graph[t][i] && !visited[i])
		{
			visited[i]=true;
			backtrack(i);
			visited[i]=false;
		}
	}
}
int main()
{
	int i,j,k,m;
	//FILE *fp;
	//fp=fopen("c:\\hb.txt","r");
    memset(graph,false,sizeof(graph));
	memset(in,true,sizeof(in));
	//fscanf(fp,"%d%d",&n,&m);
	scanf("%d%d",&n,&m);
	if(n==1)
		printf("0\n");    
	while(scanf("%d %d", &j, &k) !=EOF)
	{
		graph[j][k]=true;
		in[k]=false;
	}
	memset(visited,false,sizeof(visited));
	for(i=1;i<=n;i++)
		if(in[i])
		{
			visited[i]=true;		
			backtrack(i);
		}
	printf("%d\n",sum);
	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