This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <chrono>
#include <random>
#include <functional>
#define MAX_N 500
using namespace std;
const int M = 500 * 500 * 2;
int Nn, Cn;
int loose[M];
int win[M];
int cnt[M];
int turn[M];
int used[M];
int grid[MAX_N][MAX_N];
void dfs(int v) {
int n = Nn;
used[v] = 1;
{
int k = v;
int t = k / (n * n);
int st = k % (n * n);
int i = st / n;
int j = st % n;
if (t == 0) {
for (int to = 0; to < n; to++) {
if (grid[to][j]) {
int nw = n * n + i * n + to;
if (used[nw]) continue;
if (loose[v]) {
win[nw] = 1;
turn[nw] = v;
dfs(nw);
} else {
cnt[nw]--;
if (cnt[nw] == 0) {
loose[nw] = 1;
dfs(nw);
}
}
}
}
} else {
for (int to = 0; to < n; to++) {
if (grid[to][i] || (i == to)) {
int nw = to * n + j;
if (used[nw]) continue;
if (loose[v]) {
win[nw] = 1;
turn[nw] = v;
dfs(nw);
} else {
cnt[nw]--;
if (cnt[nw] == 0) {
loose[nw] = 1;
dfs(nw);
}
}
}
}
}
}
}
int start(int N, bool a[MAX_N][MAX_N]) {
int n = N;
Nn = N;
Cn = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
grid[i][j] = a[i][j];
}
}
for (int k = 0; k < 2 * n * n; k++) {
int t = k / (n * n);
int st = k % (n * n);
int i = st / n;
int j = st % n;
if (t == 0) {
for (int to = 0; to < n; to++) {
if (a[i][to] || i == to) {
cnt[k]++;
}
}
} else {
for (int to = 0; to < n; to++) {
if (a[j][to]) {
cnt[k]++;
}
}
}
}
for (int i = 0; i < n; i++) {
loose[n * n + i * n + i] = 1;
}
for (int i = 0; i < n; i++) {
win[i * n + i] = 1;
}
for (int i = 0; i < 2 * n * n; i++) {
if (!used[i] && (win[i] || loose[i])) {
dfs(i);
}
}
for (int i = 0; i < n; i++) {
int ok = 1;
for (int j = 0; j < n; j++) {
ok &= win[i * n + j];
}
if (ok) {
Cn = i;
return i;
}
}
return -1;
}
int nextMove(int r) {
if (!win[Cn * Nn + r]) {
return -1;
}
int next_state = turn[Cn * Nn + r];
next_state %= (Nn * Nn);
Cn = next_state / Nn;
return Cn;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |