HDU1510 White rectangles
2016-03-08 22:45
260 查看
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
没什么好说的,看代码。
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #define N 200 using namespace std; char square ; int dir[3][2] = {1, 0, 0, 1, 1, 1}; //只定义右,下,右下方向可直接避免重复。 int cnt; void cal(int k, int i, int j, int n, int I, int J) { int flag = 0; I = I + dir[k][0]; J = J + dir[k][1]; 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 = 1; break; } } } if(flag == 0) { cnt++; if(k == 2) { for(int e = 0; e < 3; 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 = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(square[i][j] == '.') { cnt++; for(int k = 0; k < 3; k++) { cal(k, i, j, n, i, j); } } } } } int main() { int n; char c; while(scanf("%d", &n) != EOF) { cnt = 0; memset(&square, 0, sizeof(square)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) cin >> square[i][j]; } Function(n); printf("%d\n", cnt); } return 0; }
相关文章推荐
- linux3种驱动初步理解
- Hibernate 忽略非数据库字段属性
- vi或者vim 屏幕编辑器,是最基本的编辑器
- ZOJ 1076 Gene Assembly
- 提高C++程序运行效率的10个简单方法
- [BZOJ1797][Ahoi2009]Mincut 最小割 做题笔记
- visio2013画图时两条直线交叉 如何让它不弯曲
- appium V1.5.x变化
- numbers-everyone-should-know
- 数据归一化的两种常用方法
- android项目的文件介绍
- touch 修改文件时间戳,或者新建一个不存在的文件 - 副本
- sleep和wait的区别
- 集群管理和分布式任务 Apache helix
- JavaScript 中的原型声明和用法总结
- pwd 查看”当前工作目录“的完整路径
- 前端渲染:服务器渲染 or 客户端渲染
- O2O、C2C、B2B、B2C的区别
- 基于POI的导入导出Excel
- Bind 视图