Squares UVA - 201
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 n 2 dots (where 2 ≤ n ≤ 9) and some interconnecting horizontal and vertical lines. A record for a single board with n 2 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.
HINT
暴力破解的,使用连两个数组分别储存竖边和横边,然后判断每一个可能的正方形。使用了4个for循环来来判断,最外层循环是正方形的大小,最大到n-1。紧接着两个循环对每一个可能的顶点来判断。最里面的才是判断标准,
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int result(int arrh[][10], int arrv[][10], int n);
int main()
{
int n, m ,p, q;
int problem = 1;
while (scanf("%d %d", &n, &m) != EOF)
{
getchar();
int arrh[10][10] = { 0 };
int arrv[10][10] = { 0 };
char ch;
for (int i = 0;i < m;i++)
{
scanf("%c %d %d", &ch, &p, &q);getchar();
if (ch == 'H')arrh[p][q]=1;
else arrv[q][p] = 1;
}
if (problem != 1)printf("\n**********************************\n\n");
printf("Problem #%d\n\n", problem++);
if (result(arrh, arrv, n)==0)printf("No completed squares can be found.\n");
}
}
int result(int arrh[][10], int arrv[][10], int n)
{
int flag = 0;
for (int i = 1;i < n;i++)
{
int sum = 0;
for (int j = 1;j <= n - i;j++)
{
for (int k = 1;k <= n - i;k++)
{
int flag1 = 0;
for (int s = 0;s<i;s++)
{
if (!arrh[j][k + s] || !arrh[j + i][k + s] || !arrv[j + s][k] || !arrv[j + s][k + i])
{
flag1 = 1;break;
}
}
if (!flag1) { sum++;flag = 1; }
}
}
if (sum)printf("%d square (s) of size %d\n", sum, i);
}
return flag;
}
Squares UVA - 201的更多相关文章
- 【每日一题】Squares UVA - 201 暴力+输出坑 + 读文件模板
题意 给你n*n的图,让你数正方形 题解:暴力for每个点,对于每个点从它出发顺时针走一个正方形.走完就ans[i]++; 坑:多输了一行******,然后在那里手摸样例,无限debug orz #d ...
- UVa 201 Squares
题意: 给出这样一个图,求一共有多少个大小不同或位置不同的正方形. 分析: 这种题一看就有思路,最开始的想法就是枚举正方形的位置,需要二重循环,枚举边长一重循环,判断是否为正方形又需要一重循环,复杂度 ...
- 【UVA】201 Squares(模拟)
题目 题目 分析 记录一下再预处理一下. 代码 #include <bits/stdc++.h> int main() { int t=0,s,n; while(scanf ...
- UVa 1453 - Squares 旋转卡壳求凸包直径
旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...
- UVa 1643 Angle and Squares
题意: 如图,有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 分析: 直观上来看,当这n个正方形的对角线在一条直线上时,封闭区域的面积最大.( ...
- uva 1453 - Squares
旋转卡壳算法: 直接在这个上面粘的模板 主要用途:用于求凸包的直径.宽度,两个不相交凸包间的最大距离和最小距离··· 这题就是求凸包的直径 #include <cstdio> #inclu ...
- UVA 4728 Squares(凸包+旋转卡壳)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...
- UVA 12113 Overlapping Squares
题意: 总共有6个2*2的正方形,判断是否能够成所给的形状. 思路: 一个正方形总共有9种摆放方式,对于整个地图来说摆放方式总共有2的9次方种摆放方式.然后将地图用9*5的数组表示,正方形的位置用其8 ...
- UVa 1643 Angle and Squares (计算几何)
题意:有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...
随机推荐
- HashMap扩容后是否需要rehash?
需要,因为要重新计算旧数组元素在新数组地址.HashMap在JDK1.8中的rehash算法(也就是扩容后重新为里面的键值对寻址的算法)进行优化.hash寻址算法是 index =(n - 1) &a ...
- git相关问题
1.git查看远程分支更新到本地 git clone 项目地址,示例如下: git clone https://github.com/zhongyushi-git/vue-test.git 在拉取时, ...
- PCA主成分分析的矩阵原理
[前言]主成分分析(PCA)实现一般有两种,一种是对于方阵用特征值分解去实现的,一种是对于不是方阵的用奇异值(SVD)分解去实现的. 一.特征值 特征值很好理解,特征值和特征向量代表了一个矩阵最鲜明的 ...
- Pyqt5——表格中隐藏的控件(Model/View/Delegate)
需求:在TableView表格中点击单元格可以实现编辑功能.性别由LineEdite控件编辑,年龄由spinBox控件编辑. 实现:(1)使用Qt的model-view模式生成表格视图. (2) ...
- 解决springboot项目打成jar包部署到linux服务器后上传图片无法访问的问题
前言:目前大三,自己也在学习和摸索的阶段.在和学校的同学一起做前后端分离项目的时候,我们发现将后端打包成jar,然后部署到服务器中通过java -jar xxx.jar运行项目以后,项目中存在文件上传 ...
- Python中OS对目录的操作以及引用
路径的获取 对当前目录的获取 1 path = os.getcwd() 2 print("获取到的当前目录是:({})".format(path)) 获取当前文件所在的绝对路径 i ...
- Chrome OS超便捷安装指南
Chrome OS是一款Google开发的基于PC的操作系统. Google Chrome OS是一款基于Linux的开源操作系统.Google在自己的官方博客表示,初期,这一操作系统将定位于上网本. ...
- 靶场练习-Sqli-labs通关记录(1-4关)
0x00 实验环境 本地:Win 10 靶场:sqli-labs(共65关,每日一关) 0x02 通关记录 简介:一天一关! (1)第一关: 简单的 ...
- 我与FreeBSD的故事之一
记得还是那些无聊的日子,群里有网友称Linux只能玩WPS,我表示质疑,并通过百度这个搜索引擎搜索到了Ubuntu Kylin,即由湖南的国防科技大学与Ubuntu社区合作并由其主导的Ubuntu麒麟 ...
- LZZY高级语言程序设计之输入秒数并用时钟的方式表达
import java.util.Scanner;public class MQ5 { public static void main(String[] args) { Scanner sc = ne ...