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<cstdio>
#include<cstring>
#include<stack>
struct p{
	int x;
	int y;
};
using namespace std;
int main(){
	int w,h,sx,sy,gx,gy,n,c,d,x,y,i,j,k,o;
	int map[100][100];
	int used[100][100];
	int mx[4]={-1,0,1,0};
	int my[4]={0,-1,0,1};
	p v,u;
	stack<p> s;
	while(1){
		scanf("%d %d",&w,&h);
		if(w==0&&h==0){
			break;
		}
		memset(map,0,sizeof(map));
		memset(used,0,sizeof(used));
		scanf("%d %d",&sx,&sy);
		sx--;
		sy--;
		scanf("%d %d",&gx,&gy);
		gx--;
		gy--;
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			x--;
			y--;
			if(!d){
				for(j=y;j<y+2;j++){
					for(k=x;k<x+4;k++){
						map[j][k]=c;
					}
				}
			}
			else{
				for(j=y;j<y+4;j++){
					for(k=x;k<x+2;k++){
						map[j][k]=c;
					}
				}
			}
		}
		if(map[sy][sx]!=map[gy][gx]){
			printf("NG\n");
			continue;
		}
		v.x=sx;
		v.y=sy;
		s.push(v);
		o=0;
		while(!s.empty()){
			v=s.top();
			s.pop();
			if(used[v.y][v.x]){
				continue;
			}
			else{
				used[v.y][v.x]=1;
			}
			if(v.x==gx&&v.y==gy){
				o=1;
				while(!s.empty){
					s.pop();
				}
				break;
			}
			for(i=0;i<4;i++){
				u=v;
				u.x+=mx[i];
				u.y+=my[i];
				if(u.x>=0&&u.x<w&&u.y>=0&&u.y<h&&map[u.y][u.x]==map[sy][sx]){
					s.push(u);
				}
			}
		}
		if(o){
			printf("OK\n");
		}
		else{
			printf("NG\n");
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int w,h,xs,ys,xg,yg,n,ban[111][111]={{0},{0}};
const int START=4545191;
const int GOAL=454545;
const int CHECKED=432535;

	bool dfs(int x,int y){
		//cout<<x<<" "<<y<<endl;
		/*if(xg==x&&yg==y)return true;
		if(ban[x+1][y]==ban[xs][ys]){
			if(dfs(x+1,y))return true;
		}
		if(ban[x][y+1]==ban[xs][ys]){
			if(dfs(x,y+1))return true;
		}*/
		return false;
	}


int main(){

	while(cin>>w>>h&&w&&h){
	cin>>xs>>ys>>xg>>yg>>n;
	for(int i=0;i<101;i++)
	for(int j=0;j<101;j++)ban[i][j]=0;
	for(int i=0;i<n;i++){
		int c,d,x,y;
		cin>>c>>d>>x>>y;
		if(!d){
			for(int i=x;i<x+4;i++){
				for(int j=y;j<y+2;j++){
					ban[i][j]=c;
				}
			}
		}
		else{
		for(int i=x;i<x+2;i++){
				for(int j=y;j<y+4;j++){
					ban[i][j]=c;
				}
			}
		}
	}
	if(dfs(xs,ys))cout<<"OK"<<endl;
	else cout<<"NG"<<endl;
	}
	return 0;
	}

C++: #include<stdio.h>
int w,h;
int is(int x,int y){
	return (x>=0 && x < w && y>=0 && y< h);
}
int main(void){
	const int dx[4]={0,1,0,-1};
	const int dy[4]={1,0,-1,0};
	int qx[100000];
	int qy[100000];
	int p,mx;
    int n,m[101][101]={0};
    int xs,ys,xg,yg,a,c,d,x,y,cnt,f;
	for(;;){
		scanf("%d %d",&w,&h);
		//printf("%d %d\n",w,h);
		if(w==0)break;
		for(int i=0;i<w;i++){
			for(int j=0;j<h;j++){
				m[i][j]=0;
			}
		}
		scanf("%d %d %d %d",&xs,&ys,&xg,&yg);
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			for(int j=0;j<(d==0?2:4);j++){
				for(int k=0;k<(d==0?4:2);k++){
					m[x-1+k][y-1+j]=c;
				}
			}
		}
		c = m[xs-1][ys-1];
		if(c==0){
			puts("NG");
			continue;
		}
		p=0;
		mx=1;
		qx[0]=xs-1;
		qy[0]=ys-1;
		f=1;
/*
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				printf("%d ",m[j][i]);
			}
			puts("");
		}
				printf("%d\n",c);*/
		while(f && p<mx){
			cnt = 0;
			for(int i=0;i<4;i++){
				x = qx[p] + dx[i];
				y = qy[p] + dy[i];
				if(is(x,y) && m[x][y] == c){
					m[x][y]=-1;
					cnt++;
					qx[p+cnt] = x;
					qy[p+cnt] = y;
					if(x == xg-1 && y == yg-1)f=0;
				}
			}
			mx += cnt;
			p++;
		}
		puts(f?"NG":"OK");
	}
    return 0;
}

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


#define rep(i,n) for(short i = 0; i < n; i++)
#define MAX 50

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

typedef pair<short,short> P;
P S,G;
short w,h;

short color;

bool check(short **maze)
{
	
	queue<P> que;
	que.push(S);
	color = maze[S.second][S.first];

	short nx,ny;
	while(que.size()){
		P p = que.front();	que.pop();

		if(p == G){
			while(que.size())
				que.pop();
			return true;
		}

		rep(i,4){
			nx = p.first + dx[i], ny = p.second + dy[i];
			
			if(nx > -1 && ny > -1 && nx < w && ny < h &&
				maze[ny][nx] == color){
					maze[ny][nx] = -1;
					que.push(P(nx,ny));
			}

		}
	}


	
	return false;
}

int main(void)
{
	short n,c,d,x,y;
	
	while(cin >> w >> h, w){
		cin >> S.first >> S.second;
		cin >> G.first >> G.second;
		S.first = S.first / 2;
		S.second= S.second/ 2;

		short **maze = new short*[h];
		for(int i = 0; i < h; i++)
			maze[i] = new short [w];

		cin >> n;

		rep(i,n){
			cin >> c >> d >> x >> y;
			x /= 2;
			y /= 2;
			x++;
			y++;

			if(d == 0)
				maze[y][x] = maze[y][x+1] = c;
			else
				maze[y][x] = maze[y+1][x] = c;
		}
		
		if(maze[S.second][S.first] == maze[G.second][G.first] && check(maze))
			cout << "OK\n";
		else
			cout << "NG\n";

	}
	
	return 0;
}