HDU 4328 Cut the cake(DP)

题目链接

没早发现这个DP,一直纠结各种图论题,蛋疼。。无奈水平太菜,最后两个小时都没搞出,本来想开10个标记数组的。。。。搞到最后半小时,发现同颜色的不会算,暴力吧。。。然后有个小细节没搞好,计算的时候。WA了,无奈还有10分钟,唉,已无力,查数据。。吃完饭,查出数据。。。过了。。。200+的DP,写的纠结。。。这次又是被虐了。。。QC只出了一个线段树模版,这个没做出,不应该啊,感觉数据应该不会很强,不会乱搞啊。。。

  1 #include <stdio.h>

  2 #include <string.h>

  3 int rr[1001][1001],rc[1001][1001],br[1001][1001],bc[1001][1001],r[1001][1001],c[1001][1001];

  4 char p[1001][1001];

  5 int main()

  6 {

  7     int t,i,j,k,n,m,max,min,num = 0;

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

  9     while(t--)

 10     {

 11         num ++;

 12         memset(rr,0,sizeof(rr));

 13         memset(rc,0,sizeof(rc));

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

 15         memset(bc,0,sizeof(bc));

 16         memset(r,0,sizeof(r));

 17         memset(c,0,sizeof(c));

 18         scanf("%d%d%*c",&n,&m);

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

 20             gets(p[i]);

 21         r[0][0] = 1;

 22         c[0][0] = 1;

 23         if(p[0][0] == 'R')

 24         {

 25             rr[0][0] = 1;

 26             rc[0][0] = 1;

 27         }

 28         else

 29         {

 30             br[0][0] = 1;

 31             bc[0][0] = 1;

 32         }

 33         for(i = 1; i <= n-1; i ++)

 34         {

 35             if(p[i][0] == 'R')

 36             {

 37                 rr[i][0] = rr[i-1][0]+1;

 38                 rc[i][0] = 1;

 39             }

 40             else

 41             {

 42                 rr[i][0] = 0;

 43                 rc[i][0] = 0;

 44             }

 45             if(p[i][0] == 'B')

 46             {

 47                 br[i][0] = br[i-1][0]+1;

 48                 bc[i][0] = 1;

 49             }

 50             else

 51             {

 52                 br[i][0] = 0;

 53                 bc[i][0] = 0;

 54             }

 55             if(p[i][0] != p[i-1][0])

 56             {

 57                 r[i][0] = r[i-1][0]+1;

 58                 c[i][0] = 1;

 59             }

 60             else

 61             {

 62                 r[i][0] = 1;

 63                 c[i][0] = 1;

 64             }

 65         }

 66         for(i = 1; i <= m-1; i ++)

 67         {

 68             if(p[0][i] == 'R')

 69             {

 70                 rc[0][i] = rc[0][i-1]+1;

 71                 rr[0][i] = 1;

 72             }

 73             else

 74             {

 75                 rc[0][i] = 0;

 76                 rr[0][i] = 0;

 77             }

 78             if(p[0][i] == 'B')

 79             {

 80                 bc[0][i] = bc[0][i-1]+1;

 81                 br[0][i] = 1;

 82             }

 83             else

 84             {

 85                 bc[0][i] = 0;

 86                 br[0][i] = 0;

 87             }

 88             if(p[0][i] != p[0][i-1])

 89             {

 90                 c[0][i] = c[0][i-1]+1;

 91                 r[0][i] = 1;

 92             }

 93             else

 94             {

 95                 c[0][i] = 1;

 96                 r[0][i] = 1;

 97             }

 98         }

 99         for(i = 1; i <= n-1; i ++)

100         {

101             for(j = 1; j <= m-1; j ++)

102             {

103                 if(p[i][j] == 'R')

104                 {

105                     rr[i][j] = rr[i-1][j]+1;

106                     rc[i][j] = rc[i][j-1]+1;

107                     br[i][j] = 0;

108                     bc[i][j] = 0;

109                 }

110                 else if(p[i][j] == 'B')

111                 {

112                     br[i][j] = br[i-1][j]+1;

113                     bc[i][j] = bc[i][j-1]+1;

114                     rr[i][j] = 0;

115                     rc[i][j] = 0;

116                 }

117                 if(p[i][j] != p[i-1][j-1])

118                 {

119                     r[i][j] = 1;

120                     c[i][j] = 1;

121                 }

122                 else

123                 {

124                     if(p[i][j] != p[i-1][j]&&p[i][j] != p[i][j-1])

125                     {

126                         r[i][j] = r[i-1][j]+1;

127                         c[i][j] = c[i][j-1]+1;

128                     }

129                     else if(p[i][j] != p[i-1][j]&&p[i][j] == p[i][j-1])

130                     {

131                         r[i][j] = r[i-1][j]+1;

132                         c[i][j] = 1;

133                     }

134                     else if(p[i][j] == p[i-1][j]&&p[i][j] != p[i][j-1])

135                     {

136                         r[i][j] = 1;

137                         c[i][j] = c[i][j-1]+1;

138                     }

139                     else

140                     {

141                         r[i][j] = 1;

142                         c[i][j] = 1;

143                     }

144                 }

145             }

146         }

147         max = 0;

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

149         {

150             for(j = 0; j <= m-1; j ++)

151             {

152                 if(max < 2*(br[i][j]+bc[i][j]))

153                 {

154                     min = br[i][j];

155                     k = 1;

156                     if(max < 2*(k+min))

157                         max = 2*(k+min);

158                     for(k = 2; k <= bc[i][j]; k ++)

159                     {

160                         if(min > br[i][j-k+1])

161                             min = br[i][j-k+1];

162                         if(max < 2*(k+min))

163                         {

164                             max = 2*(k+min);

165                         }

166                     }

167                 }

168                  if(max < 2*(rr[i][j]+rc[i][j]))

169                 {

170                     min = rr[i][j];

171                     k = 1;

172                     if(max < 2*(k+min))

173                         max = 2*(k+min);

174                     for(k = 2; k <= rc[i][j]; k ++)

175                     {

176                         if(min > rr[i][j-k+1])

177                             min = rr[i][j-k+1];

178                         if(max < 2*(k+min))

179                         {

180                             max = 2*(k+min);

181                         }

182                     }

183                 }

184                 min = r[i][j];

185                 if(min > c[i][j])

186                     min = c[i][j];

187                 if(max < 4*min)

188                     max = 4*min;

189             }

190         }

191         printf("Case #%d: %d\n",num,max);

192     }

193     return 0;

194 }

 

 

你可能感兴趣的:(HDU)