原题:
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
1 2
112233445566778899 998877665544332211Sample Output
2
.#
..
4
..#.
##. #
.#..
.#.#
解题思路:
- 循环遍历延伸,直接暴力检测(HOJ在这道题上的测试数据有点水)
代码:
#include<stdio.h>
#include <iostream>
using namespace std;
char W[101][101];
int SUM;
void Calculate(short N){
short i, j, k, l, temp;
for(i = 1; i <= N; i++)
for (j = 1; j <= N; j++){
temp = N;
if(W[i][j] == '#')continue;
SUM++;//此块为白块
for (k = j; k <= N; k++){
if(k != j){
if (W[i][k] == '#')break;
SUM++;
}
for (l = i + 1; l <= temp; l++){
if (W[l][k] == '#') {
temp = l - 1;
break;
}
SUM++;
}
}
}
}
int main(){
short N, i, j;
while (scanf("%d",&N)!=EOF)
{
memset(W, '0', sizeof(W));
SUM = 0;
for (i = 1; i <= N; i++)//行
for (j = 1; j <= N; j++)//列
cin >> W[i][j];//无需考虑清空缓存
Calculate(N);
printf("%d\n", SUM);
}
}