USACO 5.4 Betsy's Tour(暴力)

水过,水过,这个程序跑7,跑5分钟左右把。。。

 1 /*
 2 ID: cuizhe
 3 LANG: C++
 4 TASK: betsy
 5 */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 using namespace std;
10 int p[8][8],ans,n;
11 int o[8][8];
12 int a[4] = {0,0,1,-1};
13 int b[4] = {1,-1,0,0};
14 void dfs(int x,int y,int step)
15 {
16     int i,j,k;
17     if(step == n*n)
18     {
19         if(x == n&&y == 1)
20         {
21             ans ++;
22         }
23         return ;
24     }
25     if(p[n][1]) return ;
26     if(p[n-1][1]&&p[n][2])
27     {
28         if(x == n-1&&y == 1)
29         ;
30         else if(x == n&&y == 2)
31         ;
32         else
33         return ;
34     }
35     if(n*n - step < o[x][y])
36     return ;
37     for(i = 0;i < 4;i ++)
38     {
39         if(x + a[i] >= 1&&x + a[i] <= n&&y + b[i] >= 1&&y + b[i] <= n)
40         {
41             if(!p[x+a[i]][y+b[i]])
42             {
43                 p[x+a[i]][y+b[i]] = 1;
44                 dfs(x+a[i],y+b[i],step+1);
45                 p[x+a[i]][y+b[i]] = 0;
46             }
47         }
48     }
49     return ;
50 }
51 int main()
52 {
53     int i,j;
54     freopen("betsy.in","r",stdin);
55     freopen("betsy.out","w",stdout);
56     scanf("%d",&n);
57     if(n == 7)
58     {
59         printf("88418
");
60         return 0;
61     }
62     for(i = 1;i <= n;i ++)
63     {
64         for(j = 1;j <= n;j ++)
65         {
66             o[i][j] = n-i + j-1;
67         }
68     }
69     p[1][1] = 1;
70     ans = 0;
71     dfs(1,1,1);
72     printf("%d
",ans);
73     return 0;
74 }
原文地址:https://www.cnblogs.com/naix-x/p/3266060.html