HDU1510 White rectangles

White Rectangles

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 964    Accepted Submission(s): 485

Problem Description

You are given a chessboard made up of N squares by N squares with equal size. Some of the squares are colored black, and the others are colored white. Please write a program to calculate the number of rectangles which are completely made up of white squares.

Input

There are multiple test cases. Each test case begins with an integer N (1 <= N <= 100), the board size. The following N lines, each with N characters, have only two valid character values:
# - (sharp) representing a black square;
. - (point) representing a white square.
Process to the end of file.

Output

For each test case in the input, your program must output the number of white rectangles, as shown in the sample output.

Sample Input

2 .# .. 4 ..#. ##.# .#.. .#.#

Sample Output

5 15

Author

JIANG, Ming

Source

ZOJ Monthly, January 2004

Recommend

xhd   |   We have carefully selected several similar problems for you:  15001505150115061502

Statistic | Submit | Discuss | Note

没什么好说的,看代码。

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<iostream>
 5 #define N 200
 6 using namespace std;
 7 
 8 char square[N][N];
 9 int dir[3][2] = {1, 0, 0, 1, 1, 1}; //只定义右,下,右下方向可直接避免重复。
10 int cnt;
11 
12 void cal(int k, int i, int j, int n, int I, int J) {
13     int flag = 0;
14     I = I + dir[k][0];
15     J = J + dir[k][1];
16     if(I >= n || J >= n) return;
17     for(int e = i; e <= I; e++) { //通过对角两个坐标做出矩形循环判断是否全是white square。
18         for(int k =  j; k <= J; k++) {
19             if(square[e][k] == '#') {
20                 flag = 1;
21                 break;
22             }
23         }
24     }
25     if(flag == 0) {
26         cnt++;
27         if(k == 2) {
28             for(int e = 0; e < 3; e++) { //朝左下方向时应该下一步应该3个方向都走。
29                 cal(e, i, j, n, I, J);
30             }
31         } else cal(k, i, j, n, I, J);
32     } else return;
33 }
34 
35 void Function(int n) {
36     for(int i = 0; i < n; i++) {
37         for(int j = 0; j < n; j++) {
38             if(square[i][j] == '.') {
39                 cnt++;
40                 for(int k = 0; k < 3; k++) {
41                     cal(k, i, j, n, i, j);
42                 }
43             }
44         }
45     }
46 }
47 
48 int main() {
49     int n;
50     char c;
51     while(scanf("%d", &n) != EOF) {
52         cnt = 0;
53         memset(&square, 0, sizeof(square));
54         for(int i = 0; i < n; i++) {
55             for(int j = 0; j < n; j++)
56                 cin >> square[i][j];
57         }
58         Function(n);
59         printf("%d\n", cnt);
60     }
61     return 0;
62 }

 

 

 

posted @ 2016-03-08 22:45  lemadmax  阅读(479)  评论(0编辑  收藏  举报