Problem:
Problem J: Usaneko Matrix

うさぎとねこが勝負をしている. ルールは以下の通りである.

まず2 匹はそれぞれn 行n 列の正方形状にn2 個の整数を紙に書き, トランプを1 枚ずつ引く. 次に, 1 から 1 000 000 までの数が1 つずつ書かれた1 000 000 枚のカードを2 匹でシャッフルし, これを1 枚ずつ交互に引いていく. 2 匹はカードが引かれるたび, カードと同じ数が自分の紙に書かれていたらそれに印をつける. 「印がついたn 個の数の組であって, 一直線上に並んでいるもの」の個数が, はじめに引いたトランプの数以上になることを勝利条件とする.

与えられたm 枚目のカードまでで, うさぎとねこのどちらが勝つか答えよ. ただし勝敗は, あるカードが引かれて印をつけ終わった段階で2 匹のうち片方のみが勝利条件をみたしたときに決まるものとし, それ以外の場合は引き分けとする. いずれかが勝利条件をみたした後でもカードが引かれることはあるが, これは勝敗に影響しない.

Input

1 行目：“n u v m” (正方形のサイズ, うさぎのトランプの数, ねこのトランプの数, 引かれるカードの枚数) 2-(N + 1) 行目：うさぎが紙に書くn2 個の数(N + 2)-(2N + 1) 行目：ねこが紙に書くn2 個の数(2N + 2)-(2N +M + 1) 行目：引かれるm 枚のカード

1 ≤ n ≤ 500

1 ≤ u, v ≤ 13

1 ≤ m ≤ 100 000

1 ≤ (書かれる数) ≤ 1 000 000

うさぎが紙に書くn2 個の数, ねこが紙に書くn2 個の数, 引かれるm 枚のカードに書かれた数はそれぞれの中で異なる.

Output

うさぎが勝つ場合には”USAGI”を, ねこが勝つ場合には”NEKO”を, 引き分けならば”DRAW”, それぞれ
一行に出力せよ.

Sample Input 1

3 2 2 10
1 2 3
4 5 6
7 8 9
1 2 3
6 5 4
7 8 9
11
4
7
5
10
9
2
1
3
8

Sample Output 1

USAGI

Sample Input 2

3 2 1 10
1 2 3
4 5 6
7 8 9
1 2 3
6 5 4
7 8 9
11
4
7
5
10
9
2
1
3
8

Sample Output 2

DRAW

Solution in C++ (judge verdict: Accepted)

#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int diff_row_hidariue[3] = {0,1,1},diff_col_hidariue[3] = {1,0,1};
int diff_row_migiue[2] = {1,1},diff_col_migiue[2] = {-1,0};

int n,u,v,m;

struct Info{
	int row,col,value;
	bool exist,checked;
};

struct Data{
	Data(int arg_row,int arg_col){
		row = arg_row;
		col = arg_col;
	}

	int row,col;
};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= n-1 && col >= 0 && col <= n-1)return true;
	else{
		return false;
	}
}


int main(){

	scanf("%d %d %d %d",&n,&u,&v,&m);

	Info* loc_USAGI = (Info*)malloc(sizeof(Info)*(1000001));
	Info* loc_NEKO = (Info*)malloc(sizeof(Info)*(1000001));

	for(int i = 1; i <= 1000000; i++){
		loc_USAGI[i].exist = false;
		loc_NEKO[i].exist = false;
	}

	Info** map_USAGI = (Info**)malloc(sizeof(Info*)*n);
	Info** map_NEKO = (Info**)malloc(sizeof(Info*)*n);
	for(int i = 0; i < n; i++){
		map_USAGI[i] = (Info*)malloc(sizeof(Info)*n);
		map_NEKO[i] = (Info*)malloc(sizeof(Info)*n);
	}

	bool** to_Migi_USAGI = new bool*[n];
	bool** to_Migi_NEKO = new bool*[n];
	bool** to_Shita_USAGI = new bool*[n];
	bool** to_Shita_NEKO = new bool*[n];
	bool** to_Migishita_USAGI = new bool*[n];
	bool** to_Migishita_NEKO = new bool*[n];
	bool** to_Hidarishita_USAGI = new bool*[n];
	bool** to_Hidarishita_NEKO= new bool*[n];
	for(int i = 0; i < n; i++){
		to_Migi_USAGI[i] = new bool[n];
		to_Migi_NEKO[i] = new bool[n];
		to_Shita_USAGI[i] = new bool[n];
		to_Shita_NEKO[i] = new bool[n];
		to_Migishita_USAGI[i] = new bool[n];
		to_Migishita_NEKO[i] = new bool[n];
		to_Hidarishita_USAGI[i] = new bool[n];
		to_Hidarishita_NEKO[i] = new bool[n];
		for(int k = 0; k < n; k++){
			to_Migi_USAGI[i][k] = false;
			to_Migi_NEKO[i][k] = false;
			to_Shita_USAGI[i][k] = false;
			to_Shita_NEKO[i][k] = false;
			to_Migishita_USAGI[i][k] = false;
			to_Migishita_NEKO[i][k] = false;
			to_Hidarishita_USAGI[i][k] = false;
			to_Hidarishita_NEKO[i][k] = false;
		}
	}

	for(int row = 0; row < n; row++){
		for(int col = 0; col < n; col++){
			map_USAGI[row][col].checked = false;
			map_NEKO[row][col].checked = false;
		}
	}

	for(int row = 0; row < n; row++){
		for(int col = 0; col < n; col++){
			scanf("%d",&map_USAGI[row][col].value);
			loc_USAGI[map_USAGI[row][col].value].exist = true;
			loc_USAGI[map_USAGI[row][col].value].row = row;
			loc_USAGI[map_USAGI[row][col].value].col = col;
		}
	}

	for(int row = 0; row < n; row++){
		for(int col = 0; col < n; col++){
			scanf("%d",&map_NEKO[row][col].value);
			loc_NEKO[map_NEKO[row][col].value].exist = true;
			loc_NEKO[map_NEKO[row][col].value].row = row;
			loc_NEKO[map_NEKO[row][col].value].col = col;
		}
	}

	vector<Data> Usagi_D,Neko_D;

	int tmp,usagi_line,neko_line,count,start_row,start_col,tmp_row,tmp_col;
	int usagi_check_num = 0,neko_check_num = 0;

	bool usagi_FLG,neko_FLG;

	for(int loop = 0; loop < m; loop++){
		scanf("%d",&tmp);

		usagi_FLG = false;
		neko_FLG = false;

		if(loc_USAGI[tmp].exist == true){
			usagi_check_num++;
			usagi_FLG = true;
			map_USAGI[loc_USAGI[tmp].row][loc_USAGI[tmp].col].checked = true;

			if(loc_USAGI[tmp].row == 0 || loc_USAGI[tmp].col == 0){
				Usagi_D.push_back(Data(loc_USAGI[tmp].row,loc_USAGI[tmp].col));
			}
		}
		if(loc_NEKO[tmp].exist == true){
			neko_check_num++;
			neko_FLG = true;
			map_NEKO[loc_NEKO[tmp].row][loc_NEKO[tmp].col].checked = true;

			if(loc_NEKO[tmp].row == 0 || loc_NEKO[tmp].col == 0){
				Neko_D.push_back(Data(loc_NEKO[tmp].row,loc_NEKO[tmp].col));
			}
		}

		usagi_line = 0;
		if(usagi_FLG && usagi_check_num >= n){
			for(int i = 0; i < Usagi_D.size(); i++){
				start_row = Usagi_D[i].row;
				start_col = Usagi_D[i].col;

				if(n > 1){

					if(start_row == 0 && start_col == 0){
						for(int k = 0; k < 3; k++){
							tmp_row = start_row;
							tmp_col = start_col;
							count = 0;

							switch(k){
							case 0:
								if(to_Migi_USAGI[start_row][start_col]){
									usagi_line++;
									continue;
								}
								break;
							case 1:
								if(to_Shita_USAGI[start_row][start_col]){
									usagi_line++;
									continue;
								}
								break;
							case 2:
								if(to_Migishita_USAGI[start_row][start_col]){
									usagi_line++;
									continue;
								}
								break;
							}

							while(rangeCheck(tmp_row,tmp_col) == true && map_USAGI[tmp_row][tmp_col].checked == true){
								count++;
								tmp_row = tmp_row + diff_row_hidariue[k];
								tmp_col = tmp_col + diff_col_hidariue[k];
							}
							if(count >= n){
								usagi_line++;

								switch(k){
								case 0:
									to_Migi_USAGI[start_row][start_col] = true;
									break;
								case 1:
									to_Shita_USAGI[start_row][start_col] = true;
									break;
								case 2:
									to_Migishita_USAGI[start_row][start_col] = true;
									break;
								}
							}
						}
					}else if(start_row == 0 && start_col == n-1){
						for(int k = 0; k < 2; k++){
							tmp_row = start_row;
							tmp_col = start_col;
							count = 0;

							if(k == 0){
								if(to_Hidarishita_USAGI[start_row][start_col]){
									usagi_line++;
									continue;
								}
							}else{
								if(to_Shita_USAGI[start_row][start_col]){
									usagi_line++;
									continue;
								}
							}


							while(rangeCheck(tmp_row,tmp_col) == true && map_USAGI[tmp_row][tmp_col].checked == true){
								count++;
								tmp_row = tmp_row + diff_row_migiue[k];
								tmp_col = tmp_col + diff_col_migiue[k];
							}
							if(count >= n){
								usagi_line++;

								if(k == 0){
									to_Hidarishita_USAGI[start_row][start_col] = true;
								}else{
									to_Shita_USAGI[start_row][start_col] = true;
								}
							}
						}
					}else if(start_row == 0){
						tmp_row = start_row;
						tmp_col = start_col;
						count = 0;
						if(to_Shita_USAGI[start_row][start_col]){
							usagi_line++;
						}else{
							while(rangeCheck(tmp_row,tmp_col) == true && map_USAGI[tmp_row][tmp_col].checked == true){
								count++;
								tmp_row = tmp_row + diff_row_hidariue[1];
								tmp_col = tmp_col + diff_col_hidariue[1];
							}
							if(count >= n){
								usagi_line++;
								to_Shita_USAGI[start_row][start_col] = true;
							}
						}
					}else{ //tmp_col == 0
						tmp_row = start_row;
						tmp_col = start_col;
						count = 0;
						if(to_Migi_USAGI[start_row][start_col]){
							usagi_line++;
						}else{
							while(rangeCheck(tmp_row,tmp_col) == true && map_USAGI[tmp_row][tmp_col].checked == true){
								count++;
								tmp_row = tmp_row + diff_row_hidariue[0];
								tmp_col = tmp_col + diff_col_hidariue[0];
							}
							if(count >= n){
								usagi_line++;
								to_Migi_USAGI[start_row][start_col] = true;
							}
						}
					}
				}else{
					usagi_line = 1;
				}
			}
		}

		neko_line = 0;
		if(neko_FLG && neko_check_num >= n){
			for(int i = 0; i < Neko_D.size(); i++){
				start_row = Neko_D[i].row;
				start_col = Neko_D[i].col;

				if(n > 1){
					if(start_row == 0 && start_col == 0){
						for(int k = 0; k < 3; k++){
							tmp_row = start_row;
							tmp_col = start_col;
							count = 0;

							switch(k){
							case 0:
								if(to_Migi_NEKO[start_row][start_col]){
									neko_line++;
									continue;
								}
								break;
							case 1:
								if(to_Shita_NEKO[start_row][start_col]){
									neko_line++;
									continue;
								}
								break;
							case 2:
								if(to_Migishita_NEKO[start_row][start_col]){
									neko_line++;
									continue;
								}
								break;
							}

							while(rangeCheck(tmp_row,tmp_col) == true && map_NEKO[tmp_row][tmp_col].checked == true){
								count++;
								tmp_row = tmp_row + diff_row_hidariue[k];
								tmp_col = tmp_col + diff_col_hidariue[k];
							}
							if(count >= n){
								neko_line++;

								switch(k){
								case 0:
									to_Migi_NEKO[start_row][start_col] = true;
									break;
								case 1:
									to_Shita_NEKO[start_row][start_col] = true;
									break;
								case 2:
									to_Migishita_NEKO[start_row][start_col] = true;
									break;
								}
							}
						}
					}else if(start_row == 0 && start_col == n-1){
						for(int k = 0; k < 2; k++){
							tmp_row = start_row;
							tmp_col = start_col;
							count = 0;

							if(k == 0){
								if(to_Hidarishita_NEKO[start_row][start_col]){
									neko_line++;
									continue;
								}
							}else{
								if(to_Shita_NEKO[start_row][start_col]){
									neko_line++;
									continue;
								}
							}


							while(rangeCheck(tmp_row,tmp_col) == true && map_NEKO[tmp_row][tmp_col].checked == true){
								count++;
								tmp_row = tmp_row + diff_row_migiue[k];
								tmp_col = tmp_col + diff_col_migiue[k];
							}
							if(count >= n){
								neko_line++;

								if(k == 0){
									to_Hidarishita_NEKO[start_row][start_col] = true;
								}else{
									to_Shita_NEKO[start_row][start_col] = true;
								}
							}
						}
					}else if(start_row == 0){
						tmp_row = start_row;
						tmp_col = start_col;
						count = 0;
						if(to_Shita_NEKO[start_row][start_col]){
							neko_line++;
						}else{
							while(rangeCheck(tmp_row,tmp_col) == true && map_NEKO[tmp_row][tmp_col].checked == true){
								count++;
								tmp_row = tmp_row + diff_row_hidariue[1];
								tmp_col = tmp_col + diff_col_hidariue[1];
							}
							if(count >= n){
								neko_line++;
								to_Shita_NEKO[start_row][start_col] = true;
							}
						}
					}else{ //tmp_col == 0
						tmp_row = start_row;
						tmp_col = start_col;
						count = 0;
						if(to_Migi_NEKO[start_row][start_col]){
							neko_line++;
						}else{

							while(rangeCheck(tmp_row,tmp_col) == true && map_NEKO[tmp_row][tmp_col].checked == true){
								count++;
								tmp_row = tmp_row + diff_row_hidariue[0];
								tmp_col = tmp_col + diff_col_hidariue[0];
							}
							if(count >= n){
								neko_line++;
								to_Migi_NEKO[start_row][start_col] = true;
							}
						}
					}

				}else{
					neko_line = 1;
				}
			}
		}



		if(usagi_line >= u && neko_line >= v){
			printf("DRAW\n");
			return 0;
		}else if(usagi_line >= u && neko_line < v){
			printf("USAGI\n");
			return 0;
		}else if(usagi_line < u && neko_line >= v){
			printf("NEKO\n");
			return 0;
		}
	}

	printf("DRAW\n");

	return 0;
}