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<cstdio>
#include<cstring>
#include<queue>
using namespace std;

#define size_max 100+10

#define Y first
#define X second

int mx[4] = { 1,-1,0,0 };
int my[4] = { 0,0,1,-1 };

int map[size_max][size_max];


void paint(int c, int y, int x, int H, int W)
{
	int i, j;
	for (i = y; i < y + H; i++)
	{
		for (j = x; j < x + W; j++)
		{
			map[i][j] = c;
		}
	}
}


void chek(int w, int h)
{
	int i, j;
	for (i = 0; i <= h + 1; i++)
	{
		for (j = 0; j <= w + 1; j++)
		{
			cout << map[i][j];
		}
		cout << endl;
	}
}


typedef pair<int, int> PR;

int main(void)
{
	PR now, tmp;
	queue<PR> QU;

	int w, h;
	int xs, ys, xg, yg, n;
	int c, d, set_x, set_y;

	int i, j;

	while ((cin >> w >> h), w != 0 && h != 0)
	{
		memset(map, 0, sizeof map);

		cin >> xs >> ys >> xg >> yg >> n;

		for (i = 0; i < n; i++)
		{
			cin >> c >> d >> set_x >> set_y;

			if (d == 0)
			{
				paint(c, set_y, set_x, 2, 4);
			}
			else
			{
				paint(c, set_y, set_x, 4, 2);
			}
		}

		//chek(w, h);

		if (map[ys][xs] == 0)
		{
			cout << "NG\n";
			continue;
		}

		now = make_pair(ys, xs);
		QU.push(now);

		bool goal = false;
		while (!QU.empty())
		{
			now = QU.front();
			QU.pop();

			if (map[now.Y][now.X] != -1)
			{
				map[now.Y][now.X] = -1;

				if (now.Y == yg && now.X == xg)
				{
					goal = true;
					while (!QU.empty())
					{
						QU.pop();
					}
					break;
				}

				for (i = 0; i < 4; i++)
				{
					if (map[now.Y + my[i]][now.X + mx[i]] == c)
					{
						tmp = make_pair(now.Y + my[i], now.X + mx[i]);
						QU.push(tmp);
					}
				}
			}
		}

		if (goal)
		{
			cout << "OK\n";
		}
		else
		{
			cout << "NG\n";
		}

	}

	return 0;
}

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

int fld[100][100];//[横][縦]
int Dx[4]={0,-1,0,1};
int Dy[4]={1,0,-1,0};

bool ans[1000];
int counter=0;

bool flag=true;

void dfs(int x,int y,int w,int h,int c){//スタート地点と色を与える
	fld[x][y]=-1;
	for(int i=0;i<4;i++){
		int nx=x+Dx[i],ny=y+Dy[i];//範囲内にある必要あり
		if(nx>=0 && nx<w && ny>=0 && ny<h){
		if(fld[nx][ny]==c)//同色についてのみ継続
			dfs(nx,ny,w,h,c);
		}
	}
}//色の塗り替え
	

void solve(){
	int w,h,xs,ys,xg,yg,n;
	cin >> w>>h;
	
	if(w==0) {flag=false;counter--; return;}
	
	cin >>xs>>ys>>xg>>yg>>n;//start,goal座標は-1する
	xs--;ys--;xg--;yg--;
	
	for(int i=0;i<w;i++){//fieldの初期化0~w-1
		for(int j=0;j<h;j++){//0~h-1
			fld[i][j]=0;//0は何も無い
		}
	}
	
	for(int i=0;i<n;i++){
		int color,drc,x,y;
		cin >>color>>drc>>x>>y;
		x--;y--;
		if(drc==0){
			for(int dx=0;dx<4;dx++){
				for(int dy=0;dy<2;dy++){
					fld[x+dx][y+dy]=color;
				}
			}
		}
		if(drc==1){
			for(int dy=0;dy<4;dy++){
				for(int dx=0;dx<2;dx++){
					fld[x+dx][y+dy]=color;
				}
			}
		}
	}//fieldの色塗り
	
	if(fld[xs][ys]!=fld[xg][yg]) {
		ans[counter]=false;
		return;
	}
	if(fld[xs][ys]==0){
		ans[counter]=false;
		return;
	}
	
	dfs(xs,ys,w,h,fld[xs][ys]);//塗り替え
	
	if(fld[xg][yg]==-1) ans[counter]=true;
	else ans[counter]=false;
	
	return;
}

int main(){
	while(flag){
		solve();
		counter++;
	}
	for(int i=0;i<counter;i++){
		if(ans[i]) cout << "OK"<<endl;
		else cout << "NG" << endl;
	}
	
}

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

int C[102][102];

void meiro(int W,int H,int c){
 return;
 if(C[W][H]!=c){
   return;
 }

 C[W][H] = -1;

 meiro(W-1,H  ,c);
 meiro(W  ,H+1,c);
 meiro(W+1,H  ,c);
 meiro(W  ,H-1,c);
}

int main(){
 while(1){
   int w,h;
   cin >> w >> h;
   if(w==0 && h==0){
     break;
   }

   for(int i=0;i<20;i++){
     for(int j=0;j<20;j++){
       C[i][j] = 0;
     }
   }

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

   int n;
   cin >> n;

   int c,d,x,y;
   for(int i=0;i<n;i++){
     cin >> c >> d >> x >> y;

     if(d==0){
       for(int j=x;j<x+4;j++){
         for(int k=y;k<y+2;k++){
           C[j][k]=c;
         }
       }
     }else{
       for(int j=x;j<x+2;j++){
         for(int k=y;k<y+4;k++){
           C[j][k]=c;
         }
       }
     }
   }

   meiro(xs,ys,C[xs][ys]);
   //print(w,h);

   if(C[xg][yg] == -1){
     cout << "OK" << endl;
   }else{
     cout << "NG" << endl;
   }
 }

 return 0;
}