QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#235221 | #1811. How to Move the Beans | Famiglistmo | WA | 0ms | 5892kb | C++14 | 4.0kb | 2023-11-02 16:11:59 | 2023-11-02 16:12:00 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1005;
void file(){
freopen("chequer.in","r",stdin);
freopen("chequer.out","w",stdout);
}
char s[N][N];
int sg[N][N],L[N],R[N],pre[N],nex[N];
int f[N][5],g[N][5],tl[N],tr[N];
int T,n,m;
bool vis[10];
int mex(int a,int b){
if(a!=-1)vis[a]=true;
if(b!=-1)vis[b]=true;
int res=0;
while(vis[res])++res;
if(a!=-1)vis[a]=false;
if(b!=-1)vis[b]=false;
return res;
}
int mex(int a,int b,int c){
if(a!=-1)vis[a]=true;
if(b!=-1)vis[b]=true;
if(c!=-1)vis[c]=true;
int res=0;
while(vis[res])++res;
if(a!=-1)vis[a]=false;
if(b!=-1)vis[b]=false;
if(c!=-1)vis[c]=false;
return res;
}
void solve(){
cin>>n>>m;
for(int i=1;i<=n;++i)cin>>(s[i]+1);
for(int i=1;i<=m;++i)sg[n+1][i]=-1,pre[i]=i-1,nex[i]=i+1;
pre[1]=m,nex[m]=1;
for(int h=n;h>=1;--h){
if(m==1){
if(s[h][1]=='#')sg[h][1]=-1;
else sg[h][1]=mex(-1,sg[h+1][1]);
continue;
}
vector<int>vec;
for(int i=1;i<=m;++i)
if(s[h][i]=='#')
vec.push_back(i),L[i]=R[i]=-1;
if(vec.size()){
for(int _=0;_<(int)vec.size();++_){
int l=vec[_]+1,r;
if(_==(int)vec.size()-1)r=vec[0]-1;
else r=vec[_+1]-1;
if(l==m+1)l=1;
if(r==0)r=m;
L[l]=mex(-1,sg[h+1][l]);
for(int i=nex[l];i!=nex[r];i=nex[i])
L[i]=mex(L[pre[i]],sg[h+1][i]);
R[r]=mex(-1,sg[h+1][r]);
for(int i=pre[r];i!=pre[l];i=pre[i])
R[i]=mex(R[nex[i]],sg[h+1][i]);
if(l<=r) {
for(int i=l;i<=r;++i)
sg[h][i]=mex(L[pre[i]],R[nex[i]],sg[h+1][i]);
} else {
for(int i=l;i<=m;++i) sg[h][i]=mex(L[pre[i]],R[nex[i]],sg[h+1][i]);
for(int i=1;i<=r;++i) sg[h][i]=mex(L[pre[i]],R[nex[i]],sg[h+1][i]);
}
}
} else {
if(m<=300){
for(int i=1;i<=m;++i){
L[nex[i]]=mex(-1,sg[h+1][nex[i]]);
for(int j=nex[nex[i]];j!=i;j=nex[j])
L[j]=mex(L[pre[j]],sg[h+1][j]);
R[pre[i]]=mex(-1,sg[h+1][pre[i]]);
for(int j=pre[pre[i]];j!=i;j=pre[j])
R[j]=mex(R[nex[j]],sg[h+1][j]);
sg[h][i]=mex(L[pre[i]],R[nex[i]],sg[h+1][i]);
}
} else {
int p=m/2;
for(int v=0;v<4;++v)
f[p][v]=mex(v,sg[h+1][p]);
for(int i=nex[p];i!=p;i=nex[i])
for(int v=0;v<4;++v)
f[i][v]=f[pre[i]][mex(v,sg[h+1][i])];
for(int v=0;v<4;++v)
g[p][v]=v;
for(int i=pre[p];i!=p;i=pre[i])
for(int v=0;v<4;++v)
g[i][v]=mex(g[nex[i]][v],sg[h+1][i]);
for(int i=1;i<=m;++i){
if(i==p||i==pre[p]||i==nex[p]||i==pre[pre[p]]||i==nex[nex[p]]) {
R[pre[i]]=mex(-1,sg[h+1][pre[i]]);
for(int j=pre[pre[i]];j!=i;j=pre[j])
R[j]=mex(R[nex[j]],sg[h+1][j]);
tr[i]=R[nex[i]];
} else {
int rp=f[pre[pre[i]]][mex(-1,sg[h+1][pre[i]])];
tr[i]=g[nex[i]][rp];
}
}
/**/
for(int v=0;v<4;++v)
f[p][v]=mex(v,sg[h+1][p]);
for(int i=pre[p];i!=p;i=pre[i])
for(int v=0;v<4;++v)
f[i][v]=f[nex[i]][mex(v,sg[h+1][i])];
for(int v=0;v<4;++v)
g[p][v]=v;
for(int i=nex[p];i!=p;i=nex[i])
for(int v=0;v<4;++v)
g[i][v]=mex(g[pre[i]][v],sg[h+1][i]);
for(int i=1;i<=m;++i){
if(i==p||i==pre[p]||i==nex[p]||i==pre[pre[p]]||i==nex[nex[p]]) {
L[nex[i]]=mex(-1,sg[h+1][nex[i]]);
for(int j=nex[nex[i]];j!=i;j=nex[j])
L[j]=mex(L[pre[j]],sg[h+1][j]);
tl[i]=L[pre[i]];
} else {
int lp=f[nex[nex[i]]][mex(-1,sg[h+1][nex[i]])];
tl[i]=g[pre[i]][lp];
}
}
/**/
for(int i=1;i<=m;++i)
sg[h][i]=mex(tl[i],tr[i],sg[h+1][i]);
}
}
}
int ans=0;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(s[i][j]=='B')
ans^=sg[i][j];
if(ans) puts("A");
else puts("B");
}
signed main(){
// freopen("chequer.in","r",stdin);
// freopen("mine.out","w",stdout);
// file();
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
// cin>>T;
T=1;
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 5892kb
input:
2 3 B.# #..
output:
A
result:
wrong answer 1st words differ - expected: 'Alice', found: 'A'