Uva 201 - Squares (ACM/ICPC World Finals 1990)

A children’sboard game consists of a square array of dots that contains lines connectingsome of the pairs of adjacent dots. One part of the game requires that theplayers count the number of squares of certain sizes that are formed by theselines. For example, in the figure shown below, there are 3 squares — 2 of size1 and 1 of size 2. (The “size” of a square is the number of lines segmentsrequired to form a side.)
在这里插入图片描述
Your problem is to write a program thatautomates the process of counting all the possible squares.

Input

The input file representsa series of game boards. Each board consists of a description of a square arrayof n2 dots (where 2 ≤ n ≤ 9) and some interconnectinghorizontal and vertical lines. A record for a single board with n2 dots and m interconnecting lines is formatted asfollows:

Line 1: n thenumber of dots in a single row or column of the array

Line 2: m thenumber 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 indicatesa vertical line in column i whichconnects the dot in row j to the onebelow in row j + 1

Information for eachline begins in column 1. The end of input is indicated by end-of-file. Thefirst record of the sample input below represents the board of the squareabove.

Output

For each record, label the correspondingoutput with ‘Problem #1’,‘Problem #2’, and so forth. Outputfor a record consists of the number of squares of each size on the board, fromthe smallest to the largest. lf no squares of any size exist, your programshould print an appropriate message indicating so. Separate output for successiveinput records by a line of asterisks between two blank lines, like in thesample 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.

Tips

1. 因为2<=n<=9,所以可以将坐标系转化为一个二维矩阵,用图来表示;
2. 第一步将边长为1的连接(读入数据),第二步,将边最大化,如上图chess[12][32]=chess[12][42]=chess[22][42]=1
3. 然后遍历矩阵,判断有多少正方形
#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define read() freopen("input.txt","r",stdin);
#define write() freopen("output.txt","w",stdout);
using namespace std;
const int maxn = 110;
int chess[maxn][maxn];
int a[10];
int main() {
	read();write();
	int kase=0,n,m;
	while(~scanf("%d",&n)){
		if(kase) printf("\n**********************************\n\n");
		printf("Problem #%d\n\n",++kase);
		cin>>m;char ch;int v1,v2,flag=0;
		memset(chess,0,sizeof(chess));memset(a,0,sizeof(a));
		for( int i=0; i<m; i++ ){
			cin>>ch>>v1>>v2;
			if(ch=='H') chess[v1*10+v2][v1*10+v2+1]=chess[v1*10+v2+1][v1*10+v2]=1;
			else if(ch=='V') chess[v2*10+v1][(v2+1)*10+v1]=chess[(v2+1)*10+v1][v2*10+v1]=1;
		}	
		for( int i=1; i<=n; i++ ){
			for( int sta=1; sta<n; sta++ ){
				for( int end=sta+2; end<=n; end++ ){
					if(chess[i*10+sta][i*10+end-1]==1&&chess[i*10+end-1][i*10+end]==1) chess[i*10+sta][i*10+end]=1;
					if(chess[sta*10+i][(end-1)*10+i]==1&&chess[(end-1)*10+i][end*10+i]) chess[sta*10+i][end*10+i]=1;
				}
			}
		}
		for( int dis=1; dis<n; dis++ ){
			for( int i=1; i+dis<=n; i++ ){
				for( int j=1; j+dis<=n; j++ ){
					if(chess[i*10+j][i*10+j+dis]==1&&chess[i*10+j+dis][(i+dis)*10+j+dis]==1&&chess[i*10+j][(i+dis)*10+j]==1&&chess[(i+dis)*10+j][(i+dis)*10+j+dis]==1) a[dis]++;
				}
			}
		}
		for( int i=1; i<9; i++ ){
			if(a[i]){ printf("%d square (s) of size %d\n",a[i],i);flag=1;}
		}
		if(!flag) printf("No completed squares can be found.\n");
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43323172/article/details/89740111