TLE:csu 1205 放石子游戏

表示第一次用dfs。。。

 1 # include <stdio.h>
2 # include <string.h>
3
4 char board[1001][1001];
5 char vis[1001];
6 int trace[1001];
7 int N, M, ans;
8
9 void dfs(int x, int y);
10 void special_dfs(int x, int y, int trace[]);
11
12 int main()
13 {
14 int i, j, x, y;
15
16 while (~scanf("%d%d", &N, &M))
17 {
18 ans = 0;
19 for (i = 0; i < M; ++i)
20 {
21 scanf("%d%d", &x, &y);
22 board[x][y] = 1;
23 }
24 for (i = 1; i <= N; ++i)
25 if (!vis[i] && board[1][i]) dfs(1, i);
26 if (ans == 1)
27 {
28 printf("So easy\n");
29 for (i = 1; i <= N; ++i)
30 if (!vis[i] && board[1][i]) special_dfs(1, i, trace);
31 }
32 else if (ans == 0) printf("You trick me\n");
33 else printf("Ask God for help\n");
34 memset(board, 0, sizeof(board));
35 memset(vis, 0, sizeof(vis));
36 memset(trace, 0, sizeof(trace));
37 }
38
39 return 0;
40 }
41
42 void dfs(int x, int y)
43 {
44 int i;
45 if (x == N) {++ans; return;}
46 vis[y] = 1;
47 for (i = 1; i <= N; ++i)
48 if (!vis[i] && board[x+1][i])
49 dfs(x+1, i);
50 vis[y] = 0;
51 }
52 void special_dfs(int x, int y, int trace[])
53 {
54 int i;
55 if (x == N)
56 {
57 trace[x] = y;
58 printf("%d", trace[1]);
59 for (i = 2; i <= N; ++i)
60 printf(" %d", trace[i]);
61 printf("\n");
62 return;
63 }
64 vis[y] = 1;
65 trace[x] = y;
66 for (i = 1; i <= N; ++i)
67 if (!vis[i] && board[x+1][i])
68 special_dfs(x+1, i, trace);
69 vis[y] = 0;
70 }

结果应该没错。。

posted on 2012-03-27 00:26  getgoing  阅读(231)  评论(0编辑  收藏  举报

导航