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
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
int w, h;

int check(int xs, int ys, int xg, int yg ,int c, int board[110][110]) {
	int used[110][110] = { 0 };
	queue<pl> q;
	q.push({ xs, ys });

	while (!q.empty()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		for (int i = 0; i < 4; i++) {
			int ddx = dx[i] + x;
			int ddy = dy[i] + y;
			if (ddx >= 1 && ddx <= w && ddy >= 1 && ddy <= h && !used[ddy][ddx]) {
				used[ddy][ddx] = 1;
				if (ddy == yg && ddx == xg)return 1;
				else if (board[ddy][ddx] == c) {
					q.push({ddx,ddy});
				}
			}
		}
	}
	return 0;
}

int main()
{
	while (cin >> w >> h, w, h) 
	{
		int xs, ys, xg, yg;
		int n;
		int board[110][110] = { 0 };
		cin >> xs >> ys >> xg >> yg;
		cin >> n;
		for (int i = 0; i < n; i++)
		{ 
			int c,d, x, y;
			cin >> c >> d >> x >> y;
			if (!d){
				for (int i = y; i < y+2; i++) {
					for (int j = x; j < x+8; j++) 
						board[i][j] = c;
				}
			}
			else {
				for (int i = y; i < y+8; i++) {
					for (int j = x; j < x+2; j++)
						board[i][j] = c;
				}
			}
		}
		if (check(xs, ys, xg, yg, board[ys][xs], board))
			cout << "OK" << endl;
		else
			cout << "NG" << endl;
	}
	return 0;
}

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

int map[102][102];

void route(int w, int h, int color) {
	if (map[w][h] != color)
		return;
	map[w][h] = 0;
	route(w, h + 1, color);
	route(w + 1, h, color);
	route(w, h - 1, color);
	route(w - 1, h, color);
}


int main() {
	int h, w, xs, ys, xg, yg, n;
	int c, d, x, y,color;

	while (1) {
		cin >> w >> h >> xs >> ys >> xg >> yg >> n;
		if (w == 0 && h == 0)
			break;

		for (int i = 0; i<n; i++) {
			cin >> c >> d >> x >> y;
			if (d == 0) {
				for (int j = 0; j < 4; j++)
					map[j + x][y] = map[j + x][y + 1] = c;
			}
			else {
				for (int j = 0; j < 4; j++)
					map[x][y + j] = map[x + 1][y + j] = c;
			}
		}
		color = map[xs][ys];
		if(color!=0)
			route(xs, ys, color);
		if (map[xg][yg]==0)
			cout << "OK" << endl;
		else
			cout << "NG"<<endl;
	}

	return 0;
}

C++: #include <stdio.h>
#include <math.h>
#include <algorithm>
#include <queue>

using namespace std;

int width,height,table[100][100];

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

void recursive(int row,int col){
	int tmp = table[row][col];
	table[row][col] = 7;

	if(rangeCheck(row-1,col) == true && table[row-1][col] == tmp)recursive(row-1,col);
	if(rangeCheck(row+1,col) == true && table[row+1][col] == tmp)recursive(row+1,col);
	if(rangeCheck(row,col-1) == true && table[row][col-1] == tmp)recursive(row,col-1);
	if(rangeCheck(row,col+1) == true && table[row][col+1] == tmp)recursive(row,col+1);
}

int main(){

	int x_s,y_s,x_g,y_g,n,color,dist,x,y;

	while(true){
		scanf("%d %d",&width,&height);
		if(width == 0 && height == 0)break;

		for(int i = 0; i < height; i++)
		{
			for(int k = 0; k < width; k++)table[i][k] = 0;
		}

		scanf("%d %d",&x_s,&y_s);
		scanf("%d %d",&x_g,&y_g);

		scanf("%d",&n);

		for(int i = 0; i < n; i++){
			scanf("%d %d %d %d",&color,&dist,&x,&y);

			if(dist == 0){
				for(int row = 0; row < 2; row++){
					for(int col = 0; col < 4; col++){
						table[y-1+row][x-1+col] = color;
					}
				}
			}else{
				for(int row = 0; row < 4; row++){
					for(int col = 0; col < 2; col++){
						table[y-1+row][x-1+col] = color;
					}
				}
			}
		}
		recursive(y_s-1,x_s-1);

		if(table[x_g-1][y_g-1] == 7)printf("OK\n");
		else{
			printf("NG\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FFOR(i,a,b) for(int i=(a);i<=(b);i++)
#define REP(i,b) FOR(i,0,b)
#define RREP(i,b) FFOR(i,1,b)
#define PB push_back
#define F first
#define S second
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef LL ut;
typedef long double ld;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut> VI;
typedef pair<ut,pr> ppr;
typedef priority_queue<pr,Vpr, greater<pr> > PQ;
const int SIZE=5+3*1e+4;
const ut INF=1<<30;
const ld eps=1e-6;
const LL p=7+1e+9;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int W, H;
int xs, ys, xg, yg;
int N, C;
int maze[128][128];

int dfs(int x, int y){
	if(x == xg && y == yg)
		return 1;
	maze[y][x] = 0;
	int res = 0;
	REP(i,4){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(nx > 0 && nx <= W && ny > 0 && ny <= H && maze[ny][nx] == C){
			res |= dfs(nx, ny);
		}
	}
	return res;
}

int main(){
	while(cin >> W >> H && (W || H)){
		memset(maze, 0, sizeof(maze));
		cin >> xs >> ys >> xg >> yg >> N;
		int c, d, x, y;
		REP(i,N){
			cin >> c >> d >> x >> y;
			if(d == 0){
				REP(j,2)
					REP(k,4)
						maze[y + j][x + k] = c;
			}
			else{
				REP(j,4)
					REP(k,2)
						maze[y + j][x + k] = c;
			}
		}
		C = maze[yg][xg];
		if(C == 0 || maze[ys][xs] == 0){
			cout << "NG" << endl;
			continue;
		}
		int ans = dfs(xs, ys);
		if(ans){
			cout << "OK" << endl;
		}
		else{
			cout << "NG" << endl;
		}
	}
}