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

DFS暴搜代码,找了好几个DFS的代码发现都是错误的……

Posted by ly50247 at 2009-07-24 11:01:09 on Problem 1129
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

const int MAX = 50;
int minn;
int map[MAX][MAX], n, col[MAX];

bool check(int d, int color)
{
	for (int i = 0; i < n; i++)
		if (map[i][d] == 1 && color == col[i] && i != d)	//与涂有颜色i的有边相连 
			return false;
	return true;
}

void DFS(int d)
{
	if (d == n)
	{
		int tmax = *max_element(col, col + MAX);
		if (minn > tmax)
			minn = tmax;
		return;
	}
	
	for (int t=d; t < n; t++)
	{
		for (int i = 1; i <= 4; i++)
		{
			if (check(t, i))	//能用i颜色涂结点d 
			{
				col[t] = i;
				DFS(t + 1);
			}
		}
	}
}

int main()
{
	char a[50];
	while (scanf("%d", &n), n)
	{
		memset(map, 0, sizeof(map));
		memset(col, 0, sizeof(col));
		
		for (int i = 0; i < n; i++)
		{
			scanf("%s", a);
			for (int j=2; a[j] != 0; j++)
			{
				map[i][a[j] - 'A'] = 1;
			}
		}
		
		minn = 4;
		col[0] = 1;
		DFS(1);
		
		if (minn == 1)
			printf("1 channel needed.\n");
		else
			printf("%d channels needed.\n", minn);
	}
	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