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<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<utility>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<set>

using namespace std;
typedef pair<int,int> PII;
typedef long long ll;

int xs,ys,xg,yg;
int board[102][102];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

bool dfs(int x,int y,int c){
	if(board[y][x] != c) return false;
	if(x == xg && y == yg) return true;
	board[y][x] = 0;
	bool flag = false;
	for(int dir = 0;dir<4;dir++){
		if(dfs(x+dx[dir],y+dy[dir],c)){
			flag = true;
			break;
		}
	}
	return flag;
}

int main(){
	int w,h;
	int n,col;
	int c,d,x,y;
	int pos0_x[8] = {0,0,1,1,2,2,3,3};
	int pos0_y[8] = {0,1,0,1,0,1,0,1};
	int pos1_x[8] = {0,0,0,0,1,1,1,1};
	int pos1_y[8] = {0,1,2,3,0,1,2,3};

	while(true){
		cin>>w>>h;
		if(!w&&!h)break;
		cin>>xs>>ys>>xg>>yg>>n;
		for(y = 0;y<102;y++)
			for(x = 0;x<102;x++)
				board[y][x] = 0;
		for(int i = 0;i < n;i++){
			cin>>c>>d>>x>>y;
			if(d==0){
				for(int j = 0;j<8;j++)
					board[y+pos0_y[j]][x+pos0_x[j]] = c;
			}
			else{
				for(int j = 0;j<8;j++)
					board[y+pos1_y[j]][x+pos1_x[j]] = c;
			}
		}
		col = board[ys][xs];
		if(col==0)col=6;
		if(dfs(xs,ys,col))
			cout<<"OK"<<endl;
		else
			cout<<"NG"<<endl;
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

int w, h;

int field[100][100];
int pathToGoal[100][100];

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

int dfs(int x, int y, int goalX, int goalY, int color) {
    pathToGoal[y][x] = 1;
    if (x == goalX && y == goalY) {
        return 1;
    }
    int res = 0;
    for (int i = 0; i < 4; ++i) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (0 <= nx && nx < w && 0 <= ny && ny < h && pathToGoal[ny][nx] == -1 && field[ny][nx] == color) {
            res += dfs(nx, ny, goalX, goalY, color);
        }
    }
    return res;
}

int main() {
    while (true) {
        cin >> w >> h;
        if (w == 0 && h == 0) {
            return 0;
        }

        for (int i = 0; i < 100; ++i) {
            for (int j = 0; j < 100; ++j) {
                field[i][j] = -1;
                pathToGoal[i][j] = -1;
            }
        }

        int xs, ys;
        int xg, yg;
        int n;
        cin >> xs >> ys;
        cin >> xg >> yg;
        cin >> n;
        xs--;
        ys--;
        xg--;
        yg--;
        for (int i = 0; i < n; ++i) {
            int c, d, x, y;
            cin >> c >> d >> x >> y;
            x--;
            y--;
            if (d == 0) {
                for (int p = 0; p < 2; ++p) {
                    for (int q = 0; q < 4; ++q) {
                        field[y + p][x + q] = c;
                    }
                }
            }
            else {
                for (int p = 0; p < 4; ++p) {
                    for (int q = 0; q < 2; ++q) {
                        field[y + p][x + q] = c;
                    }
                }
            }
        }

        if (dfs(xs, ys, xg, yg, field[xs][ys]) > 0) {
            cout << "OK" << endl;
        }
        else {
            cout << "NG" << endl;
        }
    }
}

C++: #include <cstdio>
#include <string.h>
using namespace std;

int map[200][200];
bool reach[200][200];
int w, h;

typedef struct {
	int x;
	int y;
} SIZE;

SIZE size[2] = {
	{4, 2}, {2, 4}
};

int angle[][2] = {
	{-1, 0}, {0, -1}, {1, 0}, {0, 1}
};

void search(int x, int y)
{
	if (map[x][y] == 0 || x <= 0 || x > w || y <= 0 || y > h){
		return;
	}
	
	reach[x][y] = true;
	
	for (int i = 0; i < 4; i++){
		if (
		reach[x + angle[i][0]][y + angle[i][1]] == false &&
		map[x + angle[i][0]][y + angle[i][1]] == map[x][y]
		){
//			reach[x + angle[i][0]][y + angle[i][1]] = true;
			search(x + angle[i][0], y + angle[i][1]);
		}
	}
}
		

int main()
{
	while (scanf("%d %d", &w, &h), w + h){
		memset(map, 0, sizeof(map));
		memset(map, 0, sizeof(reach));
		
		int xs, ys;
		scanf("%d %d", &xs, &ys);
		
		int xg, yg;
		scanf("%d %d", &xg, &yg);
		
		int n;
		scanf("%d", &n);
		for (int i = 0; i < n; i++){
			int c, d, x, y;
			scanf("%d %d %d %d", &c, &d, &x, &y);
			
			for (int j = 0; j < size[d].y; j++){
				for (int k = 0; k < size[d].x; k++){
					map[x + k][y + j] = c;
				}
			}
		}
/*		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				printf("%d ", map[j][i]);
			}
			puts("");
		}
*/		
		search(xs, ys);
/*		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				printf("%d ", reach[j][i]);
			}
			puts("");
		}
*/		
		if (reach[xg][yg]){
			printf("OK\n");
		}
		else {
			printf("NG\n");
		}
	}
	
	return 0;
}

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

int n, w, h, xs, ys, xg, yg, f[105][105] = { 0 }, dx[] = { 0,1,0,-1 }, dy[] = { 1,0,-1,0 }, used[105][105] = { 0 }, color = 0;
bool flag;
void dfs(int y, int x) {
	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (xg == nx&&yg == ny) {
			flag = true;
			return;
		}
		if (used[ny][nx]) continue;
		if (1 <= nx&&nx <= w && 1 <= ny&&ny <= h&&f[ny][nx] == color) {
			used[ny][nx] = 1;
			dfs(ny, nx);
		}
	}
	return;
}

int main() {
	while (1) {
		color = -1;
		flag = false;
		cin >> w >> h;
		if (w == 0 && h == 0) return 0;
		cin >> xs >> ys >> xg >> yg >> n;
		for (int i = 0; i < n; i++) {
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			if (d == 0) {
				for (int j = 0; j < 2; j++) {
					for (int k = 0; k < 4; k++) {
						f[y + j][x + k] = c;
					}
				}
			}
			else {
				for (int j = 0; j < 4; j++) {
					for (int k = 0; k < 2; k++) {
						f[y + j][x + k] = c;
					}
				}
			}
		}
		used[ys][xs] = 1;
		color = f[ys][xs];
		dfs(ys, xs);
		if (color == -1) {//?????????????????????????????????
			cout << "NG" << endl; continue;
		}
		if (flag) cout << "OK" << endl;
		else cout << "NG" << endl;
	}
}