DFS HDOJ 2614 Beat
1 /*
2 题意:处理完i问题后去处理j问题,要满足a[i][j] <= a[j][k],问最多能有多少问题可以解决
3 DFS简单题:以每次处理的问题作为过程(即行数),最多能解决n个问题,相同的问题(行数)不再考虑
4 详细解释:http://blog.csdn.net/libin56842/article/details/41909429
5 */
6 #include <cstdio>
7 #include <iostream>
8 #include <cstring>
9 #include <algorithm>
10 #include <cmath>
11 #include <string>
12 using namespace std;
13
14 const int MAXN = 15 + 10;
15 const int INF = 0x3f3f3f3f;
16 int a[MAXN][MAXN];
17 int used[MAXN];
18 int n;
19 int ans;
20
21 void DFS(int s, int cost, int cnt)
22 {
23 ans = max (ans, cnt);
24 if (cnt == n) return ;
25 for (int k=1; k<=n; ++k)
26 {
27 if (a[s][k] >= cost && !used[k])
28 {
29 used[k] = 1;
30 DFS (k, a[s][k], cnt+1);
31 used[k] = 0;
32 }
33 }
34 }
35
36 int main(void) //HDOJ 2614 Beat
37 {
38 //freopen ("N.in", "r", stdin);
39
40 while (~scanf ("%d", &n))
41 {
42 memset (used, 0, sizeof (used));
43 for (int i=1; i<=n; ++i)
44 {
45 for (int j=1; j<=n; ++j)
46 {
47 scanf ("%d", &a[i][j]);
48 }
49 }
50
51 ans = -1; used[1] = 1;
52 for (int i=2; i<=n; ++i)
53 {
54 used[i] = 1;
55 DFS (i, a[1][i], 2);
56 used[i] = 0;
57 }
58
59 printf ("%d\n", ans);
60 }
61
62 return 0;
63 }
编译人生,运行世界!