Title: Given a n∗m n*m matrix, the requirement is to cover with square bricks so that any two square bricks with the same color are not adjacent and the dictionary order is the smallest
Enumerate each piece of bricks, if this position is empty, fill in the dictionary order the smallest bricks, and then the side of the long one to expand
If the current right position can be filled in the dictionary order smaller bricks, it does not expand
Whether or not the judge can expand and expand
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M
namespace Std;
int m,n;
Char Ans[m][m];
bool Able (int x,int Y,char c) {static const int dx[]={1,-1,0,0};
static const int DY[]={0,0,1,-1};
int i;
for (i=0;i<4;i++) if (ans[x+dx[i]][y+dy[i]]==c) return false;
return true;
} void Extend (int x,int y) {int i,j;
for (i= ' A '; i<= ' Z '; i++) if (Able (x,y,i)) {ans[x][y]=i;
Break to (i=2;x+i-1<=m&&y+i-1<=n;i++) {for (j= ' A '; j<ans[x][y];j++) if (Able (x,y+i-
1,J)) return;
for (j=x;j<=x+i-1;j++) if (ans[j][y+i-1]) return;
for (j=y;j<=y+i-1;j++) if (Ans[x+i-1][j]) return;
if (Ans[x-1][y+i-1]==ans[x][y]) return;
if (Ans[x+i-1][y-1]==ans[x][y]) return; For (j=x;j<=x+i-1;j++) if (Ans[j][y+i]==ans[x][y]) return;
for (j=y;j<=y+i-1;j++) if (Ans[x+i][j]==ans[x][y]) return;
for (j=x;j<=x+i-1;j++) ans[j][y+i-1]=ans[x][y];
for (j=y;j<=y+i-1;j++) ans[x+i-1][j]=ans[x][y];
int main () {int i,j;
cin>>m>>n;
for (i=1;i<=m;i++) for (j=1;j<=n;j++) if (!ans[i][j]) Extend (I,J);
For (i=1;i<=m;i++) puts (ans[i]+1);
return 0; }