题目就不写了:大体题意就是给你一个划有横线和竖线的区域,判断其中有几个不同边长的正方形,(两个相邻的点间的距离为1)
思路分析:这个题刚开始看的时候有点迷,毕竟很多肉眼可以看出来的东西要用计算机语言写出来不是件容易事(比如简单地两个数的加法.....),好了,言归正传,这个题其实可以用两个不同的二维数组来分别存储节点的横边和竖边的情况,(但是要注意这里有一个陷阱,在储存竖边时它的XY坐标要跟储存横边时相反,可以想象一下二维数组遍历的时候,都是横着遍历的,没有竖着遍历的情况,所以要注意这个地方,或者是表示竖着的线,要与横着的线相反,即xy表示竖线,那么yx表示竖线,因为遍历方式相反);
AC代码:
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string.h>
#include <cstdio>
#include <string>
#include <vector>
#define maxm 100
using namespace std;
int v[10][10];
int h[10][10];
int n,m;
class oper
{
public:
char ch;
int x,y;
};
bool test(int x,int y,int len)
{
for(int i=x; i<(x+len);i++)
{
if(v[y][i]==0)
return false;
if(v[y+len][i]==0)//这个地方要特别注意,储存的竖线的坐标与储存横线的坐标相反,因为二维素组在遍历时不可能“竖着”遍历
return false;
}
for(int i=y; i<(y+len);i++)
{
if(h[x][i]==0)
return false;
if(h[x+len][i]==0)
return false;
}
return true;
}
int squarelen(int len)
{
int sum=0;
for(int i=1; i<=(n-len);i++)
for(int j=1; j<=(n-len);j++)
if(test(i,j,len))
sum++;
return sum;
}
int main()
{
int t=0;
while(cin>>n&&n>=2)
{ cin>>m;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
v[i][j]=0;
h[i][j]=0;
}
while(m--)
{
oper p;
cin>>p.ch>>p.x>>p.y;
if(p.ch=='H')
{
h[p.x][p.y]=1;
}
if(p.ch=='V')
{
v[p.x][p.y]=1;
}
}
if(t)
cout<<'\n'<<"**********************************"<<endl<<endl;
cout<<"Problem #"<<++t<<endl<<endl;
int flag=0;
for(int i=1;i<n; i++)
{
int k=squarelen(i);
if(k)
{
cout<<k<<" square (s) of size "<<i<<endl;
flag=1;
}
}
if(!flag)
cout<<"No completed squares can be found."<<endl;
}
return 0;
}