QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#235238 | #1811. How to Move the Beans | Famiglistmo | WA | 185ms | 8468kb | C++14 | 4.2kb | 2023-11-02 16:22:07 | 2023-11-02 16:22:09 |
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]])];
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]);
assert(R[nex[i]]==g[nex[i]][rp]);
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("Alice");
else puts("Bob");
}
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: 100
Accepted
time: 1ms
memory: 5732kb
input:
2 3 B.# #..
output:
Alice
result:
ok "Alice"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5756kb
input:
1 1 B
output:
Bob
result:
ok "Bob"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5728kb
input:
1 3 B#.
output:
Alice
result:
ok "Alice"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
5 18 #.#..#.#..###..### ##...#.#..#.#..#.. #....#.#..###..#.. ##...#.#..#....#.. #.#..###..#....###
output:
Bob
result:
ok "Bob"
Test #5:
score: 0
Accepted
time: 3ms
memory: 8468kb
input:
293 249 #B..B..BBB..B.B.B...#BBB..B#B.BBB.#B##B.BB.B.##B#B.BB..B.#B#BB.BB##B#BB#...B..BB..B.B##B.B#B.#.##..B.#..BBBB#.BB..#.BBB#B##BB....B.##B..#...B#..B.BB#B.#...B#.BB.#B#.B...BBB.B.B..B....##.B..B##.BB#B.B.BBB.B#B..#.B.B#.B##..B#.##BB.#BB#.BB.#.B..BB.BB.B BB.B.#.B#BB.B.B..B.###.B...BB.##.B...B#BB....
output:
Alice
result:
ok "Alice"
Test #6:
score: 0
Accepted
time: 1ms
memory: 4280kb
input:
75 13 BBB.B.BB.B.#B BB.##...B##BB ..#.B....#.B. BBB..#...B#BB #B#.##B..BB.. #B..BBBB.B..# #B##.BBBBB.B# BBB.B..#B#B.. .BBB####.##BB .B##...#.#.#. #.BB#..B.B.B. BB#BB.BBB..BB B.#...#.BBBBB ..#B.BBBB..B# BB..B..#..... ..B..BBBB.B#. .B.B##B.#B.## BBBB#...#..B# B.BB..BBB#B.B .#B#B...BB.BB #.B...BB...B. ...
output:
Alice
result:
ok "Alice"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3848kb
input:
35 38 #.#..B.B.#.BB#.B.B.B..##..B#B###BBB##. .#.#.B#.#BBB..B..BB.#..BB..##B......#B B.B#..B..B...##B#B..#.##.#..B.#..B##BB #.B.B..#..B#.#.B#B##B.BBB..#.B...B..#. B#.#.BBB..B.BB.B..BBBBB##B..BB#.B#.BB. B##.B#...B.B.BB.BBB..#BBB.#..#B#.B..#B B....B#B.#.BBB.B#BB...##B#...B..BB.BB. ..###.#.B#....#.....#...
output:
Alice
result:
ok "Alice"
Test #8:
score: 0
Accepted
time: 2ms
memory: 5876kb
input:
36 106 BB.B..B.BBBB.BB..BB..BB..BB...BB.B..B.B..BBBB.B.BB..B.B..BB..BBBB.B.B.BBBB..B.BBBBBBBBBBBBBBB.BB.B.BBB..BB #BBB.BBB#..#.BB...###B.B#.B...B.#.BBBB.B..B..#B.#B#BB##.BB.#B..B#B...B....B#B#.#B.B.B.BB#..B#B#.#.#B###.B. B.BBB.BBBBB.BBB....BB..B.BBBB.BBB.BBB.BBB.B.B.BBB.B...B.B.BBBBB.BBB....BB.BBB.B...
output:
Alice
result:
ok "Alice"
Test #9:
score: 0
Accepted
time: 33ms
memory: 4896kb
input:
245 239 .BBB.BB.B.BBB..B.BB.BBBBBB..B.BBB.B.BBBBBBBBB..B..BB.B.B.BB..B.BB...B.BB.B.BB..BBB..BB..BB.BBB.BBB.BBBBB.BBBB.BB.BBBB...BBB.B..BBBBBBB..BB..B.B.B..BBBBB...BB..BBB...BBB.B..BB.BB.B.BB.BBB..B.B.BBBB.BBBBB.BBB.BBBB.BB...B.B.B...BBB.BBB.BBB..B B#.#BB..#BB.BBB#..BB..#.B#.##B.BBB###..B#B...BB..#.#...
output:
Bob
result:
ok "Bob"
Test #10:
score: 0
Accepted
time: 3ms
memory: 5092kb
input:
288 94 B....BB....BB.BBB.BBBB..B.B.B.....BBBB.B..BBBBBB.B..BBBBBBB....BBB.BBBBB.B.BBBB..B..B.B.BBB..B BB#.#B#BBB.B#BB..BBB.B#BB#B#BB#BBBBBB####.B.B...BBB.BB.B.#.B.B.B.#.B.B.#.#..B..BB..B#..B.#.... BBBBBBB...B.BB.B..BB..BBBB.BBBBB.BBBB..BBBBBB.BB....B.BBBB.BB.BBB..B.BBBB.B.BBBBBB..BBB.BBB.B. .B....B....
output:
Alice
result:
ok "Alice"
Test #11:
score: 0
Accepted
time: 1ms
memory: 8260kb
input:
119 1 B . # . B . # # # . B B # B # # # B # B . . B B B B . B B B B # # . B B B . B . # B B . # B . # . B B B B B B # # B B B # . . # . B B B B . B B . B B B # # B B . # # B B . B . # . B B B B # B . B B . B B . # # B . . . # . . B . . B . . #
output:
Alice
result:
ok "Alice"
Test #12:
score: 0
Accepted
time: 1ms
memory: 4612kb
input:
196 1 . B # . . # B . . . . B B B # . . B # B . B B # . B B . B B B . B . . . B # . . . . . B . B . . . B # B B . . B B # . . # . # B . # . B B . B B B . B B B # . . B . # . . . B . . B B . B . B . . # . . # B B # # . . . B B # . B # B . . # . . . B . . B B # # B . # B # . B . B B . # . . . . # B B ...
output:
Bob
result:
ok "Bob"
Test #13:
score: 0
Accepted
time: 1ms
memory: 6300kb
input:
148 1 . B B . . # B . # . B . . . . B . . . B . B # . # B B . . . B B B # . # . # # . # B . B B B B B . B . . # B B . . . . # . . # B . B . . . B . . . B B . . B . B . . . B B # . . . . B . B . B . . . . # # B . . B . # . # . B # B . . . # . B . B # B B # . B B B . . . . B # B # B B # B # B B B B . B
output:
Alice
result:
ok "Alice"
Test #14:
score: 0
Accepted
time: 1ms
memory: 5900kb
input:
2 5 ...#. .#B..
output:
Alice
result:
ok "Alice"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3696kb
input:
2 4 B..B ....
output:
Bob
result:
ok "Bob"
Test #16:
score: 0
Accepted
time: 1ms
memory: 5980kb
input:
8 1 . B . . . B . .
output:
Bob
result:
ok "Bob"
Test #17:
score: 0
Accepted
time: 58ms
memory: 8416kb
input:
803 520 BBB#...B.BBB.BBBB#.#BBB.#BBB.BBB.BB.BB..B#BBB##.#BB.#BBBB.BB#BB.###BBBBB.BBBBB.#.BBBBBB#BBB#.B.BB.####BBBB...#.BBBBBBBB#BBB#BBB##B#B..#BBB.BB#.BBBBB#.BB#B..BBBB.BBBB.BB.BBBBB##B#BB.BB#B##B#BBBBB.BBBBBB#BBB##BBBBB##B.B.BBB.B.BBBBB#B#BB.BBBBBB..#B.##BB...B.B.BBBBB..BB.B.##BBBB#B#..#BBBBBBBBBB#...
output:
Alice
result:
ok "Alice"
Test #18:
score: 0
Accepted
time: 185ms
memory: 7776kb
input:
713 987 B.BB#.BBB#B#BB#BBBBBBB##.BBB..B#..B..BBBBBBBB#BB#BB..BB..##BBBBBBB#BBBBB.#BBBBB#B..BBBBBB.BB#B#BBBBBB#BB.B#BB#B..B#B.B#.B#BB.BB.BBBBB.BB#B#B#B.B##BB##B#BB##..#BB#.B.B.#.B#BB.BBBBBBB#BBBB.#.B.#BBB#BB.BBBB.B...B##B..#BB.BBBB.##..#B.B##BBBB.B#BBB##.B#BB.BB..BB#B.BB.#..#.#BB#B#..BBBBBBB.BBBB.B#B...
output:
Alice
result:
ok "Alice"
Test #19:
score: 0
Accepted
time: 9ms
memory: 6140kb
input:
103 515 #B#BB..BB.BBB#BB#..BB.#BBBBBB.###B#BB##BBB#BBBBBB.B.BBBB##BBB#BBB...###BBBBBBB.BBBBB.BBB.B#B.#B..BB..BBBB#.#BB#B.B#BB#B#..B.B.B#.BBB..B#.BB##.BBB#B##BB.B.#BBBBBB#BBBBB.BBB#.BB#.B.BBBBBBB...B.BB..B..BB##BBBB##B#.BB.B..#B.BB.##B.BBBB#BBBBBBB####BBB#BB#BBBBBB#B#B##BBB.B#B.B.##B...BBBB#BBBBB#BB#...
output:
Alice
result:
ok "Alice"
Test #20:
score: 0
Accepted
time: 0ms
memory: 5812kb
input:
21 144 BBBBBB.BBBBBBBBBBB.BBBBB..BBB.B..BBBBBB.BBBBBBBB.BBBBBBB.BBBBBBBBBBBB.BB.BBBBB.BB.BBBB.BBBBB.B.BBBBBBBBB.B.BBBB.BBBBBBBBBBB.BBB.B.BB.BBBBBBB..BB BBB.B.BBBBB.##BBB.B##..BB..BBBBBBBB#B.BB.B..BBBB#.B.#BBB...B.BB#BBBBBB#B.BBBBBB.B.BBB#BB..BBBB.#.B#BBB#BB#B..B#BBB#B.B##B.B#.##B.BB#B#B.B...B.BB B.B...
output:
Alice
result:
ok "Alice"
Test #21:
score: -100
Wrong Answer
time: 76ms
memory: 4928kb
input:
245 501 B.BBBBBBB.B.BB.BBB.BB.BBBBBBBBBBB.BB.BBBBBBBBBBBBB.BB.BBBBBBB.BBBBBB.BBB.B.BBB.BBBBBBBB..BBB..BB.BBBBB.BBB.BBBBB.BBBB..BBBBB.BBB.BBBBB.BBBBBBBB.BBB.BBBBB.BBBBBBBBBBBBBBBB.BBBBBBBB.BBB.BBB.BB...BBB.B.BBBB.BBB.B.BB.BBBBBBBBBBBBBBBBB.BBBBB.BBBBBBB.BBB.B.B..BB.BBBB..BBBBBB.BBB..BB.BBB.BB.BBB.BB....
output:
Alice
result:
wrong answer 1st words differ - expected: 'Bob', found: 'Alice'