Codeforces 432E Square Tiling greedy __codeforces

Source: Internet
Author: User

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.