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个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...
随机推荐
- 将springboot项目部署到服务器的tomcat中无法访问
第一步:让启动类继承SpringBootServletInitializer,并重写configure方法,关键代码如下 @SpringBootApplication public class MyS ...
- 开工大吉:TcaplusDB将持续为您提供可靠的数据服务
开工大吉 新的一年 新的开始 我们也带着新的心意 向您奔赴而来 在此,TcaplusDB祝广大客户朋友,开工大吉,2021,我们将一如既往地守护您的数据,继续做您最坚实的后盾. 作为专为游 ...
- 进位&&大数字符串处理
Have Fun with Numbers Notice that the number 123456789 is a 9-digit number consisting exactly the nu ...
- .NET测试断言工具Shouldly
.NET测试断言工具Shouldly .NET测试 Shouldly在GitHub的开源地址:https://github.com/shouldly/shouldly Shouldly的官方文档:ht ...
- linux开启FTP服务
目录 打开FTP服务 客户端链接时会出现的问题 打开FTP服务 先ping,查看网络是否联通 打开ssh服务 查看一些服务的状态 #查看ssh状态 service sshd status #防火墙的状 ...
- 182. 查找重复的电子邮箱 + group by + having
182. 查找重复的电子邮箱 LeetCode_MySql_182 题目描述 方法一:使用笛卡尔积 # Write your MySQL query statement below select di ...
- HDOJ-1160(最长上升子序列变形)
FatMouse's Speed HDOJ-1160 注意输出长度的时候不是输出dp[n] #include<bits/stdc++.h> using namespace std; con ...
- .NET CORE 3.1 MVC Log4net
1 引用包:Microsoft.Extensions.Logging.Log4Net.AspNetCore 2 ILoggerFactory loggerFactory 注册 public voi ...
- STL之string容器
string string封装了char*,管理这个字符串,是一个char*型的容器. string的相关操作 头文件 #include<string> string构造函数 string ...
- 在ASP.NET Core中用HttpClient(二)——发送POST, PUT和DELETE请求
在上一篇文章中,我们已经学习了如何在ASP.NET Core中使用HttpClient从Web API获取数据.此外,我们还学习了如何使用GetAsync方法和HttpRequestMessage类发 ...