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 h + 1 < len(board) and board[h + 1][w] == place:
            h += 1
            stack.append((w, h))
        elif w + 1 < len(board[h]) and board[h][w + 1] == place:
            w += 1
            stack.append((w, h))
        elif h - 1 > 0 and board[h - 1][w] == place:
            h -= 1
            stack.append((w, h))
        elif w - 1 > 0 and 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(110)] for _ in range(110)]
        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))
        print_board(board)


if __name__ == '__main__':
    main()

Python: # ref: https://qiita.com/masashi127/items/0c794e28f4b295ad82c6
import heapq
import itertools


def astar(init_pos, goal,dg):
    passed_list = [init_pos]
    init_score = distance(passed_list) + heuristic(init_pos,goal)
    checked = {init_pos: init_score}
    searching_heap = []
    heapq.heappush(searching_heap, (init_score, passed_list))
    while len(searching_heap) > 0:
        score, passed_list = heapq.heappop(searching_heap)
        last_passed_pos = passed_list[-1]
        if last_passed_pos == goal:
            return passed_list
        for pos in nexts(dg,last_passed_pos):
            new_passed_list = passed_list + [pos]
            pos_score = distance(new_passed_list) + heuristic(pos,goal)
            if pos in checked and checked[pos] <= pos_score:
                continue
            checked[pos] = pos_score
            heapq.heappush(searching_heap, (pos_score, new_passed_list))
    return []

def solve_dungeon(dungeon):
    init = find_ch(dungeon,"S")
    goal = find_ch(dungeon,"G")
    path = astar(init, goal,dungeon)

    if len(path) > 0:
        return True
    else:
        return False

def find_ch(dg,ch):
    for i, l in enumerate(dg):
        for j, c in enumerate(l):
            if c == ch:
                return (i, j)

def nexts(dg,pos):
    wall = "x"
    for a, b in [[' + 1',''], [' - 1',''], ['',' + 1'], ['',' - 1']]:
        if a or b:
            if dg[eval('pos[0]' + a)][eval('pos[1]' + b)] != wall:
                yield (eval('pos[0]' + a), eval('pos[1]' + b))

def heuristic(pos,goal):
    return ((pos[0] - goal[0]) ** 2 + (pos[1] - goal[1]) ** 2) ** 0.5

def distance(path):
    return len(path)

def render_path(dg,path):
    buf = [[ch for ch in l] for l in dg]

    for pos in path[1:-1]:
        buf[pos[0]][pos[1]] = "*"

    buf[path[0][0]][path[0][1]] = "s"
    buf[path[-1][0]][path[-1][1]] = "g"
    return ["".join(l) for l in buf]

if __name__ == "__main__":
    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())
        d = [ list(map(int,input().split())) for _ in range(n)]
        m = [[0]*w for _ in range(h)]
        for e in d:
            for r in m[e[3]-1:e[3]+2*(e[1]+1)-1]:
                r[e[2]-1:e[2]+2*(2-e[1])-1] = [e[0]]*2*(2-e[1])
        ans = False
        for col in range(1,6):
            txt = [''.join(str(' ' if e==col else 'x') for e in f)  for f in m]
            if txt[ys-1][xs-1] == "x" or txt[yg-1][xg-1] == "x":
                continue
            txt[ys-1] = txt[ys-1][:xs-1] + 'S' + txt[ys-1][xs:]
            txt[yg-1] = txt[yg-1][:xg-1] + 'G' + txt[yg-1][xg:]
            for i in range(len(txt)):
                txt[i] = 'x' + txt[i] + 'x'
            txt.insert(0,'x'*(w+2))
            txt.append('x'*(w+2))
            ans |= solve_dungeon(txt)
            if ans:
                print("OK")
                break
        if not ans: print("NG")