Problem:
Problem B: Make a Sequence Your company’s next product will be a new game, which is a three-dimensional variant of the
classic game “Tic-Tac-Toe”. Two players place balls in a three-dimensional space (board), and
try to make a sequence of a certain length. People believe that it is fun to play the game, but they still cannot fix the values of some
parameters of the game. For example, what size of the board makes the game most exciting?
Parameters currently under discussion are the board size (we call it n in the following) and the
length of the sequence ( m ). In order to determine these parameter values, you are requested to
write a computer simulator of the game. You can see several snapshots of the game in Figures 1-3. These figures correspond to the three
datasets given in the Sample Input.   Figure 1: A game with n = m = 3  Here are the precise rules of the game.  Two players, Black and White, play alternately. Black plays first.  There are n × n vertical pegs. Each peg can accommodate up to n balls. A peg can be
     specified by its x - and y -coordinates (1 ≤ x , y ≤ n ). A ball on a peg can be specified by
     its z -coordinate (1 ≤ z ≤ n ). At the beginning of a game, there are no balls on any of the
     pegs.     Figure 2: A game with n = m = 3 (White made a 3-sequence before Black)   On his turn, a player chooses one of n × n pegs, and puts a ball of his color onto the peg.
   The ball follows the law of gravity. That is, the ball stays just above the top-most ball
   on the same peg or on the floor (if there are no balls on the peg). Speaking differently, a
   player can choose x - and y -coordinates of the ball, but he cannot choose its z -coordinate.  The objective of the game is to make an m -sequence. If a player makes an m -sequence or
   longer of his color, he wins. An m -sequence is a row of m consecutive balls of the same
   color. For example, black balls in positions (5, 1, 2), (5, 2, 2) and (5, 3, 2) form a 3-sequence.
   A sequence can be horizontal, vertical, or diagonal. Precisely speaking, there are 13
   possible directions to make a sequence, categorized as follows.     Figure 3: A game with n = 4, m = 3 (Black made two 4-sequences)   (a) One-dimensional axes. For example, (3, 1, 2), (4, 1, 2) and (5, 1, 2) is a 3-sequence.
  There are three directions in this category. (b) Two-dimensional diagonals. For example, (2, 3, 1), (3, 3, 2) and (4, 3, 3) is a 3-sequence.
    There are six directions in this category. (c) Three-dimensional diagonals. For example, (5, 1, 3), (4, 2, 4) and (3, 3, 5) is a 3-
    sequence. There are four directions in this category.  Note that we do not distinguish between opposite directions.  As the evaluation process of the game, people have been playing the game several times changing
the parameter values. You are given the records of these games. It is your job to write a computer
program which determines the winner of each recorded game. Since it is difficult for a human to find three-dimensional sequences, players often do not notice
the end of the game, and continue to play uselessly. In these cases, moves after the end of the
game, i.e. after the winner is determined, should be ignored. For example, after a player won
making an m -sequence, players may make additional m -sequences. In this case, all m -sequences
but the first should be ignored, and the winner of the game is unchanged. A game does not necessarily end with the victory of one of the players. If there are no pegs left
to put a ball on, the game ends with a draw. Moreover, people may quit a game before making
any m-sequence. In such cases also, the game ends with a draw. Input The input consists of multiple datasets each corresponding to the record of a game. A dataset
starts with a line containing three positive integers n , m , and p separated by a space. The
relations 3 ≤ m ≤ n ≤ 7 and 1 ≤ p ≤ n 3 hold between them. n and m are the parameter values
of the game as described above. p is the number of moves in the game. The rest of the dataset is p lines each containing two positive integers x and y . Each of these
lines describes a move, i.e. the player on turn puts his ball on the peg specified. You can assume
that 1 ≤ x ≤ n and 1 ≤ y ≤ n . You can also assume that at most n balls are put on a peg
throughout a game. The end of the input is indicated by a line with three zeros separated by a space. Output For each dataset, a line describing the winner and the number of moves until the game ends
should be output. The winner is either “Black” or “White”. A single space should be inserted
between the winner and the number of moves. No other extra characters are allowed in the
output. In case of a draw, the output line should be “Draw”. Sample Input 3 3 3
1 1
1 1
1 1
3 3 7
2 2
1 3
1 1
2 3
2 1
3 3
3 1
4 3 15
1 1
2 2
1 1
3 3
3 3
1 1
3 3
3 3
4 4
1 1
4 4
4 4
4 4
4 1
2 2
0 0 0 Output for the Sample Input Draw
White 6
Black 15

Solution (Python):
dx = [1,0,0,1, 1,1, 1,0, 0,1,-1, 1, 1]
dy = [0,1,0,1,-1,0, 0,1, 1,1, 1,-1, 1]
dz = [0,0,1,0, 0,1,-1,1,-1,1, 1, 1,-1]

def judge(B,x,y,z,color):
    for i in range(13):
        seq = B[z][y][x]
        nx,ny,nz = x+dx[i],y+dy[i],z+dz[i]
        while 0 <= min(nx,ny,nz) and max(nx,ny,nz) < n:
            seq += B[nz][ny][nx]
            nx += dx[i]; ny += dy[i]; nz += dz[i]
        nx,ny,nz = x-dx[i],y-dy[i],z-dz[i]
        while 0 <= min(nx,ny,nz) and max(nx,ny,nz) < n:
            seq = B[nz][ny][nx] + seq
            nx -= dx[i]; ny -= dy[i]; nz -= dz[i]
        if str(color)*m in seq: return True
    return False
    
while 1:
    n,m,p = map(int,raw_input().split())
    if n == 0: break
    B = [[["-"]*n for i in range(n)] for j in range(n)]
    isWin = False
    for move in xrange(p):
        x,y = map(int,raw_input().split())
        if isWin: continue

        x -= 1; y -= 1
        color = move%2
        for z in range(n):
            if B[z][y][x] == "-":
                B[z][y][x] = str(color)
                isWin = judge(B,x,y,z,color)
                break
        
        if isWin: print ["Black","White"][color],move+1
    if not isWin:
        print "Draw"

Solution (Python):
dx = [1,0,0,1, 1,1, 1,0, 0,1,-1, 1, 1]
dy = [0,1,0,1,-1,0, 0,1, 1,1, 1,-1, 1]
dz = [0,0,1,0, 0,1,-1,1,-1,1, 1, 1,-1]

def judge(B,X,Y,Z,color):
    for i in range(13):
        x,y,z = X,Y,Z
        while 0 <= min(x,y,z) and max(x,y,z) <= n-1:
            x,y,z = x-dx[i],y-dy[i],z-dz[i]
        x,y,z = x+dx[i],y+dy[i],z+dz[i] 
        seq = ""
        while 0 <= min(x,y,z) and max(x,y,z) < n:
            seq += B[z][y][x]
            x,y,z = x+dx[i],y+dy[i],z+dz[i]
        if str(color)*m in seq: return True
    return False
    
while 1:
    n,m,p = map(int,raw_input().split())
    if n == 0: break
    B = [[["-"]*n for i in range(n)] for j in range(n)]
    isWin = False
    for move in xrange(p):
        x,y = map(int,raw_input().split())
        if isWin: continue

        x -= 1; y -= 1
        color = move%2
        for z in range(n):
            if B[z][y][x] == "-":
                B[z][y][x] = str(color)
                isWin = judge(B,x,y,z,color)
                break
        
        if isWin: print ["Black","White"][color],move+1
    if not isWin:
        print "Draw"

Solution (Python):
dirs = [
    (1,0,0),(0,1,0),(0,0,1),
    (1,1,0),(1,-1,0),
    (0,1,1),(0,1,-1),
    (1,0,1),(-1,0,1),
    (1,1,1),(1,1,-1),
    (1,-1,1),(1,-1,-1)
]
def judge(x,y,z):
    c = pegs[x][y][z]
    for dx,dy,dz in dirs:
        sq = 1
        for _m in (1,-1):
            m = _m
            while True:
                mx,my,mz = x + m*dx, y + m*dy, z + m*dz
                if any(map(lambda l:not(0<=l<N), (mx,my,mz))): break
                if pegs[mx][my][mz] == c:
                    sq += 1
                    if sq >= M:
                        return c
                else:
                    break
                m += _m
    return 0

while True:
    N,M,P = map(int,input().split())
    if N == 0: break
    moves = [tuple(map(lambda x:int(x)-1, input().split())) for i in range(P)]
    pegs = [[[0 for z in range(N)] for y in range(N)] for x in range(N)]
    turn = 1
    for i,(x,y) in enumerate(moves):
        z = pegs[x][y].index(0)
        pegs[x][y][z] = turn
        turn *= -1
        c = judge(x,y,z)
        if c != 0:
            winner = 'Black' if c > 0 else 'White'
            print(winner + ' ' + str(i+1))
            break
    else:
        print('Draw')

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    dd = [(1,0,0),(0,1,0),(0,0,1),(1,1,0),(1,0,1),(0,1,1),(1,1,1),(1,-1,0),(1,0,-1),(0,1,-1),(1,1,-1),(1,-1,1),(-1,1,1)]

    while True:
        n,m,p = LI()
        if n == 0 and m == 0 and p == 0:
            break

        a = [LI() for _ in range(p)]
        r = 'Draw'
        d = collections.defaultdict(int)
        for i in range(p):
            ci,cj = a[i]
            t = i % 2 + 1
            ck = 0
            for k in range(p):
                if d[(ci,cj,k)] == 0:
                    d[(ci,cj,k)] = t
                    ck = k
                    break
            ff = False
            for di,dj,dk in dd:
                if ff:
                    break
                for k in range(1-m,1):
                    f = True
                    for kk in range(k,k+m):
                        ni = ci + di*kk
                        nj = cj + dj*kk
                        nk = ck + dk*kk
                        if d[(ni,nj,nk)] != t:
                            f = False
                            break
                    if f:
                        ff = True
                        break
            if ff:
                if t == 1:
                    r = 'Black {}'.format(i+1)
                else:
                    r = 'White {}'.format(i+1)
                break

        rr.append(r)


    return '\n'.join(map(str, rr))


print(main())

Solution (Python):
mv=(((1,0,0)  ,(-1,0,0))  ,
    ((0,1,0)  ,(0,-1,0))  ,
    ((0,0,1)  ,(0,0,-1))  ,
    ((1,1,0)  ,(-1,-1,0)) ,
    ((1,-1,0) ,(-1,1,0))  ,
    ((1,0,1)  ,(-1,0,-1)) ,
    ((1,0,-1) ,(-1,0,1))  ,
    ((0,1,1)  ,(0,-1,-1)) ,
    ((0,1,-1) ,(0,-1,1))  ,
    ((1,1,1)  ,(-1,-1,-1)),
    ((1,1,-1) ,(-1,-1,1)) ,
    ((1,-1,1) ,(-1,1,-1)) ,
    ((1,-1,-1),(-1,1,1)))

def check(L,y,x,z,color):
    for line in mv:
        cnt=1
        tx,ty,tz=x,y,z
        for dx,dy,dz in line:
            while (0<=x+dx<n) and (0<=y+dy<n) and (0<=z+dz<len(L[y+dy][x+dx])) and L[y+dy][x+dx][z+dz]==color:
                cnt+=1
                x+=dx
                y+=dy
                z+=dz
            x,y,z=tx,ty,tz
        if cnt>=m:
            return True
    return False

while True:
    n,m,p=map(int,raw_input().split())
    if n==m==p==0:break
    L=[[[] for i in range(n)] for i in range(n)]
    data=[map(int,raw_input().split()) for i in range(p)]
    for i in range(len(data)):
        x,y=data[i][0],data[i][1]
        L[y-1][x-1].append(i%2)
        z=len(L[y-1][x-1])
        if check(L,y-1,x-1,z-1,i%2):
            if i%2:
                print "White",i+1
            else:
                print "Black",i+1
            break
    else:
        print "Draw"

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
input=lambda : sys.stdin.readline().rstrip()

dx=[1,1,0,-1,1,1,0,-1,-1,-1,0,1,0]
dy=[0,1,1,1,0,1,1,1,0,-1,-1,-1,0]
dz=[0,0,0,0,1,1,1,1,1,1,1,1,1]

while True:
    n,m,q=map(int,input().split())
    if n==m==q==0:
        break
    field=[[[-1 for i in range(n)]for i in range(n)]for i in range(n)]
    d=True
    turn=1
    for t in range(q):
        x,y=map(int,input().split())
        if d==False:
            continue
        x,y=x-1,y-1
        z=n
        while z>0:
            if field[x][y][z-1]==-1:
                z-=1
            else:
                break
        field[x][y][z]=turn
        for i in range(13):
            tx,ty,tz=x,y,z
            count=0
            while 0<=tx<n and 0<=ty<n and 0<=tz<n:
                if field[tx][ty][tz]==turn:
                    count+=1
                else:
                    break
                tx,ty,tz=tx+dx[i],ty+dy[i],tz+dz[i]
            tx,ty,tz=x,y,z
            while 0<=tx<n and 0<=ty<n and 0<=tz<n:
                if field[tx][ty][tz]==turn:
                    count+=1
                else:
                    break
                tx,ty,tz=tx-dx[i],ty-dy[i],tz-dz[i]
            count-=1
            if count>=m:
                print('Black' if turn else 'White',t+1)
                d=False
                break
        turn=(turn+1)%2
    if d:
        print('Draw')