UVA-201 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.)
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
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int v[10][10],h[10][10];
int sj[10];
int test=0;
int judge(int l,int x,int y)//遍历每个点,每种长度
{
	for(int i=x;i<x+l;i++)
	{
		if(v[i][y]==0||v[i][y+l]==0)//注意v[][]上的点要倒着看
			return 0;
	}
	for(int j=y;j<y+l;j++)
	{
		if(h[x][j]==0||h[x+l][j]==0)
			return 0;
	}
	return 1;
}
int main(int argc, char const *argv[])
{
	int a,b;
	int N,M;
	int flag;
	char ch;
	while(~scanf("%d%d",&N,&M))
	{
	   
		memset(v,0,sizeof(v));
		memset(h,0,sizeof(h));
		memset(sj,0,sizeof(sj));
		flag=0;
	    for(int i=1;i<=M;i++)
	    {
	    	cin>>ch>>a>>b;
	    	if('H'==ch)
	    	    h[a][b]=1;
	        else
	        	v[b][a]=1;
	    }
	    for(int l=1;l<N;l++)//正方形边长
	    {   <i,j>为正方形左上角的端点
	    	for(int i=1;i<=N-l;i++)
	    	{
	    		for(int j=1;j<=N-l;j++)
	    		{
	    			if(judge(l,i,j))
	    				sj[l]++;
	    		}
	    	}
	    }
	    if(test!=0)
	    {
	    	printf("\n**********************************\n\n");
	    }
	    printf("Problem #%d\n\n",++test);
	    for(int i=1;i<N;i++)
	    {
	    	if(sj[i]!=0)
	       {
              flag=1;
	          printf("%d square (s) of size %d\n",sj[i],i);
	       }
	    }
	    if(0==flag)
	    	printf("No completed squares can be found.\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
协同过滤算法(Collaborative Filtering)是一种经典的推荐算法,其基本原理是“协同大家的反馈、评价和意见,一起对海量的信息进行过滤,从中筛选出用户可能感兴趣的信息”。它主要依赖于用户和物品之间的行为关系进行推荐。 协同过滤算法主要分为两类: 基于物品的协同过滤算法:给用户推荐与他之前喜欢的物品相似的物品。 基于用户的协同过滤算法:给用户推荐与他兴趣相似的用户喜欢的物品。 协同过滤算法的优点包括: 无需事先对商品或用户进行分类或标注,适用于各种类型的数据。 算法简单易懂,容易实现和部署。 推荐结果准确性较高,能够为用户提供个性化的推荐服务。 然而,协同过滤算法也存在一些缺点: 对数据量和数据质量要求较高,需要大量的历史数据和较高的数据质量。 容易受到“冷启动”问题的影响,即对新用户或新商品的推荐效果较差。 存在“同质化”问题,即推荐结果容易出现重复或相似的情况。 协同过滤算法在多个场景中有广泛的应用,如电商推荐系统、社交网络推荐和视频推荐系统等。在这些场景中,协同过滤算法可以根据用户的历史行为数据,推荐与用户兴趣相似的商品、用户或内容,从而提高用户的购买转化率、活跃度和社交体验。 未来,协同过滤算法的发展方向可能是结合其他推荐算法形成混合推荐系统,以充分发挥各算法的优势。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值