UVa 201 Squares
来源:互联网 发布:spring boot 传递数组 编辑:程序博客网 时间:2024/04/29 07:28
Squares
Time Limit: 3000MS
Memory Limit: Unknown 64bit IO Format: %lld & %llu
Description
A children’s board game consists of a square array of dots that contains lines connecting some of the pairs of adjacent dots. One part of the game requires that the players count the number of squares of certain sizes that are formed by these lines. For example, in the figure shown below, there are 3 squares-2 of size 1 and 1 of size 2. (The “size” of a square is the number of lines segments required to form a side.)
Your problem is to write a program that automates the process of counting all the possible squares.
Input
The input file represents a series of game boards. Each board consists of a description of a square array of n2 dots (where 2 <= n <= 9) and some interconnecting horizontal and vertical lines. A record for a single board with n2 dots and m interconnecting lines is formatted as follows:
Line 1: n the number of dots in a single row or column of the array
Line 2: m the number of interconnecting lines
Each of the next m lines are of one of two types:
H i j indicates a horizontal line in row i which connects
the dot in column j to the one to its right in column j + 1
or
V i j indicates a vertical line in column i which connects
the dot in row j to the one below in row j + 1
Information for each line begins in column 1. The end of input is indicated by end-of-file. The first record of the sample input below represents the board of the square above.
Output
For each record, label the corresponding output with “Problem #1”, “Problem #2”, and so forth. Output for a record consists of the number of squares of each size on the board, from the smallest to the largest. lf no squares of any size exist, your program should print an appropriate message indicating so. Separate output for successive input records by a line of asterisks between two blank lines, like in the sample below.
Sample Input
4
16
H 1 1
H 1 3
H 2 1
H 2 2
H 2 3
H 3 2
H 4 2
H 4 3
V 1 1
V 2 1
V 2 2
V 2 3
V 3 2
V 4 1
V 4 2
V 4 3
2
3
H 1 1
H 2 1
V 2 1
Sample Output
Problem #1
2 square (s) of size 1
1 square (s) of size 2
Problem #2
No completed squares can be found.
分析:
可以先找点再找边,也可以反过来。 因为题目要求不同边长升序输出,故按边长来搜索。 找一个边长k,逐次判断每个点,再输出。
代码:
#include <cstdio>#include <cstring>#include <cstdlib>const int MAX_N = 10;int V[MAX_N][MAX_N], H[MAX_N][MAX_N];int n, count;int check(int x, int y, int len) { for (int i = 0; i < len; i++) { if (y + i >= n || !H[x][y + i]) return 0; if (y + i >= n || x + len > n || !H[x + len][y + i]) return 0; if (x + i >= n || !V[x + i][y]) return 0; if (x + i >= n || y + len > n || !V[x + i][y + len]) return 0; } return 1;}void solve() { printf("Problem #%d\n\n", count + 1); int sum = 0, tot = 0; for (int k = 1; k < n; k++) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (check(i, j, k)) sum++; tot += sum; if (sum) printf("%d square (s) of size %d\n", sum, k); sum = 0; } if (tot == 0) puts("No completed squares can be found.");}int main() {#ifndef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);#endif // LOCAL count = 0; while (~scanf("%d", &n)) { if (count) puts("\n**********************************\n"); int a, b, t; char c; memset(H, 0, sizeof(H)); memset(V, 0, sizeof(V)); scanf("%d", &t); for (int i = 1; i <= t; i++) { getchar(); scanf("%c %d %d", &c, &a, &b); // printf("%c %d %d\n", c, a, b); if (c == 'H') H[a][b] = 1; else V[b][a] = 1; } solve(); count++; } // fclose(stdin); // fclose(stdout); return 0;}
- UVA-201Squares
- UVa 201 - Squares
- UVa 201 - Squares
- UVa 201 - Squares
- UVA-201 Squares
- UVA - 201 Squares
- Uva 201 Squares
- UVa-201Squares
- UVa 201 Squares
- UVA - 201 Squares
- UVA 201 Squares
- UVa 201 Squares 枚举
- UVa 201 Squares
- UVA 201 Squares
- UVa 201 Squares
- UVA - 201-Squares
- UVA - 201 Squares
- uva 201 Squares
- springmvc 有查询条件的数据 下载到excel
- Scala-2 - 5 - Lecture 1.5 - Example_ square roots with Newton_'s method (11_25)
- Nexus入门指南(图文)
- ZOJ-3770(Ranking System)
- Android Manifest.xml详解
- UVa 201 Squares
- 移动端实现 iphone android 小图标 文字排版
- RadioButton记录
- display:table用法
- 实现MethodBeforeAdvice接口问题
- Android控件之列表视图ListView与适配器用法
- arm CPSR/SPSR寄存器的访问、修改
- OD-字符串条件断点
- Unity学习笔记——ScrollView的日常