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>

using namespace std;

int table[100][100];
int memo[100][100];

int w, h;
int xs, ys, xg, yg;
int n;

void solve(int xs, int ys, int c)
{
	if( memo[xs][ys] ) {
		return;
	}

	if( !( -1 < xs && xs < w && -1 < ys && ys < h && table[xs][ys] == c ) ) {
		memo[xs][ys] = 1;
		return;
	}
	
	if( xs == xg && ys == yg ) {
		memo[xs][ys] = 1;
		cout << "OK" << endl;
		throw "OK";
	}

	memo[xs][ys] = 1;	

	solve(xs+1, ys, c);
	solve(xs, ys+1, c);	
	solve(xs-1, ys, c);
	solve(xs, ys-1, c);

	return;
}

int main(int argc, char* argv[])
{
	while(1) {

		cin >> w >> h;

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

		for( int i = 0; i < n; i++ ) {

			int c; cin >> c;
			int d; cin >> d;
			int x, y; cin >> x >> y;

			if( d == 0 ) {

				table[x][y] = c;
				table[x][y+1] = c;

				table[x+1][y] = c;
				table[x+1][y+1] = c;

				table[x+2][y] = c;
				table[x+2][y+1] = c;

				table[x+3][y] = c;
				table[x+3][y+1] = c;																
				

			} else {

				table[x][y] = c;
				table[x+1][y] = c;

				table[x][y+1] = c;
				table[x+1][y+1] = c;

				table[x][y+2] = c;
				table[x+1][y+2] = c;
				
				table[x][y+3] = c;
				table[x+1][y+3] = c;								
			}
		}

		for( int i = 0; i < 100; i++ ) {
			for( int j = 0; j < 100; j++ ) {
				memo[i][j] = 0;
			}
		}		

		try {
			solve(xs, ys, table[xs][ys]);
			cout << "NG" << endl;				  
		} catch(...) {
		}
	}

    return 0;
}

C++: #include <queue>
#include <cstring>
#include <iostream>
using namespace std;
int W, H, sx, sy, gx, gy, N, c, d, x, y, r[111][111], vis[111][111];
int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
int main() {
	while(cin >> W >> H, W | H) {
		cin >> sx >> sy >> gx >> gy; sx--, sy--, gx--, gy--;
		memset(r, 0, sizeof(r));
		cin >> N;
		for(int i = 0; i < N; i++) {
			cin >> c >> d >> x >> y; x--, y--;
			int zx = (d ? 2 : 4), zy = (d ? 4 : 2);
			for(int j = 0; j < zy; j++) {
				for(int k = 0; k < zx; k++) {
					r[y + j][x + k] = c;
				}
			}
		}
		memset(vis, 0, sizeof(vis)); vis[sy][sx] = 1;
		queue<pair<int, int> > que; que.push(make_pair(sx, sy));
		while(!que.empty()) {
			pair<int, int> v = que.front(); que.pop();
			for(int i = 0; i < 4; i++) {
				int tx = v.first + dx[i], ty = v.second + dy[i];
				if(0 <= tx && tx < W && 0 <= ty && ty < H && !vis[ty][tx] && r[ty][tx] == r[v.second][v.first]) {
					vis[ty][tx] = 1;
					que.push(make_pair(tx, ty));
				}
			}
		}
		cout << (vis[gy][gx] ? "OK" : "NG") << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

bool isMaze;

void dfs(int blocks[105][105], int target, int y, int x, int gx, int gy){
    if(x == gx && y == gy){
        isMaze = true;
        return;
    }
    if(blocks[y][x] == target){
        blocks[y][x] = 0;
    }else{
        return;
    }
    dfs(blocks, target, y + 1, x, gx, gy);
    dfs(blocks, target, y - 1, x, gx, gy);
    dfs(blocks, target, y, x + 1, gx, gy);
    dfs(blocks, target, y, x - 1, gx, gy);
}

int main(){
    int w, h, xs, ys, xg, yg, n;
    while(cin >> w >> h, w||h){
        int blocks[105][105] = {0};
        cin >> xs >> ys >> xg >> yg >> n;
        rep(i,n){
            int c, d, x, y;
            cin >> c >> d >> x >> y;
            if(d == 0){
                rep(j,2) rep(k,4) blocks[y + j][x + k] = c;
            }else{
                rep(j,4) rep(k,2) blocks[y + j][x + k] = c;
            }
        }
        isMaze = false;
        if(blocks[ys][xs] != 0) dfs(blocks, blocks[ys][xs], xs, ys, xg, yg);

        if(isMaze) cout << "OK" << endl;
        else cout << "NG" << endl;
    }
}

C++: #include <stdio.h>
int xg,yg,c,sc,ans,block[101][101],w,h;
int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
int idou(int x,int y)
{
    if(x == xg&&y == yg)
    {
        ans = 1;
        return 0;
    }
    if(sc == 0)return 0;
    block[x][y] = 0;
    for(int i = 0; i < 4; i++)
    {
        if(block[x + dx[i]][y + dy[i]] == sc)idou(x + dx[i],y + dy[i]);
    }
    return 0;
}
int main()
{
    while(1)
    {
        int xs,ys,n,d,x,y;
        ans = 0;
        for(int i = 0; i < w; i++)
        {
            for(int j = 0; j < h; j++)
            {
                block[i][j] = 0;
            }
        }
        scanf("%d%d",&w,&h);
        if(w == 0&&h == 0)break;
        scanf("%d%d",&xs,&ys);
        scanf("%d%d",&xg,&yg);
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d%d%d",&c,&d,&y,&x);
            if(d == 1)
            {
                for(int i = 0; i < 2; i++)
                {
                    for(int j = 0; j < 4; j++)
                    {
                        block[x + j][y + i] = c;
                    }
                }
            }
            if(d == 0)
            {
                for(int i = 0; i < 2; i++)
                {
                    for(int j = 0; j < 4; j++)
                    {
                        block[x + i][y + j] = c;
                    }
                }
            }
        }
        sc = block[xs][ys];
        idou(xs,ys);
        if(ans == 1)printf("OK\n");
        else printf("NG\n");
    }
    return 0;
}