Eat the Chip (Codeforces 1921E)
Tags: Greedy, games

Alice and Bob are playing a game on a checkered board. The board has $$$h$$$ rows, numbered from top to bottom, and $$$w$$$ columns, numbered from left to right. Both players have a chip each. Initially, Alice's chip is located at the cell with coordinates $$$(x_a, y_a)$$$ (row $$$x_a$$$, column $$$y_a$$$), and Bob's chip is located at $$$(x_b, y_b)$$$. It is guaranteed that the initial positions of the chips do not coincide. Players take turns making moves, with Alice starting.On her turn, Alice can move her chip one cell down or one cell down-right or down-left (diagonally). Bob, on the other hand, moves his chip one cell up, up-right, or up-left. It is not allowed to make moves that go beyond the board boundaries.More formally, if at the beginning of Alice's turn she is in the cell with coordinates $$$(x_a, y_a)$$$, then she can move her chip to one of the cells $$$(x_a + 1, y_a)$$$, $$$(x_a + 1, y_a - 1)$$$, or $$$(x_a + 1, y_a + 1)$$$. Bob, on his turn, from the cell $$$(x_b, y_b)$$$ can move to $$$(x_b - 1, y_b)$$$, $$$(x_b - 1, y_b - 1)$$$, or $$$(x_b - 1, y_b + 1)$$$. The new chip coordinates $$$(x', y')$$$ must satisfy the conditions $$$1 \le x' \le h$$$ and $$$1 \le y' \le w$$$.   Example game state. Alice plays with the white chip, Bob with the black one. Arrows indicate possible moves. A player immediately wins if they place their chip in a cell occupied by the other player's chip. If either player cannot make a move (Alice—if she is in the last row, i.e. $$$x_a = h$$$, Bob—if he is in the first row, i.e. $$$x_b = 1$$$), the game immediately ends in a draw.What will be the outcome of the game if both opponents play optimally?

Input: Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. This is followed by the description of the test cases.Each test case consists of a single line containing six integers $$$h$$$, $$$w$$$, $$$x_a$$$, $$$y_a$$$, $$$x_b$$$, $$$y_b$$$ ($$$1 \le x_a, x_b \le h \le 10^6$$$, $$$1 \le y_a, y_b \le w \le 10^9$$$) — the dimensions of the board and the initial positions of Alice's and Bob's chips. It is guaranteed that either $$$x_a \ne x_b$$$ or $$$y_a \ne y_b$$$.It is guaranteed that the sum of $$$h$$$ over all test cases does not exceed $$$10^6$$$.

Output: For each test case, output "Alice" if Alice wins, "Bob" if Bob wins, and "Draw" if neither player can secure a victory. You can output each letter in any case (lowercase or uppercase). For example, the strings "bOb", "bob", "Bob", and "BOB" will be accepted as Bob's victory.


Sample Inputs:
12
6 5 2 2 5 3
4 1 2 1 4 1
1 4 1 3 1 1
5 5 1 4 5 2
4 4 1 1 4 4
10 10 1 6 10 8
10 10 2 6 10 7
10 10 9 1 8 1
10 10 8 1 10 2
10 10 1 1 2 1
10 10 1 3 4 1
10 10 3 1 1 1

Sample Outputs:
Alice
Bob
Draw
Draw
Draw
Alice
Draw
Draw
Bob
Alice
Alice
Draw

Solution (Python):
import sys
from math import *
input = sys.stdin.readline

def inp():
    return int(input())

def inlt():
    return list(map(int, input().split()))

def insr():
    s = input()
    return list(s[:len(s) - 1])

def invr():
    return map(int, input().split())

def solve():
    (h, w, xA, yA, xB, yB) = invr()
    if (xA - xB) % 2 == 0:
        winner = 'Bob'
        if xA >= xB:
            win = False
        elif yA == yB:
            win = True
        else:
            if yA < yB:
                n_turns = yB - 1
            else:
                n_turns = w - yB
            win = xB - 2 * n_turns >= xA
    else:
        winner = 'Alice'
        xA += 1
        yA += 0 if yB - yA == 0 else 1 if yB - yA > 0 else -1
        if xA > xB:
            win = False
        elif yA == yB:
            win = True
        else:
            if yA < yB:
                n_turns = w - yA
            else:
                n_turns = yA - 1
            win = xB - 2 * n_turns >= xA
    print(winner if win else 'Draw')
if __name__ == '__main__':
    t = inp()
    for i in range(t):
        solve()

Solution (Python):
import sys

def solution(h, w, xa, ya, xb, yb):
    if xa >= xb:
        return 'Draw'
    if not (xb - xa) % 2:
        t = (xb - xa) // 2
        if ya > yb:
            return 'Draw' if yb + t < min(w, ya + t) else 'Bob'
        else:
            return 'Draw' if yb - t > max(1, ya - t) else 'Bob'
    else:
        t = (xb - xa) // 2
        if ya < yb:
            return 'Draw' if ya + t + 1 < min(w, yb + t) else 'Alice'
        else:
            return 'Draw' if ya - t - 1 > max(1, yb - t) else 'Alice'
for (i, line) in enumerate(sys.stdin):
    if i == 0:
        continue
    line = map(int, line.strip().split())
    sys.stdout.write(solution(*line) + '\n')

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (h, w, xa, ya, xb, yb) = readInts()
    if xa >= xb:
        return 'Draw'
    else:
        d = xb - xa
        if d == 1:
            return 'Alice' if abs(ya - yb) <= 1 else 'Draw'
        elif d % 2:
            if ya == yb:
                return 'Alice'
            else:
                ya -= 1 if yb < ya else -1
                if ya == yb:
                    return 'Alice'
                else:
                    da = ya - 1 if yb <= ya else w - ya
                    return 'Draw' if da > d // 2 else 'Alice'
        elif ya == yb:
            return 'Bob'
        else:
            db = yb - 1 if ya <= yb else w - yb
            return 'Draw' if db > d // 2 else 'Bob'
for _ in range(int(input())):
    print(solve())