小小明系列故事——游戏的烦恼
2013-11-03 16:46
323 查看
腾讯比赛的题目,试着做一下。用动态规划求以i, j为右下角,以1,1为左上角的矩形区域里面的*的个数,然后枚举所有可能的小矩形的右下角,用夹逼的方式求小矩形区域里面有多少*,时间复杂度为O(m*n)。
#include <stdio.h> char arr[2001][2001]; int dp[2001][2001]; void func(int m, int n, int x, int y) { int i, j, ii; int target_num, count; target_num = x*y; count = 0; for(i=0; i<=m; i++) { for(j=0; j<=n; j++) { if(i==0 || j== 0) { dp[i][j] = 0; } else { dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1]; if(arr[i][j] == '*') dp[i][j] ++; } if(i>=x && j>=y && arr[i][j]=='*') { if(dp[i][j] + dp[i-x][j-y] - dp[i-x][j] - dp[i][j-y] == target_num) count ++; } if(x == y) continue; if(i>=y && j>=x && arr[i][j]=='*') { if(dp[i][j] + dp[i-y][j-x] - dp[i-y][j] - dp[i][j-x] == target_num) count ++; } } } printf("%d\n", count); } int main(void) { int m, n; int x, y; int i; //freopen("input.dat", "r", stdin); while(1) { scanf("%d %d", &m, &n); if(!m && !n) break; scanf("%d %d", &x, &y); getchar(); for(i=1; i<=m; i++) { gets(arr[i]+1); } func(m, n, x, y); } return 0; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow