結果

問題 No.2476 Knight Game
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-11-05 01:39:25
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 3,614 bytes
コンパイル時間 3,792 ms
コンパイル使用メモリ 109,136 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-11-05 01:39:32
合計ジャッジ時間 3,490 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
4,372 KB
testcase_01 AC 223 ms
4,372 KB
testcase_02 AC 262 ms
4,372 KB
testcase_03 AC 260 ms
4,372 KB
testcase_04 AC 285 ms
4,372 KB
testcase_05 AC 262 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }

string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


// !!!Assume bipartite (n vertices overall)!!!
class BMatch {
  int n;
  int[][] g;
  int[] to;
  bool[] vis;
  this(int n) {
    this.n = n; g = new int[][n]; to = new int[n]; vis = new bool[n];
  }
  void ae(int u, int v) {
    g[u] ~= v; g[v] ~= u;
  }
  bool augment(int u) {
    foreach (v; g[u]) if (!vis[v]) {
      vis[v] = true;
      if (to[v] == -1 || augment(to[v])) {
        to[u] = v; to[v] = u; return true;
      }
    }
    return false;
  }
  int solve() {
    int ret;
    to[] = -1;
    foreach (u; 0 .. n) if (to[u] == -1) {
      vis[] = false;
      if (augment(u)) ++ret;
    }
    return ret;
  }
}


enum LIM = 7;
bool[][][][] ANS;

bool solve(long m, long n, long x, long y) {
  if (m > n) {
    swap(m, n);
    swap(x, y);
  }
  if (m < LIM && n < LIM) {
    return ANS[m][n][x][y];
  } else if (m == 1) {
    return false;
  } else if (m == 2) {
    const nn = (n - 1 - y % 2) / 2 + 1;
    if (nn % 2 != 0) {
      return ((y / 2) % 2 != 0);
    } else {
      return true;
    }
  } else {
    if (m % 2 != 0 && n % 2 != 0) {
      return ((x + y) % 2 != 0);
    } else {
      return true;
    }
  }
}

void main() {
  ANS = new bool[][][][](LIM, LIM);
  foreach (m; 1 .. LIM) foreach (n; m .. LIM) {
    int calc(int x0, int y0) {
      auto bm = new BMatch(m * n);
      foreach (x; 0 .. m) foreach (y; 0 .. n) if (!(x0 == x && y0 == y)) {
        foreach (dx; -2 .. +2 + 1) foreach (dy; -2 .. +2 + 1) if (dx^^2 + dy^^2 == 5) {
          const xx = x + dx;
          const yy = y + dy;
          if (0 <= xx && xx < m && 0 <= yy && yy < n && !(x0 == xx && y0 == yy)) {
            bm.ae(x * n + y, xx * n + yy);
          }
        }
      }
      const res = bm.solve;
      return res;
    }
    ANS[m][n] = new bool[][](m, n);
    const ma = calc(-1, -1);
    foreach (x; 0 .. m) foreach (y; 0 .. n) {
      const ma1 = calc(x, y);
      ANS[m][n][x][y] = (ma > ma1);
    }
    debug {
      writeln(m, " ", n);
      foreach (x; 0 .. m) {
        foreach (y; 0 .. n) write(ANS[m][n][x][y] ? 1 : 0);
        writeln;
      }
    }
  }
  
  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const M = readLong;
        const N = readLong;
        const X = readLong - 1;
        const Y = readLong - 1;
        
        const ans = solve(M, N, X, Y);
        writeln(ans ? "Alice" : "Bob");
      }
    }
  } catch (EOFException e) {
  }
}
0