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
Recommend
xhd | We have carefully selected several similar problems for you: 15001505150115061502
Statistic | Submit | Discuss | Note
没什么好说的,看代码。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#define N 200
using namespace std; char square[N][N];
int dir[][] = {, , , , , }; //只定义右,下,右下方向可直接避免重复。
int cnt; void cal(int k, int i, int j, int n, int I, int J) {
int flag = ;
I = I + dir[k][];
J = J + dir[k][];
if(I >= n || J >= n) return;
for(int e = i; e <= I; e++) { //通过对角两个坐标做出矩形循环判断是否全是white square。
for(int k = j; k <= J; k++) {
if(square[e][k] == '#') {
flag = ;
break;
}
}
}
if(flag == ) {
cnt++;
if(k == ) {
for(int e = ; e < ; e++) { //朝左下方向时应该下一步应该3个方向都走。
cal(e, i, j, n, I, J);
}
} else cal(k, i, j, n, I, J);
} else return;
} void Function(int n) {
for(int i = ; i < n; i++) {
for(int j = ; j < n; j++) {
if(square[i][j] == '.') {
cnt++;
for(int k = ; k < ; k++) {
cal(k, i, j, n, i, j);
}
}
}
}
} int main() {
int n;
char c;
while(scanf("%d", &n) != EOF) {
cnt = ;
memset(&square, , sizeof(square));
for(int i = ; i < n; i++) {
for(int j = ; j < n; j++)
cin >> square[i][j];
}
Function(n);
printf("%d\n", cnt);
}
return ;
}