这个题目很好,变形的题目也很多。简单DP。
1 /* 1510 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 105 7 8 char map[MAXN][MAXN]; 9 int dp[MAXN][MAXN]; 10 11 int min(int a, int b) { 12 return a<b ? a:b; 13 } 14 15 int main() { 16 int n; 17 int i, j, k, tmp; 18 int sum; 19 20 #ifndef ONLINE_JUDGE 21 freopen("data.in", "r", stdin); 22 #endif 23 24 while (scanf("%d", &n) != EOF) { 25 for (i=1; i<=n; ++i) 26 scanf("%s", map[i]+1); 27 28 sum = 0; 29 for (i=1; i<=n; ++i) { 30 for (j=1; j<=n; ++j) { 31 if (map[i][j] == ‘#‘) { 32 dp[i][j] = 0; 33 } else { 34 dp[i][j] = dp[i-1][j] + 1; 35 tmp = dp[i][j]; 36 sum += tmp; 37 for (k=j-1; k>0; --k) { 38 tmp = min(tmp, dp[i][k]); 39 sum += tmp; 40 } 41 } 42 } 43 } 44 printf("%d\n", sum); 45 } 46 47 return 0; 48 }
时间: 2024-11-08 21:26:22