QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#661846 | #8935. Puzzle: Easy as Scrabble | DBsoleil# | WA | 1ms | 5968kb | C++23 | 4.0kb | 2024-10-20 18:29:07 | 2024-10-20 18:29:07 |
Judging History
answer
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=5005;
int k,n,m;
char g[N][N],ans[N][N];
void input()
{
scanf("%d%d",&n,&m);
for(int i=0;i<=n+1;i++) scanf("%s",g[i]);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) ans[i][j]=g[i][j];
}
int check(char&x,char&y)
{
if(x=='.'||y=='.') return x+y-'.';
return x==y?x:-1;
}
bool fillrow(int x,int l,int r,int a,int b)
{
//if(x==3) cerr<<x<<' '<<l<<' '<<a<<' '<<b<<endl;
int nl=l,nr=r;
if(l>r) return 1;
if(g[x][0]!='.')
{
while(l<=r&&(ans[x][l]=='x'||check(g[x][0],g[a][l])==-1||check(g[x][0],g[b][l])==-1)) ans[x][l]='.',l++;
if(l<=r) ans[x][l]=g[x][0],g[x][0]=g[a][l]=g[b][l]='.',nl=l; else return 0;
}
else nl--;
if(g[x][m+1]!='.')
{
while(l<=r&&(ans[x][r]=='x'||check(g[x][m+1],g[a][r])==-1||check(g[x][m+1],g[b][r])==-1)) ans[x][r]='.',r--;
if(nl==l&&l==r)
{
if(ans[x][l]!=g[x][m+1]) return 0;
g[x][m+1]='.'; return 1;
}
if(l<=r) ans[x][r]=g[x][m+1],g[x][m+1]=g[a][r]=g[b][r]='.',nr=r; else return 0;
}
else nr++;
for(int i=nl+1;i<nr;i++)
{
if(ans[x][i]=='x'||check(g[a][i],g[b][i])==-1) continue;
ans[x][i]=check(g[a][i],g[b][i]),g[a][i]=g[b][i]='.';
}
return 1;
}
bool fillcolumn(int x,int u,int d,int a,int b)
{
//cerr<<x<<' '<<u<<' '<<d<<' '<<a<<' '<<b<<' '<<g[0][x]<<' '<<g[n+1][x]<<endl;
int nu=u,nd=d;
if(u>d) return 1;
if(g[0][x]!='.')
{
while(u<=d&&(ans[u][x]=='x'||check(g[0][x],g[u][a])==-1||check(g[0][x],g[u][b])==-1)) ans[u][x]='.',u++;
if(u<=d) ans[u][x]=g[0][x],g[0][x]='.',nu=u; else return 0;
}
else nu--;
if(g[n+1][x]!='.')
{
while(u<=d&&(ans[d][x]=='x'||check(g[n+1][x],g[d][a])==-1||check(g[n+1][x],g[d][b])==-1)) ans[d][x]='.',d--;
if(nu==u&&u==d)
{
if(ans[u][x]!=g[n+1][x]) return 0;
g[n+1][x]='.'; return 1;
}
//cerr<<'!'<<g[d][a]<<' '<<char(check(g[n+1][x],g[d][a]))<<' '<<char(check(g[n+1][x],g[d][b]))<<endl;
if(u<=d) ans[d][x]=g[n+1][x],g[n+1][x]='.',nd=d; else return 0;
}
else nd++;
// cerr<<' '<<u<<' '<<d<<endl;
for(int i=nu+1;i<nd;i++)
{
//cerr<<' '<<i<<' '<<g[i][a]<<' '<<g[i][b]<<endl;
if(ans[i][x]=='x'||check(g[i][a],g[i][b])==-1) continue;
ans[i][x]=check(g[i][a],g[i][b]),g[i][a]=g[i][b]='.';
}
return 1;
}
void update(char&x,char&y,char&ans)
{
if(ans=='x') return;
if(check(x,y)==-1) ans='.';
else ans=check(x,y),x=y='.';
}
bool fillin(int u,int d,int l,int r)
{
update(g[u][0],g[0][l],ans[u][l]);
update(g[u][m+1],g[0][r],ans[u][r]);
update(g[d][0],g[n+1][l],ans[d][l]);
update(g[d][m+1],g[n+1][r],ans[d][r]);
if(!fillrow(u,l+1,r-1,0,0)) return 0;
if(!fillrow(d,l+1,r-1,n+1,n+1)) return 0;
if(!fillcolumn(l,u+1,d-1,0,0)) return 0;
if(!fillcolumn(r,u+1,d-1,m+1,m+1)) return 0;
return 1;
}
bool work()
{
int u=1,d=n,l=1,r=m;
while(u<=d&&l<=r)
{
//cerr<<' '<<u<<' '<<d<<' '<<l<<' '<<r<<endl;
if(u==d) return fillrow(u,l,r,0,n+1);
if(l==r) return fillcolumn(l,u,d,0,m+1);
if(!fillin(u,d,l,r)) return 0;
else u++,d--,l++,r--;
}
return 1;
}
bool check()
{
for(int i=1;i<=n;i++) if(g[i][0]!='.'||g[i][m+1]!='.') return 0;
for(int i=1;i<=m;i++) if(g[0][i]!='.'||g[n+1][i]!='.') return 0;
return 1;
}
void solve()
{
input();
if(!work()||!check()) {printf("NO\n"); return;}
printf("YES\n");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(ans[i][j]=='x') ans[i][j]='.';
printf("%c",ans[i][j]);
}
printf("\n");
}
}
int main()
{
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5892kb
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: 0
Accepted
time: 1ms
memory: 5832kb
input:
1 2 .... Nx.. ..O.
output:
NO
result:
ok Correct.
Test #3:
score: 0
Accepted
time: 1ms
memory: 5920kb
input:
5 5 .U.N.X. U....xX Ox....X M...xxN Vx....S Ix.x..X ..IBHX.
output:
YES U.NX. .O.XX M.N.. .VB.S .I.HX
result:
ok Correct.
Test #4:
score: 0
Accepted
time: 1ms
memory: 5924kb
input:
10 10 .BAZEMIEKUJ. A..........K B..x.x.x..x. K.........xT A.x..x.....J Hx....x....B Q..x....x.xW S...x......W S...x.xxx..Z ...x......xZ I..x..x.x.xR .QKO.ID..RW.
output:
YES .AZEMIEK.. BB......U. K.......T. A.......JJ .H.......B Q.......W. S.......WW S.O.....Z. QK...D..Z. ...II...R.
result:
ok Correct.
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 5968kb
input:
5 10 .PTWIVVISPY. T...x.x....Y Xx.x.xx..x.K P.x.xx.....K S..........A E.........xS .SPEASDCYSA.
output:
NO
result:
wrong answer Jury has answer but participant has not.