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: from collections import deque

def search(xs, ys, xg, yg, board):  
  direct = ((-1, 0), (1, 0), (0, -1), (0, 1))
  visited = [[False] * (w + 2) for _ in range(h + 2)]
  visited[ys][xs] = True
  que = deque()
  que.append((xs, ys))
  while que:
    x, y = que.popleft()
    for dx, dy in direct:
      nx, ny = x + dx, y + dy
      if (nx, ny) == (xg, yg):
        print("OK")
        return
      if not visited[ny][nx]:
        visited[ny][nx] = True
        if board[ny][nx] == color:
          que.append((nx, ny))
  else:
    print("NG")


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

  xs, ys = map(int, input().split())
  xg, yg = map(int, input().split())

  board = [[0] * (w + 2) for _ in range(h + 2)]
  n = int(input())
  for _ in range(n):
    c, d, x, y = map(int, input().split())
    if d == 0:
      for xi in range(x, x + 4):
        for yi in range(y, y + 2):
          board[yi][xi] = c
    else:
      for xi in range(x, x + 2):
        for yi in range(y, y + 4):
          board[yi][xi] = c
  
  color = board[ys][xs]
  if color == 0 or board[yg][xg] != color:
    print("NG")
    continue
  
  search(xs, ys, xg, yg, board)

Python: def block(board, y, x, direction, color):
    # ???
    if direction == 0:
        for i in range(2):
            for j in range(4):
                print(x + i, y + j)
                board[x + i][y + j] = color
    # ??±
    elif direction == 1:
        for i in range(2):
            for j in range(4):
                board[x + j][y + i] = color


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


def go_maze(board, s_x, s_y, g_x, g_y):
    place = board[s_x][s_y]
    if place == 0:
        return 'NG'
    x, y = s_x, s_y
    stack = []

    while True:
        # print(x, y)
        board[x][y] = place + 1
        if x + 1 < len(board) and board[x + 1][y] == place:
            x += 1
            stack.append((x, y))
        elif y + 1 < len(board[x]) and board[x][y + 1] == place:
            y += 1
            stack.append((x, y))
        elif x - 1 > 0 and board[x - 1][y] == place:
            x -= 1
            stack.append((x, y))
        elif y - 1 > 0 and board[x][y - 1] == place:
            y -= 1
            stack.append((x, y))
        else:
            if len(stack) == 0 and board[x + 1][y] != place and board[x][y + 1] != place and board[x - 1][y] != place and board[x][y - 1] != place:
                return 'NG'
            (x, y) = stack.pop()

        if x == g_x and y == g_y:
            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 + 1)] for _ in range(h + 1)]
        s_x, s_y = map(int, input().split())
        g_x, g_y = map(int, input().split())
        for _ in range(int(input())):
            color, direction, x, y = map(int, input().split())
            block(board, x, y, direction, color)

        print_board(board)
        # print(go_maze(board, s_y, s_x, g_y, g_x))


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()))
    if H == 0 and W == 0:
        break
    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')

C++: #include<iostream>
#include<map>
using namespace std;

bool dfs(int x,int y);
int w,h;
int xs,ys;
int xg,yg;
int board[111][111];
int cheak[111][111];
int xd[]={0,-1,0,1};
int yd[]={-1,0,1,0};
int main(){
  int n;
  int c,d,x,y;
  int a[]={1,0,1,0,1,0,1};
  int b[]={0,1,1,2,2,3,3};
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	board[i][j]=0;
	cheak[i][j]=0;
	cout << board[i][j];
      }
      cout << endl;
    }
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;
    for(int i=0;i<n;i++){
      cin >> c >> d >> x >> y;
      board[y-1][x-1]=c;
      for(int j=0;j<7;j++){
	if(d==0) board[y+a[j]-1][x+b[j]-1]=c;
	else board[y+b[j]-1][x+a[j]-1]=c;
      }
    }
    if(board[ys][xs]!=0){
      if(dfs(xs-1,ys-1)==true) cout << "OK" << endl;
      else cout << "NG" << endl;
    }
    else cout << "NG" << endl;
  }
}

bool dfs(int x,int y){
  cheak[y][x]=1;
  if(x==xg-1 && y==yg-1) return true;
  for(int i=0;i<4;i++){
    int xn=x+xd[i];
    int yn=y+yd[i];
    if(xn>=0 && xn<w && yn>=0 && yn<h && board[yn][xn]==board[ys-1][xs-1] && cheak[yn][xn]==0){
      if(dfs(xn,yn)==true) return true;
    }
  }
  return false;
}