Go: package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

var (
	dx = []int{0, -1, 0, 1}
	dy = []int{1, 0, -1, 0}
)

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		w, _ := strconv.Atoi(buf[0])
		h, _ := strconv.Atoi(buf[1])
		if w == 0 && h == 0 {
			break
		}
		scanner.Scan()
		buf_ := strings.Split(scanner.Text(), " ")
		xs, _ := strconv.Atoi(buf_[0])
		ys, _ := strconv.Atoi(buf_[1])
		scanner.Scan()
		buf__ := strings.Split(scanner.Text(), " ")
		xg, _ := strconv.Atoi(buf__[0])
		yg, _ := strconv.Atoi(buf__[1])
		xs--
		ys--
		xg--
		yg--
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		field := make([][]int, 100)
		for i := range  field {
			field[i] = make([]int, 100)
		}
		visited := make([][]bool, 100)
		for i := range visited {
			visited[i] = make([]bool, 100)
		}
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf___ := strings.Split(scanner.Text(), " ")
			c, _ := strconv.Atoi(buf___[0])
			d, _ := strconv.Atoi(buf___[1])
			x, _ := strconv.Atoi(buf___[2])
			y, _ := strconv.Atoi(buf___[3])
			x--
			y--
			switch d {
			case 0:
				for j := 0; j < 4; j++ {
					for k := 0; k < 2; k++ {
						field[x+j][y+k] = c
					}
				}
			case 1:
				for j := 0; j < 2; j++ {
					for k := 0; k < 4; k++ {
						field[x+j][y+k] = c
					}
				}
			}
		}
		if field[xs][ys] != field[xg][yg] || // スタートとゴールの色が等しくないと"NG"
			field[xs][ys] == 0 { // スタートの色が無職
			fmt.Println("NG")
			continue
		}
		Q := make([]Entry, 0)
		visited[xs][ys] = true
		Q = append(Q, Entry{xs, ys})
		for len(Q) > 0 {
			row := Q[0].x
			col := Q[0].y
			Q = Q[1:]
			for i := 0; i < 4; i++ {
				if 0 <= row+dx[i] && row+dx[i] < w &&
					0 <= col+dy[i] && col+dy[i] < h && // 移動可能な範囲内かどうか
					!visited[row + dx[i]][col + dy[i]] && // 訪問済みか
					field[row+dx[i]][col+dy[i]]==field[row][col] { // 色が同じかどうか
						visited[row+dx[i]][col+dy[i]] = true
						Q = append(Q, Entry{row+dx[i],col+dy[i]})
				}
			}
		}
		if visited[xg][yg] {
			fmt.Println("OK")
		} else {
			fmt.Println("NG")
		}
	}
}

type Entry struct {
	x, y int
}

Python: """ Created by Jieyi on 9/20/16. """


def block(board, w, h, direction, color):
    # horizontal.
    if direction == 0:
        for i in range(2):
            for j in range(4):
                board[h + i][w + j] = color
    # vertical.
    elif direction == 1:
        for i in range(2):
            for j in range(4):
                board[h + j][w + i] = color


def print_board(board):
    for i in range(len(board)):
        print(board[i])


def go_maze(board, s_w, s_h, g_w, g_h):
    place = board[s_h][s_w]
    if place == 0:
        return 'NG'
    w, h = s_w, s_h
    stack = []

    while True:
        board[h][w] = place + 1
        if board[h + 1][w] == place:
            h += 1
            stack.append((w, h))
        elif board[h][w + 1] == place:
            w += 1
            stack.append((w, h))
        elif board[h - 1][w] == place:
            h -= 1
            stack.append((w, h))
        elif board[h][w - 1] == place:
            w -= 1
            stack.append((w, h))
        else:
            if len(stack) == 0 and board[h + 1][w] != place and board[h][w + 1] != place and board[h - 1][w] != place and board[h][w - 1] != place:
                return 'NG'
            (w, h) = stack.pop()

        if w == g_w and h == g_h:
            return 'OK'


def main():
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break

        board = [[0 for _ in range(w)] for _ in range(h)]
        s_w, s_h = map(int, input().split())
        g_w, g_h = map(int, input().split())
        for _ in range(int(input())):
            color, direction, w, h = map(int, input().split())
            block(board, w, h, direction, color)

        # print_board(board)
        print(go_maze(board, s_w, s_h, g_w, g_h))


if __name__ == '__main__':
    main()

Python: lx,ly = [4,2],[2,4]
dx = [-1,0,1,0]
dy = [0,-1,0,1]
while 1:
    w,h = map(int, raw_input().split())
    if w==0 and h==0: break
    xs,ys = map(int, raw_input().split())
    xg,yg = map(int, raw_input().split())
    f = [[0 for i in range(102)] for j in range(102)]
    s = []
    n = input()
    for i in range(n):
        c,d,x,y = map(int, raw_input().split())
        for p in range(lx[d]):
            for q in range(ly[d]):
                f[y+q][x+p] = c
    ans = 0
    if f[ys][xs]>0 and f[ys][xs]==f[yg][xg]:
        c = f[ys][xs]
        s.append([xs,ys])
        while len(s)>0:
            x,y = s.pop()
            if x==xg and y==yg:
                ans = 1; break;
            for k in range(4):
                if f[y+dy[k]][x+dx[k]]==c:
                    s.append([x+dx[k],y+dy[k]])
                    f[y+dy[k]][x+dx[k]] = 0
    print ("OK" if ans==1 else "NG")

Python: BLOCK_WIDTH = 4
BLOCK_HEIGHT = 2
field = []
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
w = 0
h = 0
xg = 0
yg = 0

def main():
    global field, w, h, xg, yg
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break
        xs, ys = map(int, input().split())
        xg, yg = map(int, input().split())
        n = int(input())
        field = [[0] * w for _ in range(h)]
        for i in range(n):
            c, d, x, y = map(int, input().split())
            arrangement(c, d, x, y)

        if dfs(field[ys][xs], xs, ys):
            print("OK")
        else:
            print("NG")


def arrangement(c, d, x, y):
    global field
    if d == 0:
        [[draw(x + j, y + i, c) for j in range(BLOCK_WIDTH)] for i in range(BLOCK_HEIGHT)]
    else:
        [[draw(x + j, y + i, c) for j in range(BLOCK_HEIGHT)] for i in range(BLOCK_WIDTH)]


def draw(x, y, c):
    field[y][x] = c


def dfs(c, x, y):
    global field

    if x == xg and y == yg:
        return True
    field[y][x] = -1
    for i in range(4):
        next_x = x + dx[i]
        next_y = y + dy[i]
        if next_x < 0 or next_x >= w or next_y < 0 or next_y >= h:
            continue
        if field[next_y][next_x] == c:
            if dfs(c, next_x, next_y):
                return True

    return False


if __name__ == '__main__':
    main()

Python: import numpy as np

def dfs(x, y, m, v, W, H, xg, yg):
    '''
    '''
    v[y, x] = True
    for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        nx = x + dx
        ny = y + dy
        if nx in range(W) and ny in range(H) and m[ny, nx] == m[y, x] and v[ny, nx] == False:
            dfs(nx, ny, m, v, W, H, xg, yg)

while True:
    H, W = list(map(int, input().split()))
    xs, ys = list(map(lambda x: int(x)-1, input().split()))
    xg, yg = list(map(lambda x: int(x)-1, input().split()))
    n = int(input())
    m = np.array([[0 for _ in range(W)] for __ in range(H)])
    v = np.array([[False for _ in range(W)] for __ in range(H)])

    for _ in range(n):
        c, d, x, y = list(map(int, input().split()))
        x -= 1
        y -= 1
        if d == 0:
            m[y:y+2, x:x+4] = c
        else:
            m[y:y+4, x:x+2] = c


    dfs(xs, ys, m, v, W, H, xg, yg)
    if v[yg, xg]:
        print('OK')
    else:
        print('NG')

    # print(m)
    # print(v)