結果

問題 No.1427 Simplified Tetris
ユーザー chocorusk
提出日時 2021-03-12 23:17:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 4,938 bytes
コンパイル時間 4,925 ms
コンパイル使用メモリ 189,264 KB
最終ジャッジ日時 2025-01-19 15:45:09
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0