UVa 201 Squares (习题4-2)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/decision2016/article/details/83650372

时隔一年之后继续搞这些东西

然后开始复习以前的内容,把以前高中没写的题好好的搞一下。。

紫书第四章的习题以前就直接没写过,然后昨天调象棋调了一天还是疯狂WA,看到网上正解有400行。。真jier可怕

然后先把那题放一放,主要还是调用函数比较多,改天再重新写一下

Squares这个题以前还是想的太复杂了。今天上去教室的途中突然想到这么一个n^3的算法

然后看了一下数据范围2<=n<=9,简直就是纯暴力啊,以前我搞什么鬼。。

下早自习回寝室开始写,然后发现长度为2的检测不了,仔细一看是V的储存问题

紫书上说V x y 表示(x,y)(x+1,y)的直线

但是原题样例中出现了V 4 1,且n = 4,是不会有点(5,1)的,而在图中表示出了V 1 4

那么只要把V的x和y交换一下就行了

检测的时候用一个函数检测两个点之间的线段是否是连通的即可,依次检测4条边,全部连通的话就是一个正方形,返回true进行计数 

题目地址(vjudge):https://vjudge.net/problem/UVA-201

代码:

//Decision's template
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<set>
using namespace std;

#define DP_maxn 16
#define maxn 1000000+10
#define INF 1000000007
#define mod 1000000007
#define mst(s,k) memset(s,k,sizeof(s))

typedef long long ll;

struct Edge{
    int from,to,dist;
    Edge(int u,int v,int d):from(u),to(v),dist(d){}
};

/*-------------------------------template End--------------------------------*/

int m,n;
int h[10][10],v[10][10],ans=0,now = 0;
char c;

bool check_v(int x_1,int x_2,int y){          //检测x方向两点是否连通
	for(int i = x_1;i<x_2;i++){
		if(v[i][y]) continue;
		else return false;
	}
	return true;
}

bool check_h(int y_1,int y_2,int x){          //检测y方向两点是否连通
	for(int i = y_1;i<y_2;i++){
		if(h[x][i]) continue;
		else return false;
	}
	return true;
}

bool check(int x,int y,int l)
{
	if(x+l<=n&&y+l<=n){
		if(check_v(x,x+l,y)&&check_v(x,x+l,y+l)&&check_h(y,y+l,x)&&check_h(y,y+l,x+l)) return true;
		else return false;
	}
	else return false;
}

int main()
{
	while(cin>>m>>n){
		mst(h,0);
		mst(v,0);
		int flag = 0;
		int t_x,t_y;
		now++;
		if(now!=1) cout<<endl<<"**********************************"<<endl<<endl;
		cout<<"Problem #"<<now<<endl<<endl;
		for(int i = 1;i<=n;i++){
			cin>>c>>t_x>>t_y;;
			if(c=='H') h[t_x][t_y] = 1;
			else v[t_y][t_x] = 1; 
		}
		for(int i = 1;i<=m;i++){
			ans = 0;
			for(int j = 1;j<=m;j++){
				for(int k = 1;k<=m;k++){
					if(check(j,k,i)) ans++;
				}
			}
			if(ans>0&&flag == 0) flag = 1;
			if(ans>0) cout<<ans<<" square (s) of size "<<i<<endl;
		}
		if(!flag) cout<<"No completed squares can be found."<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/decision2016/article/details/83650372
201