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 <vector>

using namespace std;

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

bool fill(vector<vector<int> > &v, int x, int y, int gx, int gy) {
    if(x == gx && y == gy) return true;

    int W = v.size();
    int H = v[0].size();
    int clr = v[x][y];
    v[x][y] = 0;

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

        if(0 <= xx && xx < W && 0 <= yy && yy < H) {
            if(v[xx][yy] == clr) {
                if(fill(v, xx, yy, gx, gy)) return true;
            }
        }
    }
    return false;
}

int main() {
    while(true) {
        int W, H;
        int sx, sy, gx, gy;
        int N;

        cin >> W >> H;
        if(!W && !H) break;
        cin >> sx >> sy >> gx >> gy >> N;
        --sx; --sy; --gx; --gy;

        vector<vector<int> > v(W, vector<int>(H, 0));
        for(int i = 0; i < N; ++i) {
            int c, d, x, y;
            cin >> c >> d >> x >> y;
            --x; --y;
            int w = d ? 2 : 4;
            int h = d ? 4 : 2;
            for(int dx = 0; dx < w; ++dx) 
                for(int dy = 0; dy < h; ++dy)
                    v[x+dx][y+dy] = c;
        }

        if(v[sx][sy] != 0 && fill(v, sx, sy, gx, gy)) cout << "OK" << endl;
        else cout << "NG" << endl;
    }
    return 0;
}

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(W,vi(H));
		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]) 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>
#include <queue>
#include <cstring>
const int MAX_W = 100, MAX_H = 100;

typedef std::pair<int,int> P;
int map[MAX_H + 1][MAX_W + 1];

int bfs(P sp, P gp, int w, int h, int c){
	//½±Æª é©ði[
	int f[MAX_H + 1][MAX_W + 1] = {{0}};
	std::queue<P> q;
	q.push(P(sp.first, sp.second));
	
	while(q.size()){
		P p = q.front(); q.pop();
		//S[Å«½ç¬÷
		if(p.first == gp.first && p.second == gp.second && map[p.second][p.first] == c)return 1;
		
		int vy[4] = {-1,1,0,0}, vx[4] = {0,0,-1,1};
		for(int i=0;i<4;i++){
			int nx = p.first + vx[i],
				ny = p.second + vy[i];
			if(nx >= 0 && nx <= w && ny >= 0 && ny <= h && 
				 map[ny][nx] == c && !f[ny][nx]){
				q.push(P(nx,ny));
				f[ny][nx] = 1;
			}
		}
	}

	//ÅãÜÅS[Å«È¢Èç¸s
	return 0;
}

int main(){
	int w, h, xs, ys, xg, yg;
	while(std::cin >> w >> h, w&&h){
		memset(map,0,sizeof(map));
		std::cin >> xs >> ys;
		std::cin >> xg >> yg;

		int n;
		std::cin >> n;

		//ubNÌÝu
		while(n--){
			int c, d, x, y;
			std::cin >> c >> d >> x >> y;
			
			int mx, my;
			if(d)//w < h
				{my = 4, mx = 2;}
			else//w > h
				{mx = 4, my = 2;}
			for(int i=y;i<y+my;i++){
				for(int j=x;j<x+mx;j++){
					map[i][j] = c;
				}
			}
		}

		//bfsÅS[Å«é©mF
		P sp = P(xs,ys), gp = P(xg,yg);
		int c = map[ys][xs];
		if(bfs(sp, gp, w, h, c))std::cout << "OK" << std::endl;
		else{std::cout << "NG" << std::endl;}
	}
}