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 <map>
#include <set>
#include <queue>
#include <algorithm>
#include <iostream>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define contains(c, x) ((c).find(x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))

using namespace std;

int a[101][101];
int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };
int main()
{
	int w, h, sx, sy, gx, gy, n;
	while (true) {
		cin >> w >> h;
		if (!(w | h)) break;
		cin >> sx >> sy >> gx >> gy >> n;
		sx--, sy--, gx--, gy--;
		int c, d, x, y;
		rep(k, n) {
			cin >> c >> d >> x >> y;
			if (d == 0) {
				REP(i, y-1, y+1) REP(j, x-1, x+3) a[i][j] = c;
			}
			else {
				REP(i, y-1, y+3) REP(j, x-1, x+1) a[i][j] = c;
			}
		}
		if (a[sy][sx] != a[gy][gx]) {
			puts("NG");
			continue;
		}

		bool f = false;
		queue<pair<int, int> > q;
		set<pair<int, int> > s;
		pair<int, int> start = mp(sy, sx);
		pair<int, int> goal = mp(gy, gx);
		q.push(start);
		s.insert(start);
		while (!q.empty()) {
			pair<int, int> t = q.front(); q.pop();
			if (t == goal) { f = true; break; }
			rep(i, 4) {
				int r = t.first + dy[i];
				int c = t.second + dx[i];
				if (r < 0 || r >= h || c < 0 || c >= w) continue;
				if (a[sy][sx] != a[r][c] || contains(s, mp(r, c))) continue;
				q.push(mp(r, c));
				s.insert(mp(r, c));
			}
		}
		puts((f ? "OK" : "NG"));
	}
	return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;

bool bfs(pair<int,int> [2],int [102][102]);

int main(void){
	int w,h,xs,ys,xg,yg,n,board[102][102],dr[2][2]={4,2,2,4};
	pair<int,int> sg[2];

	while(true){
		cin >> w >> h;

		if(w==0&&h==0)
			break;

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

		sg[0] = make_pair(xs,ys);
		sg[1] = make_pair(xg,yg);

		for(int i=0;i<h+2;i++)
			for(int j=0;j<w+2;j++)
				board[j][i]=0;

		for(int i=0;i<n;i++){
			int c,d,x,y;
			cin >> c >> d >> x >> y;
			for(int j=0;j<dr[d][0];j++)
				for(int k=0;k<dr[d][1];k++)
					board[x+j][y+k]=c;
		}

		cout << (bfs(sg,board)?"OK":"NG");
		cout << endl;

	}

	return 0;
}

bool bfs(pair<int,int> sg[2],int board[102][102]){
	return false;
}

C++: #include<stdio.h>

int w,h;
int masu[100][100];
int uramasu[100][100];
int stx,sty;
int gox,goy;
int flg;

void saiki(int x,int y,int col){
	
	if(x==gox && y==goy){
		flg=1;
	}
	
	if(x+1<w && masu[x+1][y]==col && uramasu[x+1][y]==0){
		uramasu[x+1][y]=1;
		saiki(x+1,y,col);
	}
	
	if(y+1<h && masu[x][y+1]==col && uramasu[x][y+1]==0){
		uramasu[x][y+1]=1;
		saiki(x,y+1,col);
	}
	
	if(x-1>=0 && masu[x-1][y]==col && uramasu[x-1][y]==0){
		uramasu[x-1][y]=1;
		saiki(x-1,y,col);
	}
	
	if(y-1>=0 && masu[x][y-1]==col && uramasu[x][y-1]==0){
		uramasu[x][y-1]=1;
		saiki(x,y-1,col);
	}
}

int main(void){
	while(1){
		
		scanf("%d%d",&w,&h);
		flg=0;
		if(w==0){
			break;
		}
		scanf("%d%d",&stx,&sty);
		scanf("%d%d",&gox,&goy);
		int n;
		scanf("%d",&n);
		
		for(int i=0;i<100;i++){
			for(int j=0;j<100;j++){
				masu[i][j]=0;
				uramasu[i][j]=0;
			}
		}
		for(int i=0;i<n;i++){
			int c,d,x,y;
			scanf("%d%d%d%d",&c,&d,&x,&y);
			switch(d){
				case 0:
				for(int i=0;i<4;i++){
					for(int j=0;j<2;j++){
						masu[i+x][j+y]=c;
					}
				}break;
				
				case 1:
				for(int i=0;i<2;i++){
					for(int j=0;j<4;j++){
						masu[i+x][j+y]=c;
					}
				}
			}
		}
		
		
		if(masu[stx][sty]!=0){
			saiki(stx,sty,masu[stx][sty]);
			masu[stx][sty]=8;
		}
		
		if(flg==0){
			puts("NG");
		}else{
			puts("OK");
		}
		
	}
	
}