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 <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int w, h;
int sx, sy;
int gx, gy;
int n;
int field[120][120];

void PrintField() {
  REP(y, h + 2) {
    REP(x, w + 2) {
      printf("%d", field[y][x]);
    }
    puts("");
  }
}

int main() {
  while (scanf("%d %d", &w, &h), w|h) {
    MEMSET(field, 0);
    scanf("%d %d", &sx, &sy);
    scanf("%d %d", &gx, &gy);
    scanf("%d", &n);
    REP(i, n) {
      int c, d, sx, sy;
      scanf("%d %d %d %d", &c, &d, &sx, &sy);
      int ex = sx + 2;
      int ey = sy + 2;
      if (d == 0) { ex += 2; }
      if (d == 1) { ey += 2; }
      FOR(y, sy, ey) {
        FOR(x, sx, ex) {
          field[y][x] = c;
        }
      }
    }
    int sc = field[sy][sx];
    queue<int> que;
    que.push(sy * w + sx);
    while (!que.empty()) {
      int x = que.front() % w;
      int y = que.front() / w;
      que.pop();
      if (x == gx && y == gy) {
        puts("OK");
        goto next;
      }
      if (sc == 0) { break; }
      REP(dir, 4) {
        const int dx[4] = { 1, 0, -1, 0 };
        const int dy[4] = { 0, 1, 0, -1 };
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        if (field[ny][nx] != sc) { continue; }
        field[ny][nx] = 9;
        que.push(ny * w + nx);
      }
    }
    puts("NG");
next:;
  }
}

C++: #include <iostream>
using namespace std;
int F[102][102];

void DFScolor(int X,int Y,int color,int Xg,int Yg){

	if(F[X][Y]!=color)
		return;
	
	F[X][Y]=0;
	DFScolor(X-1,Y,color,Xg,Yg);
	DFScolor(X,Y+1,color,Xg,Yg);
	DFScolor(X+1,Y,color,Xg,Yg);
	DFScolor(X,Y-1,color,Xg,Yg);
	
}


int main(){
while(1){
	int H,W,xg,yg,xs,ys,n;
	cin>>H>>W;
	if(W==0&&H==0)
		return 0;
	cin>>xs>>ys;
	cin>>xg>>yg;	
	cin>>n;
	int c,d,x,y;
	int xb[n],yb[n],colo[n];
	for(int i=0;i<n;i++){
		
		cin>>c>>d>>x>>y;
		colo[i]=c;
		xb[i]=x;
		yb[i]=y;
		if(d==0){
			for(int o=y;o<y+2;o++){
				for(int j=x;j<x+4;j++){
					F[j][o]=c;
				}
			}
		}
		else if(d==1){
			for(int o=y;o<y+4;o++){
				for(int j=x;j<x+2;j++){
					F[j][o]=c;
				}
			}
		}
		
	}
	
	for(int i=0;i<n;i++){
		if(xb[i]==xs&&yb[i]==ys){
			DFScolor(xb[i],yb[i],colo[i],xg,yg);
			break;
		}
	}
	if(F[xg][yg]==0)
		cout<<"OK"<<endl;
	if(F[xg][yg]!=0)
		cout<<"NG"<<endl;
	
}
	return 0;
}

C++: #include<iostream>
using namespace std;
int w, h, xs, ys, xg, yg, stage[101][101];
int ny[4] = { 0, 1, 0, -1 }, nx[4] = { 1, 0, -1, 0 };
int bc;
bool ans = false;
void func(int x, int y, int lastp){
	if (x == xg && y == yg){
		ans = true;
	}
	for (int i = 0; i < 4; i++){
		if (stage[y + ny[i]][x + nx[i]] != bc || i == lastp) return;
		func(x + nx[i], y + ny[i], (i + 2) % 4);
	}
}
int main(){
	int n, c, d, x, y, bw, bh;
	while (1){
		for (int i = 0; i < 101; i++){
			for (int j = 0; j < 101; j++){
				stage[i][j] = 0;
			}
		}
		cin >> w >> h;
		if (w == 0 && h == 0) return 0;
		cin >> xs >> ys >> xg >> yg >> n;
		for (int i = 0; i < n; i++){
			cin >> c >> d >> x >> y;
			if (d == 0){
				bw = 4;
				bh = 2;
			}
			else{
				bw = 2;
				bh = 4;
			}
			for (int j = 0; j < bh; j++){
				for (int k = 0; k < bw; k++){
					stage[y + j][x + k] = c;
				}
			}
		}
		bc = stage[ys][xs];
		if (bc == 0) cout << "NG\n";
		func(1, 1, -1);
		if (ans == true) cout << "OK\n";
		else cout << "NG\n";
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int board[101][101];
int h,w;
int xs,ys,xg,yg;
int n;

int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
bool search(int c,int y,int x){
	if(y<1||h<y||x<1||w<x||c!=board[y][x])return false;
	if(y==yg&&x==xg)return true;
	board[y][x]=0;
	for(int i=0;i<4;i++)
		if(search(c,y+dy[i],x+dx[i]))return true;
	return false;
}
int main(){
	while(cin>>w>>h,w|h){
		int c,d,x,y;
		cin>>xs>>ys>>xg>>yg>>n;
		fill_n((int*)board,sizeof(board)/sizeof(int),0);
		for(int i=0;i<n;i++){
			cin>>c>>d>>x>>y;
			int dx=2,dy=2;
			if(d==0)dx=4;
			else dy=4;
			for(int i=0;i<dy;i++)
				for(int j=0;j<dx;j++)
					board[y+i][x+j]=c;
		}
		if(board[ys][xs]!=0 && board[yg][xg]!=0 && search(board[ys][xs],ys,xs))cout<<"OK\n";
		else cout<<"NG\n";
	}
}