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 <fstream>
#include <typeinfo>
#include <vector>
#include <cmath>
#include <set>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<25;
const double pi=acos(-1);
const double eps=1e-8;
const vi emp;

int W,H,sx,sy,gx,gy,color;
int dx[8]={0,0,0,0,1,1,1,1},dy[8]={0,1,2,3,0,1,2,3},cx[4]={-1,0,0,1},cy[4]={0,-1,1,0};
vvi field;
vvb used;

bool solve(int x,int y,int color,vvb& used){
	used[x][y]=1;
	if(x==gx&&y==gy) return 1;
	for(int i=0;i<4;i++){
		int tmpx=x+cx[i],tmpy=y+cy[i];
		if(tmpx>=0&&tmpx<W&&tmpy>=0&&tmpy<H&&field[tmpx][tmpy]==color&&!used[tmpx][tmpy]){
			if(solve(tmpx,tmpy,color,used)) return 1;
		}
	}
	return 0;
}

int main(){
	while(1){
		cin>>W>>H;
		if(!W) break;
		field=vvi(H,vi(W,-1));
		cin>>sx>>sy>>gx>>gy;
		sx--;
		sy--;
		gx--;
		gy--;
		int n;
		cin>>n;
		int c,d,x,y;
		for(int i=0;i<n;i++){
			cin>>c>>d>>x>>y;
			x--;
			y--;
			if(d){
				for(int i=0;i<8;i++) field[x+dx[i]][y+dy[i]]=c;
			}
			else{
				for(int i=0;i<8;i++) field[x+dy[i]][y+dx[i]]=c;
			}
		}
		if(field[sx][sy]==-1||field[gx][gy]==-1) cout<<"NG"<<endl;
		else {
			int color=field[sx][sy];
			used=vvb(H,vb(W));
			if(solve(sx,sy,color,used)) cout<<"OK"<<endl;
			else cout<<"NG"<<endl;
		}
	}
}

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

int map[102][102];

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


int main() {
	int h, w, xs, ys, xg, yg, n;

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

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

	return 0;
}

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

int w, h, xs, ys, xg, yg, n;
int board[110][110];
bool used[110][110];

int dx1[8] = {0,1,2,3,0,1,2,3};
int dy1[8] = {0,0,0,0,1,1,1,1};

int dx2[8] = {0,0,0,0,1,1,1,1};
int dy2[8] = {0,1,2,3,0,1,2,3};

bool dfs(int x, int y, int xg, int yg, int color) {
    if (board[y][x] != color || used[y][x] || x <= 0 || y <= 0 || w < x || h < y) {
        return false;
    }
    if (x == xg && y == yg) {
        return true;
    }
    used[y][x] = true;
    bool ret = false;
    ret |= dfs(x+1, y, xg, yg, color);
    ret |= dfs(x-1, y, xg, yg, color);
    ret |= dfs(x, y+1, xg, yg, color);
    ret |= dfs(x, y-1, xg, yg, color);
    return ret;
}

int main() {
    while (cin >> w >> h, (w || h)) {
        memset(board, 0, sizeof board);
        cin >> xs >> ys >> xg >> yg >> n;
        int c, d, x, y;
        for (int i=0; i<n; ++i) {
            cin >> c >> d >> x >> y;
            if (d == 0) {
                for (int i=0; i<8; ++i) {
                    board[y + dy1[i]][x + dx1[i]] = c;
                }
            } else {
                for (int i=0; i<8; ++i) {
                    board[y + dy2[i]][x + dx2[i]] = c;
                }
            }
        }
        memset(used, false, sizeof used);
        if (board[ys][xs] && dfs(xs, ys, xg, yg, c)) {
            cout << "OK" << endl;
        } else {
            cout << "NG" << endl;
        }
    }
    return 0;
}