program solved playlinkr.net level3 7.11 in 1.5sec.

· · 科技·工程

#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define TIAO cout << fixed << setprecision(15)
using namespace std;
/*
by qqqaaazzz
好的,linknet.

目标:将网格图铺满,并满足一个条件。
1 5 1
1...1

4 4 2
1##1
.2..
..2.
....

5 10 3
1..2......
..1.......
.3........
...2......
....3.....

4 4 3
.31.
..2.
3...
2.1.

5554443334
5114744434
5144777434
514111#434
5111##2444
5588##2266
858#888226
8588858626
8555558626
8888888666

*/
int n,m,k;
char s[50][50];
int a[50][50];
int fx[]={0,0,1,-1};
int fy[]={1,-1,0,0};
struct Node{
    vector<int> col;//这一圈的颜色
    vector<int> ci;//这一圈的次数
    vector<int> more;
    bool operator < (const Node &x) const{
        if(col==x.col) return ci<x.ci;
        else return col<x.col;
    }
    bool operator == (const Node &x) const{
        return (col==x.col&&ci==x.ci);
    } 
};

signed main()
{
    cin >> n >> m >> k;
    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            cin >> s[i][j];
        }
    }
    vector<Node> my;
    Node q ;

    for (int i=0;i<m;i++) q.col.push_back(0),q.ci.push_back(0);

    my.push_back(q);

    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            vector<Node> nxt;
            for (auto y : my){
                for (int t=0;t<=k;t++){
                    if(s[i][j]=='.');//ok
                    else if(s[i][j]=='#'&&t!=0) continue;
                    else if(s[i][j]>='1'&&s[i][j]<='9'&&s[i][j]-'0'!=t) continue;

                    if(y.col[j]==0);//ok
                    else if(y.ci[j]+(t==y.col[j])!=2)continue;//no
                    Node to=y;

                    to.ci[j] = (j==0? 0:(to.col[j-1]==t))+(to.col[j]==t)+(s[i][j]>='1'&&s[i][j]<='9');
                    to.col[j]=t;
                    to.more.push_back(t);
                    if(j) to.ci[j-1]+=(to.col[j-1]==t);

                    bool ok=1;
                    for (int u=0;u<m;u++){
                        int lin = 1+(u==j&&u!=m-1);
                        if(to.col[u]){
                            if(to.ci[u]>2||to.ci[u]+lin<2) ok=0;
                            if(u!=j&&u+1!=j&&u+1<m&&to.ci[u]==1&&to.ci[u+1]==1&&to.col[u]==to.col[u+1])ok=0;
                        }
                    }

                    if(ok)nxt.push_back(to);
                }
            }
            sort(nxt.begin(),nxt.end());
            nxt.erase(unique(nxt.begin(),nxt.end()),nxt.end());
            my=nxt;
            if(nxt.size()){
                /*for (auto i : nxt){
                    for (auto t : i.ci) cout << t << " ";
                    cout << "\n";
                    break;
                }
                cout<<nxt.size()<<"\n";*/
            }
        }
    }
    for (auto i : my){
        bool ok=1;
        for (int j=0;j<m;j++) if(i.col[j]&&i.ci[j]!=2)ok=0;
        if(ok){
            for (int u=0;u<n;u++){
                for (int v=0;v<m;v++){
                    cout << (char)(i.more[u*m+v]? i.more[u*m+v]+'0':s[u][v]);
                }
                cout << "\n";
            }
            return 0;
        }
    }
    return 0;
}
10 10 8
..5...3..4
..1.7.....
......7...
..4..1#.3.
....##2...
...8##..6.
8..#......
.....5.6..
........2.
..........
#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define TIAO cout << fixed << setprecision(15)
using namespace std;
/*
by qqqaaazzz
好的,linknet.

目标:将网格图铺满,并满足一个条件。
1 5 1
1...1

4 4 2
1##1
.2..
..2.
....

5 10 3
1..2......
..1.......
.3........
...2......
....3.....

4 4 3
.31.
..2.
3...
2.1.

5554443334
5114744434
5144777434
514111#434
5111##2444
5588##2266
858#888226
8588858626
8555558626
8888888666

*/
int n,m,k;
char s[50][50];
int a[50][50];
int fx[]={0,0,1,-1};
int fy[]={1,-1,0,0};
struct Node{
    vector<int> col;//这一圈的颜色
    vector<int> ci;//这一圈的次数
    vector<int> more;
    bool operator < (const Node &x) const{
        if(col==x.col) return ci<x.ci;
        else return col<x.col;
    }
    bool operator == (const Node &x) const{
        return (col==x.col&&ci==x.ci);
    } 
};

signed main()
{
    cin >> n >> m >> k;
    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            cin >> s[i][j];
        }
    }
    vector<Node> my;
    Node q ;

    for (int i=0;i<m;i++) q.col.push_back(0),q.ci.push_back(0);

    my.push_back(q);

    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            vector<Node> nxt;
            for (auto y : my){
                for (int t=0;t<=k;t++){
                    if(s[i][j]=='.');//ok
                    else if(s[i][j]=='#'&&t!=0) continue;
                    else if(s[i][j]>='1'&&s[i][j]<='9'&&s[i][j]-'0'!=t) continue;

                    if(y.col[j]==0);//ok
                    else if(y.ci[j]+(t==y.col[j])!=2)continue;//no
                    Node to=y;

                    to.ci[j] = (j==0? 0:(to.col[j-1]==t))+(to.col[j]==t)+(s[i][j]>='1'&&s[i][j]<='9');
                    to.col[j]=t;
                    to.more.push_back(t);
                    if(j) to.ci[j-1]+=(to.col[j-1]==t);

                    bool ok=1;
                    for (int u=0;u<m;u++){
                        int lin = 1+(u==j&&u!=m-1);
                        if(to.col[u]){
                            if(to.ci[u]>2||to.ci[u]+lin<2) ok=0;
                            if(u!=j&&u+1!=j&&u+1<m&&to.ci[u]==1&&to.ci[u+1]==1&&to.col[u]==to.col[u+1])ok=0;
                        }
                    }

                    if(ok)nxt.push_back(to);
                }
            }
            sort(nxt.begin(),nxt.end());
            nxt.erase(unique(nxt.begin(),nxt.end()),nxt.end());
            my=nxt;
        }
    }
    for (auto i : my){
        bool ok=1;
        for (int j=0;j<m;j++) if(i.col[j]&&i.ci[j]!=2)ok=0;
        if(ok){
            for (int u=0;u<n;u++){
                for (int v=0;v<m;v++){
                    cout << (char)(i.more[u*m+v]? i.more[u*m+v]+'0':s[u][v]);
                }
                cout << "\n";
            }
            return 0;
        }
    }
    return 0;
}
#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define TIAO cout << fixed << setprecision(15)
using namespace std;
/*
by qqqaaazzz
好的,linknet.

目标:将网格图铺满,并满足一个条件。
1 5 1
1...1

4 4 2
1##1
.2..
..2.
....

6 6 3
1##2..
..1...
.3....
...2..
......
.....3

4 4 3
.31.
..2.
3...
2.1.

5 10 4
1..2.4....
..1.......
.3........
...2......
....3....4


10 10 8
..5...3..4
..1.7.....
......7...
..4..1#.3.
....##2...
...8##..6.
8..#......
.....5.6..
........2.
..........




*/
int n,m,k;
char s[50][50];
int a[50][50];
int fx[]={0,0,1,-1};
int fy[]={1,-1,0,0};

bool yu(){
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if(s[i][j]>='1'&&s[i][j]<='9'){
                int cnt=a[i][j],rong=0;
                for (int d=0;d<4;d++){
                    int dx=i+fx[d],dy=j+fy[d];
                    if(s[i][j]==s[dx][dy])++cnt;
                    if(s[dx][dy]=='.')++rong;
                }
                if(cnt>2) return 0;
                if(cnt+rong<2) return 0;
            }
        }
    }
    return 1;
}

void check(){
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if(s[i][j]>='1'&&s[i][j]<='9'){
                int cnt=a[i][j];
                for (int d=0;d<4;d++){
                    int dx=i+fx[d],dy=j+fy[d];
                    if(s[i][j]==s[dx][dy])++cnt;
                }
                if(cnt!=2) return;
            }
        }
    }
    cout << "Dekomori found a solution !\n";
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cout << s[i][j];
        }
        cout << "\n";
    }
    //exit(0);
}

void dfs(){
    /*for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cout << s[i][j];
        }
        cout << "\n";
    }
    cout << "-\n";*/
    int E=9999;
    bool win=1;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if(s[i][j]>='1'&&s[i][j]<='9'){
                int cnt=a[i][j],rong=0;
                for (int d=0;d<4;d++){
                    int dx=i+fx[d],dy=j+fy[d];
                    if(s[i][j]==s[dx][dy])++cnt;
                    if(s[dx][dy]=='.')++rong;
                }
                if(cnt!=2)win=0;
                if(cnt>2) return;
                if(cnt+rong<2) return;

                if(rong==1&&cnt==1){
                    for (int d=0;d<4;d++){
                        int dx=i+fx[d],dy=j+fy[d];
                        if(s[dx][dy]=='.'){
                            s[dx][dy]=s[i][j];
                            dfs();
                            s[dx][dy]='.';
                            return;
                        }
                    }
                }
            }
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if(s[i][j]>='1'&&s[i][j]<='9'){
                int cnt=a[i][j],rong=0;
                for (int d=0;d<4;d++){
                    int dx=i+fx[d],dy=j+fy[d];
                    if(s[i][j]==s[dx][dy])++cnt;
                    if(s[dx][dy]=='.')++rong;
                }
                if(cnt==1){
                    for (int d=0;d<4;d++){
                        int dx=i+fx[d],dy=j+fy[d];
                        if(s[dx][dy]=='.'){
                            s[dx][dy]=s[i][j];
                            dfs();
                            s[dx][dy]='.';
                        }
                    }
                }
            }
        }
    }
    if(!win)return;
    cout << "Dekomori found a solution !\n";
    cout << E << "\n";
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cout << s[i][j];
        }
        cout << "\n";
    }
}

signed main()
{
    cin >> n >> m >> k;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cin >> s[i][j];
            a[i][j] = (s[i][j]>='0'&&s[i][j]<='9');
        }
    }
    dfs();
    return 0;
}
#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define TIAO cout << fixed << setprecision(15)
using namespace std;
/*
by qqqaaazzz
好的,linknet.

目标:将网格图铺满,并满足一个条件。
1 5 1
1...1

4 4 2
1##1
.2..
..2.
....

6 6 3
1##2..
..1...
.3....
...2..
......
.....3

4 4 3
.31.
..2.
3...
2.1.

5 10 4
1..2.4....
..1.......
.3........
...2......
....3....4


10 10 8
..5...3..4
..1.7.....
......7...
..4..1#.3.
....##2...
...8##..6.
8..#......
.....5.6..
........2.
..........




*/
int n,m,k;
char s[50][50];
int a[50][50];
int fx[]={0,0,1,-1};
int fy[]={1,-1,0,0};

bool yu(){
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if(s[i][j]>='1'&&s[i][j]<='9'){
                int cnt=a[i][j],rong=0;
                for (int d=0;d<4;d++){
                    int dx=i+fx[d],dy=j+fy[d];
                    if(s[i][j]==s[dx][dy])++cnt;
                    if(s[dx][dy]=='.')++rong;
                }
                if(cnt>2) return 0;
                if(cnt+rong<2) return 0;
            }
        }
    }
    return 1;
}

void check(){
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if(s[i][j]>='1'&&s[i][j]<='9'){
                int cnt=a[i][j];
                for (int d=0;d<4;d++){
                    int dx=i+fx[d],dy=j+fy[d];
                    if(s[i][j]==s[dx][dy])++cnt;
                }
                if(cnt!=2) return;
            }
        }
    }
    cout << "Dekomori found a solution !\n";
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cout << s[i][j];
        }
        cout << "\n";
    }
    //exit(0);
}
set<pair<pair<int,int>,string> > st;
int E=0;

void dfs(){
//  ++E;
//  if(E%10000000==0){
//      for (int i=1;i<=n;i++){
//      for (int j=1;j<=m;j++){
//          cout <<s[i][j] << " ";
//      }
//      cout <<"\n";
//  }
//  cout <<"-\n";
//  }

    bool win=1;
    int sx=0,sy=0;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if(s[i][j]=='.'&&sx==0){
                sx=i,sy=j;
            }
            if(s[i][j]>='1'&&s[i][j]<='9'){
                int cnt=a[i][j],rong=0;
                for (int d=0;d<4;d++){
                    int dx=i+fx[d],dy=j+fy[d];
                    if(s[i][j]==s[dx][dy])++cnt;
                    if(s[dx][dy]=='.')++rong;
                }
                if(cnt>2) return;
                if(cnt+rong<2) return;
                if(rong==1&&cnt==1){
                    for (int d=0;d<4;d++){
                        int dx=i+fx[d],dy=j+fy[d];
                        if(s[dx][dy]=='.'){
                            s[dx][dy]=s[i][j];
                            dfs();
                            s[dx][dy]='.';
                            return;
                        }
                    }
                }
            }
        }
    }
    if(sx==0){
        cout << "Dekomori found a solution !\n";
        //cout << E << "\n";
        ++E;
        if(E>=15)exit(0);
        for (int i=1;i<=n;i++){
            for (int j=1;j<=m;j++){
                cout << (s[i][j]=='0'? '.':s[i][j]);
            }
            cout << "\n";
        }
    }
    else{
        //填!
        string qwq;
        for (int i=1;i<=sy-1;i++) qwq+=s[sx][i];
        for (int i=sy;i<=m;i++) qwq+=s[sx-1][i];
        //if(st.find({{sx,sy},qwq})!=st.end()) return;
        st.insert({{sx,sy},qwq});
        for (int i=0;i<=k;i++){
            s[sx][sy]='0'+i;
            dfs();
            s[sx][sy]='.';
        }
    }
}

signed main()
{
    cin >> n >> m >> k;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cin >> s[i][j];
            a[i][j] = (s[i][j]>='0'&&s[i][j]<='9');
        }
    }
    dfs();
    return 0;
}

include <bits/stdc++.h>

define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)

define TIAO cout << fixed << setprecision(15)

using namespace std; /* by qqqaaazzz 好的,linknet.

目标:将网格图铺满,并满足一个条件。 1 5 1 1...1

4 4 2 1##1 .2.. ..2. ....

6 6 3 1##2.. ..1... .3.... ...2.. ...... .....3

4 4 3 .31. ..2. 3... 2.1.

10 10 4 1..2.4.... ..1....... .3........ ...2...... ....3....4 .......... .......... .......... .......... ..........

10 10 8 ..5...3..4 ..1.7..... ......7... ..4..1#.3. ....##2... ...8##..6. 8..#...... .....5.6.. ........2. ..........

12 12 5 ............ ............ ..........1. .......5.... .......4.... 3......5.... ............ .........1.. ....2.4..... ............ 3.........2. ............

*/ int n,m,k; char s[50][50]; int a[50][50]; int fx[]={0,0,1,-1}; int fy[]={1,-1,0,0};

bool yu(){ for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if(s[i][j]>='1'&&s[i][j]<='9'){ int cnt=a[i][j],rong=0; for (int d=0;d<4;d++){ int dx=i+fx[d],dy=j+fy[d]; if(s[i][j]==s[dx][dy])++cnt; if(s[dx][dy]=='.')++rong; } if(cnt>2) return 0; if(cnt+rong<2) return 0; } } } return 1; }

void check(){ for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if(s[i][j]>='1'&&s[i][j]<='9'){ int cnt=a[i][j]; for (int d=0;d<4;d++){ int dx=i+fx[d],dy=j+fy[d]; if(s[i][j]==s[dx][dy])++cnt; } if(cnt!=2) return; } } } cout << "Dekomori found a solution !\n"; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ cout << s[i][j]; } cout << "\n"; } //exit(0); } set<pair<pair<int,int>,string> > st; int E=0; bool w[60][60];

void run(int x,int y,char A,char B){ for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ w[i][j]=0; } } w[x][y]=1; queue<pair<int,int> > q; q.push({x,y}); while(!q.empty()){ pair<int,int> f = q.front(); q.pop(); for (int d=0;d<4;d++){ int dx=f.first+fx[d]; int dy=f.second+fy[d]; if(s[dx][dy]==A||s[dx][dy]==B){ if(w[dx][dy]==0){ w[dx][dy]=1; q.push({dx,dy}); } } } } }int Rikka; int NE[60][60]; int _w[60][60],__w[60][60]; / 2222255555 2###25###5 6666.5###5 6#.6.5##55 6#.611155. 6..6###5.. 6.344445.. 6.34..455. 6.333.4..4 66666.4444 /

void dfs(){ //预先判断一下有没有可能得到答案。 // for (int i=1;i<=n;i++){ // for (int j=1;j<=m;j++){ // if(s[i][j]>='1'&&s[i][j]<='9'){ // int cnt=a[i][j],rong=0; // for (int d=0;d<4;d++){ // int dx=i+fx[d],dy=j+fy[d]; // if(s[i][j]==s[dx][dy])++cnt; // if(s[dx][dy]=='.')++rong; // } // NE[i][j]=cnt; // } // } // } // for (int i=1;i<=n;i++){ // for (int j=1;j<=m;j++){ // if(s[i][j]>='1'&&s[i][j]<='9'){ // run(i,j,s[i][j],'.'); // for (int X=1;X<=n;X++){ // for (int Y=1;Y<=m;Y++){ // _w[X][Y]=w[X][Y]; // } // } //
// run(i,j,s[i][j],s[i][j]); // bool ok=0; // bool die=(NE[i][j]==1); //
// for (int X=1;X<=n;X++){ // for (int Y=1;Y<=m;Y++){ // w[X][Y]=w[X][Y]; // if(w[X][Y]){ // if(NE[X][Y]==1||a[X][Y])ok=1; // } // if(NE[X][Y]==1){ // if(__w[X][Y]==0&&_w[X][Y]==1&&NE[X][Y]==1)die=0; // } // } // } // if(ok==0)return; // } // } // } for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if(s[i][j]=='0'&&s[i-1][j]>='1'&&s[i-1][j]<='9'&&s[i-1][j]==s[i+1][j])return; if(s[i][j]=='0'&&s[i][j-1]>='1'&&s[i][j-1]<='9'&&s[i][j-1]==s[i][j+1])return; if(s[i][j]>='1'&&s[i][j]<='9'&&s[i][j]==s[i+1][j]&&s[i][j]==s[i][j+1]&&s[i][j]==s[i+1][j+1]) return; } }

++E;
if(E%10000000==0){
    for (int i=1;i<=n;i++){
    for (int j=1;j<=m;j++){
        cout <<s[i][j] << " ";
    }
    cout <<"\n";
}
cout <<"-\n";
}

bool win=1;
int sx=0,sy=0;
for (int i=1;i<=n;i++){
    for (int j=1;j<=m;j++){
        if(s[i][j]=='.'&&sx==0){
            sx=i,sy=j;
        }
        if(s[i][j]>='1'&&s[i][j]<='9'){
            int cnt=a[i][j],rong=0;
            for (int d=0;d<4;d++){
                int dx=i+fx[d],dy=j+fy[d];
                if(s[i][j]==s[dx][dy])++cnt;
                if(s[dx][dy]=='.')++rong;
            }
            if(cnt>2) return;
            if(cnt+rong<2) return;
            if(rong==1&&cnt==1){
                for (int d=0;d<4;d++){
                    int dx=i+fx[d],dy=j+fy[d];
                    if(s[dx][dy]=='.'){
                        s[dx][dy]=s[i][j];
                        dfs();
                        s[dx][dy]='.';
                        return;
                    }
                }
            }
        }
    }
}
if(sx==0){
    int z=0;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if(s[i][j]=='0')++z;
        }
    }

// if(z>n*m/3)return;

    cout << "Dekomori found a solution !\n";
    //cout << E << "\n";
    ++Rikka;
    if(Rikka>=15){
        cout << "[more...]";
        exit(0);
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cout << (a[i][j]? s[i][j]:(s[i][j]=='.'||s[i][j]=='0'? '#':'.'));
            continue;

            if(s[i][j]>='1'&&s[i][j]<='9'){
                run(i,j,s[i][j],s[i][j]);
                bool ok=0;
                for (int X=1;X<=n;X++){
                    for (int Y=1;Y<=m;Y++){
                        if(w[X][Y]){
                            if(a[X][Y])ok=1;
                        }
                    }
                }
                if(ok==0)cout<<'.';
                else cout<<s[i][j];
            }
            else cout << (s[i][j]=='0'? '.':s[i][j]);
        }
        cout <<"\n";
    }
    return;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cout << (s[i][j]=='0'? '.':s[i][j]);
        }
        cout << "\n";
    }
}
else{
    //填!
    string qwq;
    for (int i=1;i<=sy-1;i++) qwq+=s[sx][i];
    for (int i=sy;i<=m;i++) qwq+=s[sx-1][i];
    //if(st.find({{sx,sy},qwq})!=st.end()) return;
    st.insert({{sx,sy},qwq});
    for (int i=k;i>=0;i--){
        s[sx][sy]='0'+i;
        dfs();
        s[sx][sy]='.';
    }
}

}

signed main() { cin >> n >> m >> k; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ cin >> s[i][j]; a[i][j] = (s[i][j]>='0'&&s[i][j]<='9'); } } dfs(); return 0; }

include <bits/stdc++.h>

define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)

define TIAO cout << fixed << setprecision(15)

using namespace std; / by qqqaaazzz / int n,m,k; char s[50][50]; int a[50][50]; int fx[]={0,0,1,-1}; int fy[]={1,-1,0,0}; set<pair<pair<int,int>,string> > st; int E=0; bool w[60][60]; mt19937 rd(time(0));

int Rikka; bool imp; void dfs(){ //预先判断一下有没有可能得到答案。 for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if(s[i][j]=='0'&&s[i-1][j]>='1'&&s[i-1][j]<='9'&&s[i-1][j]==s[i+1][j])return; if(s[i][j]=='0'&&s[i][j-1]>='1'&&s[i][j-1]<='9'&&s[i][j-1]==s[i][j+1])return; if(s[i][j]>='1'&&s[i][j]<='9'&&s[i][j]==s[i+1][j]&&s[i][j]==s[i][j+1]&&s[i][j]==s[i+1][j+1]) return; } } bool win=1; int sx=0,sy=0; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if(s[i][j]=='.'&&sx==0){ sx=i,sy=j; } if(s[i][j]>='1'&&s[i][j]<='9'){ int cnt=a[i][j],rong=0; for (int d=0;d<4;d++){ int dx=i+fx[d],dy=j+fy[d]; if(s[i][j]==s[dx][dy])++cnt; if(s[dx][dy]=='.')++rong; } if(cnt>2) return; if(cnt+rong<2) return; if(rong==1&&cnt==1){ for (int d=0;d<4;d++){ int dx=i+fx[d],dy=j+fy[d]; if(s[dx][dy]=='.'){ s[dx][dy]=s[i][j]; dfs(); if(imp)return; s[dx][dy]='.'; return; } } } } } } if(sx==0){ int z=0; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if(s[i][j]=='0')++z; } } ++Rikka; if(z){ for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if(s[i][j]=='0')s[i][j]='#'; else if(s[i][j]=='#')s[i][j]='#'; else if(a[i][j]==0)s[i][j]='.'; } } imp=1; return; } } else{ string qwq; for (int i=1;i<=sy-1;i++) qwq+=s[sx][i]; for (int i=sy;i<=m;i++) qwq+=s[sx-1][i]; st.insert({{sx,sy},qwq}); for (int i=k;i>=0;i--){ s[sx][sy]='0'+i; dfs(); if(imp)return; s[sx][sy]='.';

    }
}

}

void dfs(int x,int y,char k,int D,int lst){ s[x][y]=k; vector<int> E; for (int i=0;i<4;i++)E.push_back(i); E.push_back(lst); E.push_back(lst); E.push_back(lst); E.push_back(lst); for (int i=0;i<8;i++)swap(E[i],E[rd()%8]); for (auto f :E){ if(D>1&&rd()%10==0)break; int dx=x+fx[f],dy=y+fy[f]; if(dx>=1&&dx<=n&&dy>=1&&dy<=m&&s[dx][dy]=='.'){ int E=0; for (int t=0;t<4;t++){ int ddx=dx+fx[t],ddy=dy+fy[t]; if(s[ddx][ddy]==s[x][y])++E; } if(E>=2)continue; dfs(dx,dy,k,D+1,f); return; } } a[x][y]=1; } int lin(int x,int y){ int cnt=0; for (int i=0;i<4;i++){ int dx=x+fx[i],dy=y+fy[i]; if(s[dx][dy]=='.')++cnt; } return cnt; } bool L(int x,int y){ if(s[x][y]!='.')return 0;

for (int i=0;i<4;i++){
    int dx=x+fx[i],dy=y+fy[i];
    if(dx>=1&&dx<=n&&dy>=1&&dy<=m&&s[dx][dy]=='.'){
        if(lin(dx,dy)>=1) return 1;
    }
}
return 0;

} bool go(){ for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ s[i][j]='.'; a[i][j]=0; } } k=0; while(1){ vector<pair<int,int> > ans; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if(L(i,j)){ ans.push_back({i,j}); } } } if(ans.empty())break; ++k; int E=rd()%ans.size(); dfs(ans[E].first,ans[E].second,'0'+k,0,rd()%4); s[ans[E].first][ans[E].second]='0'+k; a[ans[E].first][ans[E].second]=1; if(k==9||rd()%10==0){ break; } } int Z=0; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if(s[i][j]=='.')++Z; } } if(Z>6) return 1; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ s[i][j]=(s[i][j]=='.'? '#':(a[i][j]?s[i][j]:'.')); } } return 0; } void complete(){ imp=0; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ a[i][j] = (s[i][j]>='0'&&s[i][j]<='9'); } } dfs(); } int QT,QK; char final[60][60];

void doit(int x,int y){ n=x,m=y; while(go()); // for (int i=1;i<=n;i++){ // for (int j=1;j<=m;j++){ // cout<<s[i][j]; // } // cout <<"\n"; // } // return; for (int i=1;;i++){ complete(); if(imp==0)break; } int E=0; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ E+=(s[i][j]=='#'); } } if(E<QT){ QT=E; QK=k; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ final[i][j]=s[i][j]; } } } } void simple(int x,int y){ QT=999999999; for (int i=1;i<=5;i++)doit(x,y); k=QK; printf("{rows: %d, cols: %d, colorCount: %d,\n",n,m,k); cout <<"grid:["; // cout << n <<" " <<m <<" " << k <<"\n"; for (int i=1;i<=n;i++){ cout <<"["; for (int j=1;j<=m;j++){ cout<<(final[i][j]=='.'? 0:(final[i][j]=='#'? -1:final[i][j]-'0')); if(j!=m)cout<<","; } cout<<"]"; if(i!=n)cout<<","; cout <<"\n"; } cout <<"]},"; }

signed main() { for (int i=1;i<=1;i++){ simple(5,5); } for (int i=1;i<=1;i++){ simple(7,7); } for (int i=1;i<=12;i++){ simple(10,10); } return 0; }