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 <bits/stdc++.h>
using namespace std;

int main() {
    int H, W;
    while (cin >> W >> H, H || W) {
        int sx, sy, gx, gy;
        cin >> sx >> sy >> gx >> gy;
        sx--, sy--, gx--, gy--;
        int M;
        cin >> M;
        vector<vector<int> > d(H, vector<int>(W));
        vector<vector<int> > used(H, vector<int>(W));

        for (int i = 0; i < M; i++) {
            int c, dir, x, y;
            cin >> c >> dir >> x >> y;
            x--, y--;
            if (dir) {
                for (int j = 0; j < 2; j++) {
                    for (int k = 0; k < 4; k++) {
                        d[x + j][y + k] = c;
                    }
                }
            } else {
                for (int j = 0; j < 4; j++) {
                    for (int k = 0; k < 2; k++) {
                        d[x + j][y + k] = c;
                    }
                }
            }
        }

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

        queue<int> qx, qy;
        qx.push(sx);
        qy.push(sy);

        while (qx.size()) {
            int x = qx.front(); qx.pop();
            int y = qy.front(); qy.pop();

            if (used[x][y]) continue;
            used[x][y] = 1;

            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];

                if (0 <= nx && nx < H && 0 <= ny && ny < W && !used[nx][ny] && d[x][y] == d[nx][ny]) {
                    qx.push(nx);
                    qy.push(ny);
                }
            }
        }

        if (used[gx][gy]) cout << "OK" << endl;
        else cout << "NG" << endl;
    }

    return 0;
}

C++: #include<iostream>
using namespace std;
int w, h, xs, ys, xg, yg, stage[102][102] = {};
int ny[4] = { 0, 1, 0, -1 }, nx[4] = { 1, 0, -1, 0 };
int bc;
bool ans = false;
void func(int x, int y){
	if (x == xg && y == yg){
		ans = true;
	}
	for (int i = 0; i < 4; i++){
		if (stage[y + ny[i]][x + nx[i]] != bc) return;
		func(x + nx[i], y + ny[i]);
	}
}
int main(){
	int n, c, d, x, y, bw, bh;
	while (1){
		cin >> w >> h;
		if (w == 0 && h == 0) return 0;
		cin >> xs >> ys >> xg >> yg >> n;
		for (int i = 0; i < n; i++){
			cin >> c >> d >> x >> y;
			if (d == 0){
				bw = 4;
				bh = 2;
			}
			else{
				bw = 2;
				bh = 4;
			}
			for (int j = 0; j < bh; j++){
				for (int k = 0; k < bw; k++){
					stage[y + j][x + k] = c;
				}
			}
		}
		bc = stage[1][1];
		if (bc == 0) cout << "NG\n";
		func(1, 1);
		if (ans == true) cout << "OK\n";
		else cout << "NG\n";
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main(){
	int w, h, xs, ys, xg, yg, n, c, d, x, y, color;
	vector< vector< int > > field;
	queue< complex< int > > que;
	bool exist;
	
	while(cin >> w >> h, w){
		cin >> xs >> ys >> xg >> yg >> n;
		
		field.resize(w);
		for(int i = 0; i < w; ++i) field[i].resize(h);
		
		for(int i = 0; i < n; ++i){
			cin >> c >> d >> x >> y;
			
			if(d == 0){
				for(int j = 0; j < 4; ++j){
					for(int k = 0; k < 2; ++k){
						field[x + j - 1][y + k - 1] = c;
					}
				}
			}else{
				for(int j = 0; j < 2; ++j){
					for(int k = 0; k < 4; ++k){
						field[x + j - 1][y + k - 1] = c;
					}
				}
			}
		}
		
		exist = false;
		if(field[xs - 1][ys - 1] != 0){
			que.push(complex< int >(xs - 1, ys - 1));
			color = field[xs - 1][ys - 1];
			
			while(!que.empty()){
				complex< int > pos = que.front();
				que.pop();
				
				if(pos.real() == xg - 1 && pos.imag() == yg - 1){
					exist = true;
					break;
				}
				
				if(pos.real() > 0 && field[pos.real() - 1][pos.imag()] == color) que.push(pos + complex< int >(-1, 0));
				if(pos.real() < w - 1 && field[pos.real() + 1][pos.imag()] == color) que.push(pos + complex< int >(1, 0));
				if(pos.imag() > 0 && field[pos.real()][pos.imag() - 1] == color) que.push(pos + complex< int >(0, -1));
				if(pos.imag() < h - 1 && field[pos.real()][pos.imag() + 1] == color) que.push(pos + complex< int >(0, 1));
				
				field[pos.real()][pos.imag()] = 0;
			}
		}
		
		cout << (exist ? "OK" : "NG") << endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)

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

const int ax[2][8] = {
	{0, 1, 2, 3, 0, 1, 2, 3},
	{0, 0, 0, 0, 1, 1, 1, 1}
};

const int ay[2][8] = {
	{0, 0, 0, 0, 1, 1, 1, 1},
	{0, 1, 2, 3, 0, 1, 2, 3}
};

int W, H, sx, sy, gx, gy, n, board[110][110];

bool dfs(int x, int y) {
	if (x == gx && y == gy) return true;
	int c = board[y][x];
	board[y][x] = -1;
	
	for_(d,0,4) {
		int nx = x + dx[d], ny = y + dy[d];
		if (nx < 1 || W < nx || ny < 1 || H < ny) continue;
		if (board[ny][nx] != c) continue;
		if (dfs(nx, ny)) return true;
	}
	
	return false;
}

int main() {
	while (cin >> W >> H, W) {
		memset(board, -1, sizeof(board));
		
		cin >> sx >> sy;
		cin >> gx >> gy;
		cin >> n;
		
		for_(i,0,n) {
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			for_(a,0,8) board[y + ay[d][a]][x + ax[d][a]] = c;
		}
		
		if (board[sy][sx] != -1 && dfs(sx, sy)) puts("OK");
		else puts("NG");
	}
}