| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
DFS暴搜代码,找了好几个DFS的代码发现都是错误的……#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator