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

相关文章

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

将sqlserver数据迁移到mysql的详细步骤记录

《将sqlserver数据迁移到mysql的详细步骤记录》:本文主要介绍将SQLServer数据迁移到MySQL的步骤,包括导出数据、转换数据格式和导入数据,通过示例和工具说明,帮助大家顺利完成... 目录前言一、导出SQL Server 数据二、转换数据格式为mysql兼容格式三、导入数据到MySQL数据

关于rpc长连接与短连接的思考记录

《关于rpc长连接与短连接的思考记录》文章总结了RPC项目中长连接和短连接的处理方式,包括RPC和HTTP的长连接与短连接的区别、TCP的保活机制、客户端与服务器的连接模式及其利弊分析,文章强调了在实... 目录rpc项目中的长连接与短连接的思考什么是rpc项目中的长连接和短连接与tcp和http的长连接短

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

Servlet中配置和使用过滤器的步骤记录

《Servlet中配置和使用过滤器的步骤记录》:本文主要介绍在Servlet中配置和使用过滤器的方法,包括创建过滤器类、配置过滤器以及在Web应用中使用过滤器等步骤,文中通过代码介绍的非常详细,需... 目录创建过滤器类配置过滤器使用过滤器总结在Servlet中配置和使用过滤器主要包括创建过滤器类、配置过滤

正则表达式高级应用与性能优化记录

《正则表达式高级应用与性能优化记录》本文介绍了正则表达式的高级应用和性能优化技巧,包括文本拆分、合并、XML/HTML解析、数据分析、以及性能优化方法,通过这些技巧,可以更高效地利用正则表达式进行复杂... 目录第6章:正则表达式的高级应用6.1 模式匹配与文本处理6.1.1 文本拆分6.1.2 文本合并6

python与QT联合的详细步骤记录

《python与QT联合的详细步骤记录》:本文主要介绍python与QT联合的详细步骤,文章还展示了如何在Python中调用QT的.ui文件来实现GUI界面,并介绍了多窗口的应用,文中通过代码介绍... 目录一、文章简介二、安装pyqt5三、GUI页面设计四、python的使用python文件创建pytho

usaco 1.2 Palindromic Squares(进制转化)

考察进制转化 注意一些细节就可以了 直接上代码: /*ID: who jayLANG: C++TASK: palsquare*/#include<stdio.h>int x[20],xlen,y[20],ylen,B;void change(int n){int m;m=n;xlen=0;while(m){x[++xlen]=m%B;m/=B;}m=n*n;ylen=0;whi

Node.js学习记录(二)

目录 一、express 1、初识express 2、安装express 3、创建并启动web服务器 4、监听 GET&POST 请求、响应内容给客户端 5、获取URL中携带的查询参数 6、获取URL中动态参数 7、静态资源托管 二、工具nodemon 三、express路由 1、express中路由 2、路由的匹配 3、路由模块化 4、路由模块添加前缀 四、中间件