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 <queue>
using namespace std;
class node{
public:
	int x,y;
	void set(int xx,int yy){
		x = xx;
		y = yy;
	}
	bool check(int w,int h){
		if(x>=0&&y>=0){
			if(x<=w&&y<=h) return true;
		}
		return false;
	}
	
};

int main(){
	
	int w,h,xs,ys,xg,yg,n,c,dir,x,y;
	int i,j;
	int color[101][101];
	int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
	bool visited[101][101];
	queue<node> q;
	node a;

	scanf("%d%d",&w,&h);
	scanf("%d%d",&xs,&ys);
	scanf("%d%d",&xg,&yg);
	scanf("%d",&n);
	while(n--){
		scanf("%d%d",&c,&dir,&x,&y);
		if(dir==0){
			for(i=0;i<2;i++){
				for(j=0;j<4;j++) color[x+j][y+i] = c;
			}
		}
		else{
			for(i=0;i<2;i++){
				for(j=0;j<4;j++) color[x+i][y+j] = c;
			}
		}
	}
	a.set(xs,ys);
	
	q.push(a);
	visited[a.x][a.y]=true;
	while(!q.empty()){
		a = q.front();
		q.pop();
		for(int i=0;i<4;i++){
			a.x += dx[i];
			a.y += dy[i];
			if(!a.check(w,h)&&(color[xs][ys]==color[a.x][a.y])&&!visited[a.x][a.y]){
				q.push(a);
				if(a.x==xg&&a.y==yg) break;
				visited[a.x][a.y] = true;
			}
		}
	}
	if(visited[xg][yg]) printf("OK\n");
	else printf("NG\n");
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ANSWER(x) cout<<x<<endl
#define debug(x) cout<<#x<<": "<<x
const int BA = 1000;
const int INFTY = (1<<21);
int xg,yg,w,h,gc;
int maps[101][101];
int solve(int ys,int xs);
int main(){
  int tate[2] = {2,4};
  int yoko[2] = {4,2};
  int xs,ys,n;
  int c[101],d[101],x[101],y[101];
  while(1){
    fill(maps[0],maps[101],0);
    cin >> w >> h;
    if(w==0 && h==0)break;
    cin >> xs >> ys >> xg >> yg >> n;
    for(int i=0;i<n;i++){
      cin >> c[i] >> d[i] >> x[i] >> y[i];
    }
    for(int i=0;i<n;i++){
      for(int j=y[i];j<y[i]+tate[d[i]];j++){
	for(int k=x[i];k<x[i]+yoko[d[i]];k++){
	  if(j == ys && k == xs)gc = c[i];
	  maps[j-1][k-1] = c[i];
	}
      }
    }
    /* for(int i=0;i<h;i++){
       for(int j=0;j<w;j++){
       cout << maps[i][j];
       }
       cout << endl;
       }*///マップ確認用
    if(solve(ys-1,xs-1))cout << "OK\n";
    else cout << "NG\n";
  }
}
int solve(int ys,int xs){
  if(xs < 0 || ys < 0 || xs > w-1 || ys > h-1 || maps[ys][xs] == 0){
    return 0;
  }
  else if(maps[ys][xs] == maps[yg-1][xg-1]){
    return 1;
  }
  maps[ys][xs] = 0;
  return solve(ys+1,xs) + solve(ys,xs+1)+ solve(ys-1,xs) + solve(ys,xs-1);
}

C++: #include <stdio.h>
#include <math.h>
#include <algorithm>
#include <queue>

using namespace std;

int width,height,table[100][100];

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= height-1 && col >= 0 && col <= width-1)return true;
	else{
		return false;
	}
}

void recursive(int row,int col){
	int tmp = table[row][col];
	table[row][col] = 7;

	if(rangeCheck(row-1,col) == true && table[row-1][col] == tmp)recursive(row-1,col);
	if(rangeCheck(row+1,col) == true && table[row+1][col] == tmp)recursive(row+1,col);
	if(rangeCheck(row,col-1) == true && table[row][col-1] == tmp)recursive(row,col-1);
	if(rangeCheck(row,col+1) == true && table[row][col+1] == tmp)recursive(row,col+1);
}

int main(){

	int x_s,y_s,x_g,y_g,n,color,dist,x,y;

	while(true){
		scanf("%d %d",&width,&height);
		if(width == 0 && height == 0)break;

		for(int i = 0; i < height; i++)
		{
			for(int k = 0; k < width; k++)table[i][k] = 0;
		}

		scanf("%d %d",&x_s,&y_s);
		scanf("%d %d",&x_g,&y_g);

		scanf("%d",&n);

		for(int i = 0; i < n; i++){
			scanf("%d %d %d %d",&color,&dist,&x,&y);

			if(dist == 0){
				for(int row = 0; row < 2; row++){
					for(int col = 0; col < 4; col++){
						table[y-1+row][x-1+col] = color;
					}
				}
			}else{
				for(int row = 0; row < 4; row++){
					for(int col = 0; col < 2; col++){
						table[y-1+row][x-1+col] = color;
					}
				}
			}
		}
		recursive(y_s-1,x_s-1);

		if(table[y_g-1][x_g-1] == 7)printf("OK\n");
		else{
			printf("NG\n");
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///


int main()
{
	int w, h;
	while (cin >> w >> h, w)
	{
		vector<vi> v(h, vi(w));
		int sx, sy, gx, gy;
		cin >> sx >> sy >> gx >> gy;
		sx--; sy--; gx--; gy--;
		int n; cin >> n;
		while (n--)
		{
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			x--; y--;
			if (d==0)
			{
				REP(i, 4)REP(j, 2)
				{
					v[y+j][x+i] = c;
				}
			}
			else
			{
				REP(i, 2)REP(j, 4)
				{
					v[y+j][x+i] = c;
				}
			}
		}
		int co = v[sx][sy];
		if (co == 0)
		{
			puts("NG");
			continue;
		}

		queue<P> que;
		que.push(P(sx, sy));
		bool f = false;
		while (que.size())
		{
			P tmp = que.front();
			que.pop();
			int tx = tmp.first, ty = tmp.second;
			if (tx == gx&&ty == gy)
			{
				f = true;
				break;
			}
			v[tx][ty] = 0;
			REP(i, 4)
			{
				int nx = tx + dx[i], ny = ty + dy[i];
				if (valid(nx, ny, w, h) && v[nx][ny] == co) que.push(P(nx, ny));
			}
		}
		puts(f ? "OK" : "NG");
	}
}