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 w,h;
int field[103][103];
int gx,gy;
int sc;

void init()
{
	for(int i=0;i<103;i++){
		for(int j=0;j<103;j++){
			field[i][j] = 0;
		}
	}
}
bool search(int x,int y)
{
	cout << x << " " << y << endl;
	bool flag = false;
	if(x == gx && y == gy)
		flag = true;
	else{
		int vectorx[4] = {-1,0,1,0},vectory[4] = {0,-1,0,1};
		for(int i=0;i<4;i++){
			if(x+vectorx[i] >= 0 && y+vectory[i] >= 0 && x+vectorx[i] < w && y+vectory[i] < h){
				if(field[x+vectorx[i]][y+vectory[i]] == sc){
					field[x][y] = -1;
					flag |= search(x+vectorx[i],y+vectory[i]);
				}
			}
		}
	}
	return flag;
}
int main()
{
	while(cin >> w >> h){
		if(w == 0 && h == 0)
			break;
		init();
		int sx,sy;
		cin >> sx >> sy;
		cin >> gx >> gy;
		int n,c,x,y;
		bool d;
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> c >> d >> x >> y;
			int vectorx,vectory;
			if(d == 0){
				vectorx = 4;vectory = 2;
			}else{
				vectorx = 2;vectory = 4;
			}
			for(int j=0;j<vectorx;j++){
				for(int k=0;k<vectory;k++){
					field[x+j][y+k] = c;
				}
			}
		}
		for(int i=0;i<w;i++){
			for(int j=0;j<h;j++){
				cout << field[i][j];
			}
			cout << endl;
		}
		cout << endl;
		sc = field[sx][sy];
		if(field[sx][sy] == 0 || field[gx][gy] == 0)
			cout << "NG" << endl;
		else{
			if(search(sx,sy))
				cout << "OK" << endl;
			else
				cout << "NG" << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int minDist(const vector<vector<int> >& plane, char road, int y1, int x1, int y2, int x2)
{
    int dy[] = {0, 0, -1, 1};
    int dx[] = {-1, 1, 0, 0};

    int h = plane.size();
    int w = plane[0].size();

    queue<pair<int, int> > q;
    vector<vector<bool> > check(h, vector<bool>(w, false));
    q.push(make_pair(y1, x1));
    check[y1][x1] = true;
    int ret = 0;
    while(!q.empty()){
        queue<pair<int, int> > q1;
        while(!q.empty()){
            int y = q.front().first;
            int x = q.front().second;
            q.pop();
            if(y == y2 && x == x2)
                return ret;
            for(int i=0; i<4; ++i){
                int y0 = y + dy[i];
                int x0 = x + dx[i];
                if(0 <= y0 && y0 < h && 0 <= x0 && x0 < w && plane[y0][x0] == road && !check[y0][x0]){
                    q1.push(make_pair(y0, x0));
                    check[y0][x0] = true;
                }
            }
        }
        ++ ret;
        q = q1;
    }
    return -1;
}

int main()
{
    for(;;){
        int w, h;
        cin >> w >> h;
        if(w == 0)
            return 0;

        int xs, ys, xg, yg;
        cin >> xs >> ys >> xg >> yg;

        int n;
        cin >> n;
        vector<vector<int> > grid(h+2, vector<int>(w+2, 0));
        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){
                        grid[y+j][x+k] = c;
                    }
                }
            }else{
                for(int j=0; j<4; ++j){
                    for(int k=0; k<2; ++k){
                        grid[y+j][x+k] = c;
                    }
                }
            }
        }

        if(grid[ys][xs] != grid[yg][xg] || grid[ys][xs] == 0)
            cout << "NG" << endl;
        else if(minDist(grid, grid[ys][xs], ys, xs, yg, xg) == -1)
            cout << "NG" << endl;
        else
            cout << "OK" << endl;
    }
}