QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#574552#8935. Puzzle: Easy as ScrabblechenhongxuanWA 1ms8148kbC++148.9kb2024-09-18 22:43:352024-09-18 22:43:35

Judging History

你现在查看的是最新测评结果

  • [2024-09-18 22:43:35]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:8148kb
  • [2024-09-18 22:43:35]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define lc p<<1
#define rc p<<1|1
#define pb push_back
#define pii pair<int,int>
#define st first
#define nd second
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar())f^=ch=='-';
    for(;isdigit(ch);ch=getchar())x=x*10+(ch^48);
    return f?x:-x;
}
mt19937 rnd(time(0));
const int mo=998244353;
inline int qpow(int x,int t){
    int ret=1;
    for(;t;t>>=1,x=x*x%mo)if(t&1)ret=ret*x%mo;
    return ret;
}
inline void red(int &x){x>=mo?x-=mo:0;}
inline void chmin(int &x,int y){x=min(x,y);}
inline void chmax(int &x,int y){x=max(x,y);}
const int N=1005;
char str[N][N],ff[N][N];
int n,m,tag[N][N],qd[N][N];
struct oper{
    int x,y,dx,dy;
    char c;
    pii type,ori;
};
vector<oper> op;
pii rev(pii t,int x,int y){
    if(t.st==-1)t.st=x;
    if(t.nd==-1)t.nd=y;
    return t;
}
void output(int t=0){
    for(int i=1;i<=n;++i,puts(""))for(int j=1;j<=m;++j){
        if(!ff[i][j])putchar('.');
        else putchar(ff[i][j]);
    }
    if(t==1){
        for(int i=1;i<=n;++i,puts(""))for(int j=1;j<=m;++j){
            printf("%lld ",qd[i][j]);
        }
    }
    if(t==2){
        for(int i=0;i<=n+1;++i,puts(""))for(int j=0;j<=m+1;++j){
            printf("%lld ",tag[i][j]);
        }
    }
}
int in(int x,int l,int r){return l<=x&&x<=r;}
void work(int i1,int i2,int j1,int j2){
    // printf("This is the :(%lld,%lld,%lld,%lld) work\n",i1,i2,j1,j2);
    if(i1>i2)return;
    if(j1>j2)return;
    op.clear();
    if(!tag[i1][0])op.pb(oper{i1,j1,0,1,str[i1][0],{0,-1},{i1,0}});
    if(!tag[i1][m+1])op.pb(oper{i1,j2,0,-1,str[i1][m+1],{0,-1},{i1,m+1}});
    if(!tag[i2][0])op.pb(oper{i2,j1,0,1,str[i2][0],{n+1,-1},{i2,0}});
    if(!tag[i2][m+1])op.pb(oper{i2,j2,0,-1,str[i2][m+1],{n+1,-1},{i2,m+1}});
    if(!tag[0][j1])op.pb(oper{i1,j1,1,0,str[0][j1],{-1,0},{0,j1}});
    if(!tag[n+1][j1])op.pb(oper{i2,j1,-1,0,str[n+1][j1],{-1,0},{n+1,j1}});
    if(!tag[0][j2])op.pb(oper{i1,j2,1,0,str[0][j2],{-1,m+1},{0,j2}});
    if(!tag[n+1][j2])op.pb(oper{i2,j2,-1,0,str[n+1][j2],{-1,m+1},{n+1,j2}});
    //recheck : so try again
    // if(!tag[i1][0])op.pb(oper{i1,j1,0,1,str[i1][0],{0,-1},{i1,0}});
    // if(!tag[i1][m+1])op.pb(oper{i1,j2,0,-1,str[i1][m+1],{0,-1},{i1,m+1}});
    // if(!tag[i2][0])op.pb(oper{i2,j1,0,1,str[i2][0],{n+1,-1},{i2,0}});
    // if(!tag[i2][m+1])op.pb(oper{i2,j2,0,-1,str[i2][m+1],{n+1,-1},{i2,m+1}});
    // if(!tag[0][j1])op.pb(oper{i1,j1,1,0,str[0][j1],{-1,0},{0,j1}});
    // if(!tag[n+1][j1])op.pb(oper{i2,j1,-1,0,str[n+1][j1],{-1,0},{n+1,j1}});
    // if(!tag[0][j2])op.pb(oper{i1,j2,1,0,str[0][j2],{-1,m+1},{0,j2}});
    // if(!tag[n+1][j2])op.pb(oper{i2,j2,-1,0,str[n+1][j2],{-1,m+1},{n+1,j2}});
    for(oper T:op){
        int exx=0;
        int x=T.x,y=T.y,dx=T.dx,dy=T.dy;
        char c=T.c;
        pii pos=T.type;
        int r=0;
        while(in(x,i1,i2)&&in(y,j1,j2)){
            if(ff[x][y]){
                if(ff[x][y]=='.'){
                    qd[x][y]=-1;
                    x+=dx;
                    y+=dy;
                    continue;
                }
                if(ff[x][y]==c)r=1;
                exx=1;
                break;
            }
            else{
                pii tmp=rev(pos,x,y);
                if(tag[tmp.st][tmp.nd]||str[tmp.st][tmp.nd]==c){
                    ff[x][y]=c;
                    r=1;
                    break;
                }
                qd[x][y]=-1;
                x+=dx;
                y+=dy;
            }
        }
        if(!r){
            if(n==1000&&m==1000)printf("%lld @ %lld %lld\n",exx,T.ori.st,T.ori.nd);
            printf("%lld %lld\n",dx,dy);
            puts("NO");
            exit(0);
            // output(1);
            return;
        }
    }
    for(oper T:op){
        int x=T.x,y=T.y,dx=T.dx,dy=T.dy;
        char c=T.c;
        pii pos=T.type;
        int r=0;
        while(in(x,i1,i2)&&in(y,j1,j2)){
            if(ff[x][y]){
                if(ff[x][y]=='.'){
                    qd[x][y]=-1;
                    x+=dx;
                    y+=dy;
                    continue;
                }
                if(ff[x][y]==c)r=1;
                break;
            }
            else{
                pii tmp=rev(pos,x,y);
                if(tag[tmp.st][tmp.nd]||str[tmp.st][tmp.nd]==c){
                    ff[x][y]=c;
                    r=1;
                    break;
                }
                qd[x][y]=-1;
                x+=dx;
                y+=dy;
            }
        }
        if(!r){
            if(n==1000&&m==1000)printf("get killed-by correction @ %lld %lld\n",T.ori.st,T.ori.nd);
            puts("NO");
            exit(0);
            // output(1);
            return;
        }
    }
    for(auto T:op){
        tag[T.ori.st][T.ori.nd]=1;
    }
    // puts("First Work");
    // output(1);
    for(int i=i1+1;i<i2;++i){
        if(qd[i][j1]!=-1){
            if(ff[i][j1]){
                tag[i][0]|=(ff[i][j1]==str[i][0]);
            }
            else{
                if(!tag[i][0]){
                    ff[i][j1]=str[i][0];
                    tag[i][0]=1;
                }
            }
        }
    }
    for(int i=i1+1;i<i2;++i){
        if(qd[i][j2]!=-1){
            if(ff[i][j2]){
                tag[i][m+1]|=(ff[i][j2]==str[i][m+1]);
            }
            else{
                if(!tag[i][m+1]){
                    ff[i][j2]=str[i][m+1];
                    tag[i][m+1]=1;
                }
            }
        }
    }
    for(int j=j1+1;j<j2;++j){
        if(qd[i1][j]!=-1){
            if(ff[i1][j]){
                tag[0][j]|=(ff[i1][j]==str[0][j]);
            }
            else{
                if(!tag[0][j]){
                    ff[i1][j]=str[0][j];
                    tag[0][j]=1;
                }
            }
        }
    }
    for(int j=j1+1;j<j2;++j){
        if(qd[i2][j]!=-1){
            if(ff[i2][j]){
                tag[n+1][j]|=(ff[i2][j]==str[n+1][j]);
            }
            else{
                if(!tag[n+1][j]){
                    ff[i2][j]=str[n+1][j];
                    tag[n+1][j]=1;
                }
            }
        }
    }
    // puts("Second work");
    // output(2);
    work(i1+1,i2-1,j1+1,j2-1);
}
void solve(){
    n=read(),m=read();
    for(int i=0;i<=n+1;++i)scanf("%s",str[i]);
    for(int i=1;i<=n;++i){
        if(str[i][0]=='.')tag[i][0]=1;
        if(str[i][m+1]=='.')tag[i][m+1]=1;
    }
    for(int j=1;j<=m;++j){
        if(str[0][j]=='.')tag[0][j]=1;
        if(str[n+1][j]=='.')tag[n+1][j]=1;
    }
    for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){
        if(str[i][j]=='x')ff[i][j]='.';
    }
    work(1,n,1,m);
    for(int t=1;t<=2;++t){
        for(int i=1;i<=n;++i){
            if(!tag[i][0]){
                int r=1;
                for(int j=1;j<=m;++j){
                    if(ff[i][j]!='.'&&ff[i][j]){
                        if(str[i][0]==ff[i][j])r=0;
                        break;
                    }
                }
                if(r)for(int j=1;j<=m;++j){
                    if(!ff[i][j]){ff[i][j]=str[i][0],r=0;break;}
                }
                if(r){puts("NOp");return;}
            }
            if(!tag[i][m+1]){
                int r=1;
                for(int j=m;j>=1;--j){
                    if(ff[i][j]!='.'&&ff[i][j]){
                        if(str[i][m+1]==ff[i][j])r=0;
                        break;
                    }
                }
                if(r)for(int j=m;j>=1;--j){
                    if(!ff[i][j]){ff[i][j]=str[i][m+1],r=0;break;}
                }
                if(r){puts("NOp");return;}
            }
        }
        for(int j=1;j<=m;++j){
            if(!tag[0][j]){
                int r=1;
                for(int i=1;i<=n;++i){
                    if(ff[i][j]!='.'&&ff[i][j]){
                        if(str[0][j]==ff[i][j])r=0;
                        break;
                    }
                }
                if(r)for(int i=1;i<=n;++i){
                    if(!ff[i][j]){ff[i][j]=str[0][j],r=0;break;}
                }
                if(r){puts("NOp");return;}
            }
            if(!tag[n+1][j]){
                int r=1;
                for(int i=n;i>=1;--i){
                    if(ff[i][j]!='.'&&ff[i][j]){
                        if(str[n+1][j]==ff[i][j])r=0;
                        break;
                    }
                }
                if(r)for(int i=n;i>=1;--i){
                    if(!ff[i][j]){ff[i][j]=str[n+1][j],r=0;break;}
                }
                if(r){puts("NOp");return;}
            }
        }
    }
    puts("YES");
    for(int i=1;i<=n;++i,puts(""))for(int j=1;j<=m;++j){
        if(!ff[i][j])putchar('.');
        else putchar(ff[i][j]);
    }
    return;
}
signed main(){
    // freopen("D.out","w",stdout);
    for(int cas=1;cas--;)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 8020kb

input:

5 5
.CBA...
....x..
..x...C
A.....B
B..x..A
C......
.......

output:

YES
CBA..
....C
A...B
B...A
C....

result:

ok Correct.

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 8148kb

input:

1 2
....
Nx..
..O.

output:

-1 0
NO

result:

wrong answer YES or NO expected in answer, but -1 found.