HDU 4403 A very hard Aoshu problem(DFS)

题目链接

如此裸的题,竟然写了近1个小时,最近做题太少了。签到题都做的这么慢,而且写的代码真心没法看了。。。

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <iostream>

 4 using namespace std;

 5 #define LL __int64

 6 LL map[30][30];

 7 int ans,len,o[16];

 8 void dfs(int k,int step)

 9 {

10     LL x,y;

11     int z,i,j;

12     if(step == len)

13     {

14         if(k == 0)

15         return ;

16         z = 0;

17         j = 0;

18         x = y = 0;

19         for(i = 0; i <= len-1; i ++)

20         {

21             if(o[i] == 2&&!z)

22             {

23                 x += map[j][i];

24                 j = i+1;

25             }

26             else if(o[i] == 2&&z)

27             {

28                 y += map[j][i];

29                 j = i+1;

30             }

31             else if(o[i] == 3)

32             {

33                 z = 1;

34                 x += map[j][i];

35                 j = i+1;

36             }

37         }

38         if(x == y)

39         {

40             ans ++;

41         }

42         return ;

43     }

44     if(step == len-1)

45     {

46         o[step] = 2;

47         dfs(k,step+1);

48     }

49     else

50     {

51         o[step] = 1;

52         dfs(k,step+1);

53         o[step] = 2;

54         dfs(k,step+1);

55         if(k == 0)

56         {

57             o[step] = 3;

58             dfs(1,step+1);

59         }

60     }

61 }

62 int main()

63 {

64     char p[100];

65     int i,j;

66     while(scanf("%s",p)!=EOF)

67     {

68         ans = 0;

69         if(strcmp("END",p) == 0)break;

70         len = strlen(p);

71         memset(map,0,sizeof(map));

72         memset(o,0,sizeof(o));

73         for(i = 0; i <= len-1; i ++)

74         {

75             for(j = i; j <= len-1; j ++)

76             {

77                 if(i == j)

78                     map[i][j] = p[j]-'0';

79                 else

80                     map[i][j] = map[i][j-1]*10+p[j]-'0';

81             }

82         }

83         dfs(0,0);

84         printf("%d\n",ans);

85     }

86     return 0;

87 }

你可能感兴趣的:(HDU)