POJ 3083 Children of the Candy Corn

题目链接

纠结中,DFS统计写错了,到最后也没对拍出数据cha了,看了一下别人的写法,过了。

  1 #include <cstdio>

  2 #include <cstring>

  3 #include <cmath>

  4 #include <queue>

  5 #include <iostream>

  6 using namespace std;

  7 char p[51][51];

  8 int o[51][51],n,m,sr,sc,er,ec,num,z;

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

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

 11 void cl()

 12 {

 13     int i,j;

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

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

 16     {

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

 18         {

 19             if(p[i-1][j-1] == '#')

 20                 o[i][j] = 1;

 21             else if(p[i-1][j-1] == 'S')

 22             {

 23                 sr = i;

 24                 sc = j;

 25             }

 26             else if(p[i-1][j-1] == 'E')

 27             {

 28                 er = i;

 29                 ec = j;

 30             }

 31         }

 32     }

 33 }

 34 int dfs(int x,int y,int di)

 35 {

 36     int i,j;

 37     if(x == er&&y == ec)

 38     {

 39        return 1;

 40     }

 41     for(j = 0; j <= 3; j ++)

 42     {

 43         i = (j+di-1)%4;

 44         if(i < 0) i += 4;

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

 46         {

 47             return 1+dfs(x+a[i],y+b[i],i);

 48         }

 49     }

 50     return 0;

 51 }

 52 int dfs1(int x,int y,int di)

 53 {

 54     int i,j;

 55     if(x == er&&y == ec)

 56     {

 57        return 1;

 58     }

 59     for(j = 0; j <= 3; j ++)

 60     {

 61         i = (di+1-j)%4;

 62         if(i < 0) i += 4;

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

 64         {

 65             return 1+dfs1(x+a[i],y+b[i],i);

 66         }

 67     }

 68     return 0;

 69 }

 70 void bfs()

 71 {

 72     int quer[2001],quec[2001];

 73     int str,end,i,j,x,y,k;

 74     str = end = 1;

 75     quer[1] = sr;

 76     quec[1] = sc;

 77     while(str <= end)

 78     {

 79         if(o[er][ec]) break;

 80         j = 1;

 81         for(k = str; k <= end; k ++)

 82         {

 83             x = quer[k];

 84             y = quec[k];

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

 86             {

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

 88                 {

 89                     o[x+a[i]][y+b[i]] = 1;

 90                     quer[end+j] = x+a[i];

 91                     quec[end+j] = y+b[i];

 92                     j ++;

 93                 }

 94             }

 95         }

 96         str = end+1;

 97         end = end+j-1;

 98         num ++;

 99     }

100 }

101 int main()

102 {

103     int i,t,ans;

104     scanf("%d",&t);

105     while(t--)

106     {

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

108         for(i = 0; i <= n-1; i ++)

109             scanf("%s",p[i]);

110         cl();

111         o[sr][sc] = 1;

112         if(sr == 1)

113         ans = dfs(sr+1,sc,2);

114         else if(sr == n)

115         ans = dfs(sr-1,sc,0);

116         else if(sc == 1)

117         ans = dfs(sr,sc+1,1);

118         else if(sc == m)

119         ans = dfs(sr,sc-1,3);

120         printf("%d",ans+1);

121         cl();

122         o[sr][sc] = 1;

123         if(sr == 1)

124         ans = dfs1(sr+1,sc,2);

125         else if(sr == n)

126         ans = dfs1(sr-1,sc,0);

127         else if(sc == 1)

128         ans = dfs1(sr,sc+1,1);

129         else if(sc == m)

130         ans = dfs1(sr,sc-1,3);

131         printf(" %d",ans+1);

132         cl();

133         num = 1;

134         bfs();

135         printf(" %d\n",num);

136     }

137     return 0;

138 }

你可能感兴趣的:(children)