USACO 2.1 The Castle(DFS)

感觉代码大体写的还行,就是BUG太多了。。。特别注意输出的优先级,然后了

 1 /*

 2  ID: cuizhe

 3  LANG: C++

 4  TASK: castle

 5  */

 6 #include <cstdio>

 7 #include <cstring>

 8 #include <cmath>

 9 using namespace std;

10 int p[60][61],o[60][60][5],key[61][61],num,n,m;

11 int f[3501];

12 int a[4] = {0,-1,0,1};

13 int b[4] = {-1,0,1,0};

14 void judge(int x,int y,int sum)

15 {

16     int i;

17     for(i = 0; i <= 3; i ++)

18     {

19         if((1<<i)&sum)

20             o[x][y][i] = 1;

21     }

22 }

23 void dfs(int x,int y)

24 {

25     int i;

26     key[x][y] = num;

27     for(i = 0; i <= 3; i ++)

28     {

29         if(x+a[i]<=n&&x+a[i]>=1&&y+b[i]<=m&&y+b[i]>=1&&!key[x+a[i]][y+b[i]]&&!o[x][y][i])

30             dfs(x+a[i],y+b[i]);

31     }

32 }

33 int main()

34 {

35     int i,j,k,t,r,c,d,ans;

36     freopen("castle.in","r",stdin);

37     freopen("castle.out","w",stdout);

38     scanf("%d%d",&m,&n);

39     for(i = 1; i <= n; i ++)

40     {

41         for(j = 1; j <= m; j ++)

42         {

43             scanf("%d",&p[i][j]);

44             judge(i,j,p[i][j]);

45         }

46     }

47     num = 1;

48     for(i = 1; i <= n; i ++)

49     {

50         for(j = 1; j <= m; j ++)

51         {

52             if(!key[i][j])

53             {

54                 dfs(i,j);

55                 num ++;

56             }

57         }

58     }

59     printf("%d\n",num-1);

60     for(i = 1; i <= n; i ++)

61     {

62         for(j = 1; j <= m; j ++)

63         {

64             f[key[i][j]]++;

65         }

66     }

67     t = 1;

68     for(i = 1; i <= num-1; i ++)

69     {

70         if(t < f[i])

71             t = f[i];

72     }

73     printf("%d\n",t);

74     ans = t;

75     for(j = 1; j <= m; j ++)//优先级

76     {

77         for(i = n; i >= 1; i --)

78         {

79             for(k = 1; k <= 2; k ++)

80             {

81                 if(i+a[k]<=n&&i+a[k]>=1&&j+b[k]>=1&&j+b[k]<=m&&key[i][j]!=key[i+a[k]][j+b[k]])

82                 {

83                     if(ans < f[key[i][j]]+f[key[i+a[k]][j+b[k]]])

84                     {

85                         ans = f[key[i][j]]+f[key[i+a[k]][j+b[k]]];

86                         r = i;

87                         c = j;

88                         d = k;

89                     }

90                 }

91             }

92         }

93     }

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

95     printf("%d %d ",r,c);

96     if(d == 1) printf("N\n");

97     else printf("E\n");

98     return 0;

99 }

 

犯各种很2的错误。。

你可能感兴趣的:(USACO)