結果

問題 No.1427 Simplified Tetris
ユーザー chocoruskchocorusk
提出日時 2021-03-12 23:17:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 4,938 bytes
コンパイル時間 3,780 ms
コンパイル使用メモリ 196,084 KB
実行使用メモリ 9,880 KB
最終ジャッジ日時 2023-08-04 16:41:39
合計ジャッジ時間 5,805 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,728 KB
testcase_01 AC 3 ms
8,088 KB
testcase_02 AC 6 ms
9,744 KB
testcase_03 AC 2 ms
8,136 KB
testcase_04 AC 3 ms
8,176 KB
testcase_05 AC 5 ms
9,836 KB
testcase_06 AC 4 ms
9,728 KB
testcase_07 AC 3 ms
8,080 KB
testcase_08 AC 3 ms
8,084 KB
testcase_09 AC 4 ms
9,636 KB
testcase_10 AC 3 ms
8,204 KB
testcase_11 AC 3 ms
8,016 KB
testcase_12 AC 3 ms
8,080 KB
testcase_13 AC 5 ms
9,664 KB
testcase_14 AC 4 ms
9,736 KB
testcase_15 AC 4 ms
9,556 KB
testcase_16 AC 3 ms
7,988 KB
testcase_17 AC 4 ms
9,608 KB
testcase_18 AC 3 ms
8,084 KB
testcase_19 AC 6 ms
9,740 KB
testcase_20 AC 3 ms
8,016 KB
testcase_21 AC 3 ms
8,060 KB
testcase_22 AC 5 ms
9,732 KB
testcase_23 AC 5 ms
9,652 KB
testcase_24 AC 5 ms
9,704 KB
testcase_25 AC 4 ms
9,568 KB
testcase_26 AC 3 ms
8,304 KB
testcase_27 AC 3 ms
8,164 KB
testcase_28 AC 8 ms
9,740 KB
testcase_29 AC 5 ms
9,572 KB
testcase_30 AC 5 ms
9,864 KB
testcase_31 AC 5 ms
9,712 KB
testcase_32 AC 19 ms
9,648 KB
testcase_33 AC 19 ms
9,880 KB
testcase_34 AC 5 ms
9,640 KB
testcase_35 AC 3 ms
8,044 KB
testcase_36 AC 5 ms
8,032 KB
testcase_37 AC 6 ms
9,644 KB
testcase_38 AC 4 ms
8,020 KB
testcase_39 AC 3 ms
8,192 KB
testcase_40 AC 4 ms
8,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;

const ll INF=1e18;
struct edge {int to; ll cap; int rev;} ;

vector<edge> G[200020];
int level[200020];
int iter[200020];

void add_edge(int from, int to, ll cap){
	edge e;
	e.to=to, e.cap=cap, e.rev=G[to].size();
	G[from].push_back(e);
	e.to=from, e.cap=0, e.rev=G[from].size()-1;
	G[to].push_back(e);
}

void bfs(int s){
	memset(level, -1, sizeof(level));
	queue<int> que;
	level[s]=0;
	que.push(s);
	while(!que.empty()){
		int v=que.front();
		que.pop();
		for(int i=0; i<G[v].size(); i++){
			edge e=G[v][i];
			if(e.cap>0 && level[e.to]<0){
				level[e.to]=level[v]+1;
				que.push(e.to);
			}
		}
	}
}

ll dfs(int v, int t, ll f){
	if(v==t) return f;
	for(int &i=iter[v]; i<G[v].size(); i++){
		edge &e=G[v][i];
		if(e.cap>0 && level[v]<level[e.to]){
			ll d=dfs(e.to, t, min(f, e.cap));
			if(d>0){
				e.cap-=d;
				G[e.to][e.rev].cap+=d;
				return d;
			}
		}
	}
	return 0;
}

ll max_flow(int s, int t){
	ll flow=0;
	while(1){
		bfs(s);
		if(level[t]<0) return flow;
		memset(iter, 0, sizeof(iter));
		ll f;
		while((f=dfs(s, t, INF))>0){
			flow+=f;
		}
	}
}
string s[10];
string moji;
int main()
{
    int h, w; cin>>h>>w;
    for(int i=0; i<h; i++){
        cin>>s[i];
    }
    for(char c='A'; c<='Z'; c++) moji+=c;
    for(char c='a'; c<='z'; c++) moji+=c;
    bool exist[10]={};
    for(int i=0; i<h; i++){
        bool all=1;
        for(int j=0; j<w; j++){
            if(s[i][j]=='#'){
                exist[i]=1;
            }
            if(s[i][j]=='.'){
                all=0;
            }
        }
        if(all){
            cout<<"No"<<endl;
            return 0;
        }
    }
    for(int i=0; i<h-1; i++){
        if(exist[i] && !exist[i+1]){
            cout<<"No"<<endl;
            return 0;
        }
    }
    int l=-1;
    for(int i=0; i<h; i++){
        if(exist[i]){
            l=i; break;
        }
    }
    if(l==-1){
        cout<<"Yes"<<endl;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++) cout<<'.';
            cout<<endl;
        }
        return 0;
    }
    for(int i=0; i<(1<<h); i++){
        if(h-l!=popcount(i)) continue;
        string s1[10];
        for(int j=0; j<h; j++) for(int k=0; k<w; k++) s1[j]+='.';
        int j1=l;
        int l1=-1;
        for(int j=0; j<h; j++){
            if(i&(1<<j)){
                s1[j]=s[j1++];
                if(l1==-1) l1=j;
            }else if(j1>l){
                s1[j].clear();
                for(int k=0; k<w; k++) s1[j]+='#';
            }
        }
        auto solve=[&](){
            int a=h*w, b=h*w+1;
            for(int j=0; j<h*w+2; j++){
                G[j].clear();
            }
            int dx[4]={1, -1, 0, 0}, dy[4]={0, 0, 1, -1};
            int c1=0, c2=0;
            for(int j=0; j<h; j++){
                for(int k=0; k<w; k++){
                    if(s1[j][k]=='.') continue;
                    if((j+k)%2==0) add_edge(a, j*w+k, 1), c1++;
                    else add_edge(j*w+k, b, 1), c2++;
                    if((j+k)%2==0){
                        for(int p=0; p<4; p++){
                            int x1=j+dx[p], y1=k+dy[p];
                            if(x1>=0 && x1<h && y1>=0 && y1<w){
                                add_edge(j*w+k, x1*w+y1, 1);
                            }
                        }
                    }
                }
            }
            if(c1!=c2) return false;
            int f=max_flow(a, b);
            if(f<c1) return false;
            cout<<"Yes"<<endl;
            int m=0;
            for(int j=0; j<h; j++){
                for(int k=0; k<w; k++){
                    if(s1[j][k]=='.' || (j+k)%2!=0) continue;
                    int x=j*w+k;
                    for(auto e:G[x]){
                        int y=e.to;
                        if(y>=h*w) continue;
                        if(e.cap==0){
                            s1[j][k]=moji[m];
                            s1[y/w][y%w]=moji[m++];
                        }
                    }
                }
            }
            for(int j=0; j<h; j++){
                cout<<s1[j]<<endl;
            }
            return true;
        };
        if(solve()) return 0;
        for(int j=l1-1; j>=0; j--){
            s1[j].clear();
            for(int k=0; k<w; k++) s1[j]+='#';
            if(solve()) return 0;
        }
    }
    cout<<"No"<<endl;
    return 0;
}
0