# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
959141 | berr | Cop and Robber (BOI14_coprobber) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
int pos;
map<array<int, 2>, int> mp;
int state[500][500][2];
vector<vector<int>> g(500);
int calc(int x, int y, int player){
if(x==y&&player==0) return state[x][y][player]=x;
else if(x==y)return state[x][y][player]=-1;
if(state[x][y][player]!=-2)return state[x][y][player];
if(player==0){
int c=0;
state[x][y][player]=-1; //-1 means holding
for(auto i: g[x]){
if(calc(i, y, 1)==-1){
state[x][y][0]=i;
c++;
}
}
if(calc(x, y, 1)==-1){
state[x][y][0]=x;
c++;
}
if(!c) state[x][y][player]=-1;
}
else{
int c=0;
state[x][y][player]=0;
for(auto i: g[y]){
if(calc(x, i, 0)==-1){
state[x][y][1]=i;
c++;
}
}
if(!c){
state[x][y][player]=-1;
for(auto i: g[x]){
for(auto l: g[y])
state[i][l][0]=x;
}
for(auto l: g[y])
state[x][l][0]=x;
}
}
return state[x][y][player];
};
int start(int n, bool a[MAX_N][MAX_N]) {
for(int i=0; i<n; i++){
for(int l=0; l<n; l++){
state[i][l][0]=-2;
state[i][l][1]=-2;
if(a[i][l]){
g[i].push_back(l);
}
}
}
for(int i=0; i<n; i++){
int flag=1;
for(int l=0; l<n; l++){
if(calc(i, l, 1)!=-1){
flag=0;
}
}
if(flag) return pos=i;
}
return -1;
}
int nextMove(int R) {
return pos = calc(pos, R, 0);
}