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个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...
随机推荐
- Nifi组件脚本开发—ExecuteScript 使用指南(二)
Part 2 - FlowFile I/O 和 Error Handling flow File的IO NiFi 的 Flow files 由两个主要部件组成:attributes 和 content ...
- iOS拍照定制之AVCapturePhotoOutput
问题 领导安排任务,写个拍照功能,界面跟系统拍照有点出入 拍完照片,底部显示已拍照片,有个拍照上限[在此不论] 点击已拍照片,可以预览.放大缩小查看 思路 系统拍照肯定不行了,只能定制,没提是否拍照禁 ...
- 按照阿里巴巴规范创建Java线程池
前言 Executors Executors 是一个Java中的工具类.提供工厂方法来创建不同类型的线程池. 常用方法: 1.newSingleThreadExecutor 介绍:创建一个单线程的 ...
- (数据科学学习手札109)Python+Dash快速web应用开发——静态部件篇(中)
本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...
- Docker daemon socket权限不足
一.概述 普通用户执行命令:docker ps报错,具体信息如下: docker: Got permission denied while trying to connect to the Docke ...
- 微信小程序一周时间表
<view class="dateView"> <image class="dateLeft" bindtap="prevWeek& ...
- IDEA中部署servlet
配置和不是servlet 第一种方法:(两种方法不能混用,使用第一种方法后,尽量删除第二种方法的注解方式) 使用xml文件配置: name没有什么特别的,就是需要统一即可.url-pattern中的地 ...
- CentOS7 安装 MySQL Cluster 7.6.7
引用自:http://lemonlone.com/posts/mysql-ndb-cluster-install/ 仅做备份和配置文件更改 1.先在VMware中安装 CentOS-7-x86_64- ...
- .zip爆破
.zip爆破 Python的优化问题 Python在计算密集型任务方面没有明显的多线程优化,多线程更加适合用于处理I/O密集型任务(如网络请求).爆破任务使用顺序执行即可. 编写Python脚本 一个 ...
- MyBatis(十一):MyBatis架构流程浅析
架构分层 我们将MyBatis架构分为三层,分别为接口层.数据处理层和框架支撑层 接口层:提供外部接口调用的API,使用端通过这些API来操作数据库,接口层收到请求后会调用数据处理层完成具体的数据处理 ...