纯暴力水题,拿一个结构体存一下每个点的连接情况就行了。
原题:
ac代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#define DEBUG
using namespace std;
typedef struct
{int face[4];//上下左右
}Squares;
Squares board[11][11];
int num[12];
int cnt=1;
void init(int n)
{memset(board,0,sizeof(board));memset(num,0,sizeof(num));
}
void Hor(int xr,int xc)
{board[xr][xc].face[3]=1;board[xr][xc+1].face[2]=1;
}
void Ver(int xr,int xc)
{board[xc][xr].face[1]=1;board[xc+1][xr].face[0]=1;
}
void find(int r,int c,int n)
{int size;for(size=1;size<=min(n-r,n-c);size++){int flag=1;int i,j;for(i=r;i<=r+size;i++){if(i==r){if(!board[i][c].face[1]||!board[i][c+size].face[1]){flag=0;break;}}else if(i==r+size){if(!board[i][c].face[0]||!board[i][c+size].face[0]){flag=0;break;}}else{if(!board[i][c].face[0]||!board[i][c].face[1]||!board[i][c+size].face[0]||!board[i][c+size].face[1]){flag=0;break;}}}for(i=c;i<=c+size;i++){if(i==c){if(!board[r][i].face[3]||!board[r+size][i].face[3]){flag=0;break;}}else if(i==c+size){if(!board[r][i].face[2]||!board[r+size][i].face[2]){flag=0;break;}}else{if(!board[r][i].face[2]||!board[r][i].face[3]||!board[r+size][i].face[2]||!board[r+size][i].face[3]){flag=0;break;}}}if(flag){num[size]++;}}
}
int main(void)
{std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);int n,m;int flag_c=0;while(cin>>n>>m){if(!flag_c){flag_c=!flag_c;}else{cout<<endl<<"**********************************"<<endl<<endl;}char c;int xr,xc;init(n);while(m--){cin>>c>>xr>>xc;if(c=='H'){Hor(xr,xc);}else{Ver(xr,xc);}}int i,j;for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(board[i][j].face[1]&&board[i][j].face[3]){find(i,j,n);}}}cout<<"Problem #"<<cnt++<<endl<<endl;int flag=1;for(i=1;i<=n;i++){if(num[i]){flag=0;cout<<num[i]<<" "<<"square (s) of size"<<" "<<i<<endl;}}if(flag){cout<<"No completed squares can be found."<<endl;}}return 0;
}