uva201 Squares 记录

2024-06-09 21:38
文章标签 记录 squares uva201

本文主要是介绍uva201 Squares 记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

uva201 Squares 题解


 Squares 

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.)

tex2html_wrap_inline240

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 ofn2 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 #12 square (s) of size 1
1 square (s) of size 2**********************************Problem #2No completed squares can be found.


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
const int N=20;
int h[N][N];
int v[N][N];
int Gnum;
void judge(int nsize,int n){int i,j,k;int num=0;if (nsize>n){return;}for ( i = 1; i+nsize <= n; ++i){for ( j = 1; j+nsize <= n ; ++j){int x=0;while(x<nsize){if (!h[i][j+x]||!h[i+nsize][j+x]){break;}if (!v[i+x][j]||!v[i+x][j+nsize]){break;}x++;}if (x>=nsize){num++;Gnum++;}}}if (num){printf("%d square (s) of size %d\n",num,nsize);}judge(nsize+1,n);
}
int main()
{int n,m,i;int T=0;while(cin>>n){T++;Gnum=0;if (T!=1){printf("\n**********************************\n\n");}int m;cin>>m;memset(h,0,sizeof(h));memset(v,0,sizeof(v));for ( i = 0; i < m; ++i){char ch;int x,y;cin>>ch>>x>>y;if (ch=='H'){h[x][y]=1;}else if(ch=='V'){v[y][x]=1;}}printf("Problem #%d\n\n",T);//printfcheck(n);judge(1,n);if (Gnum==0){printf("No completed squares can be found.\n");}}return 0;
}


这道题有几个需要注意之处:
1.要求size从小到大排列,运用递归可以完成
2.要求××××号分隔
3.全局的判断是否存在和局部判断存在,分别要输出不同的结果。所以我用了Gnum记录全部数量,局部变量num记录局部数量
4.最坑爹之处,H和V字符的x,y坐标是反的。需要注意一下
5.
核心判断代码为以下:
void judge(int nsize,int n){int i,j,k;int num=0;if (nsize>n){return;}for ( i = 1; i+nsize <= n; ++i){for ( j = 1; j+nsize <= n ; ++j){int x=0;while(x<nsize){if (!h[i][j+x]||!h[i+nsize][j+x]){break;}if (!v[i+x][j]||!v[i+x][j+nsize]){break;}x++;}if (x>=nsize){num++;Gnum++;}}}if (num){printf("%d square (s) of size %d\n",num,nsize);}judge(nsize+1,n);
}

这篇关于uva201 Squares 记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1046368

相关文章

apache的commons-pool2原理与使用实践记录

《apache的commons-pool2原理与使用实践记录》ApacheCommonsPool2是一个高效的对象池化框架,通过复用昂贵资源(如数据库连接、线程、网络连接)优化系统性能,这篇文章主... 目录一、核心原理与组件二、使用步骤详解(以数据库连接池为例)三、高级配置与优化四、典型应用场景五、注意事

SpringBoot实现文件记录日志及日志文件自动归档和压缩

《SpringBoot实现文件记录日志及日志文件自动归档和压缩》Logback是Java日志框架,通过Logger收集日志并经Appender输出至控制台、文件等,SpringBoot配置logbac... 目录1、什么是Logback2、SpringBoot实现文件记录日志,日志文件自动归档和压缩2.1、

qtcreater配置opencv遇到的坑及实践记录

《qtcreater配置opencv遇到的坑及实践记录》我配置opencv不管是按照网上的教程还是deepseek发现都有些问题,下面是我的配置方法以及实践成功的心得,感兴趣的朋友跟随小编一起看看吧... 目录电脑环境下载环境变量配置qmake加入外部库测试配置我配置opencv不管是按照网上的教程还是de

使用nohup和--remove-source-files在后台运行rsync并记录日志方式

《使用nohup和--remove-source-files在后台运行rsync并记录日志方式》:本文主要介绍使用nohup和--remove-source-files在后台运行rsync并记录日... 目录一、什么是 --remove-source-files?二、示例命令三、命令详解1. nohup2.

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

在Spring Boot中浅尝内存泄漏的实战记录

《在SpringBoot中浅尝内存泄漏的实战记录》本文给大家分享在SpringBoot中浅尝内存泄漏的实战记录,结合实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录使用静态集合持有对象引用,阻止GC回收关键点:可执行代码:验证:1,运行程序(启动时添加JVM参数限制堆大小):2,访问 htt

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE