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<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int a[][102],int b[][102],int x,int y,int gx,int gy,int c){  
  if(a[x][y]!=c)
    return 0;
  if(x==gx&&y==gy)
    return 1;
  if(b[x][y])
    return 0;
  b[x][y]=1;
  if(f(a,b,x-1,y,gx,gy,c))
    return 1;
  if(f(a,b,x,y-1,gx,gy,c))
    return 1;
  if(f(a,b,x+1,y,gx,gy,c))
    return 1;
  if(f(a,b,x,y+1,gx,gy,c))
    return 1;
  return 0;
}
int main(){
  int i,j,k;
  int a[102][102],b[102][102],w,h,sx,sy,gx,gy,n;
  while(cin>>w>>h&&w+h){
    cin>>sx>>sy>>gx>>gy>>n;
    memset(a,0,sizeof(a));
    for(i=0;i<n;i++){
      int p,q,r,s;
      cin>>p>>q>>r>>s;
      for(j=0;j<2+2*q;j++)
	for(k=0;k<2+2*!q;k++)
	  a[r+k][s+j]=p;
    }
    if(a[sx][sy]!=a[gx][gy])
      cout<<"NG"<<endl;
    else if(a[sx][sy]==0)
      cout<<"NG"<<endl;
    else{
      memset(b,0,sizeof(b));
      if(f(a,b,sx,sy,gx,gy,a[sx][sy]))
	cout<<"OK"<<endl;
      else
	cout<<"NG"<<endl;
    }
  }
  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,int c){
		//cout<<x<<" "<<y<<endl;
		if(xg==x&&yg==y)return true;
		if(ban[x+1][y]==c){
			if(dfs(x+1,y,c))return true;
		}
		if(ban[x][y+1]==c){
			if(dfs(x,y+1,c))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,ban[xs][ys]))cout<<"OK"<<endl;
	else cout<<"NG"<<endl;
	}
	return 0;
	}

C++: #include <stdio.h>
#include <queue>
#include <iostream>

#define MAX 110

int map[MAX][MAX];

void makemap(int c, int d, int x, int y);

int bfs(int xs,int ys,int xg,int yg);

int main()
{
	int w,h,xs,ys,xg,yg,n,c,d,x,y;
	while(1)
	{
	scanf("%d%d",&w,&h);
	if(w==0 && h==0)
		return 0;
	scanf("%d%d",&xs,&ys);
	scanf("%d%d",&xg,&yg);
	scanf("%d",&n);

	for(int i=0;i<MAX;i++)
		for(int j=0;j<MAX;j++)
			map[i][j]=0;

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

	if(bfs(xs+1,ys+1,xg+1,yg+1)==0)
		printf("OK\n");
	else
		printf("NG\n");
	}
	return 0;
}

void makemap(int c, int d, int x, int y)
{
	if(d==0)
		for(int i=x+1;i<x+5;i++)
			for(int j=y+1;j<y+3;j++)
				map[i][j]=c;
	else
		for(int i=x+1;i<x+3;i++)
			for(int j=y+1;j<y+5;j++)
				map[i][j]=c;
}

int bfs(int xs,int ys,int xg,int yg)
{
	int startcolor=map[xs][ys];
	int nowx=xs,nowy=ys;
	using namespace std;
	queue<int> x;
	queue<int> y;
	while(1)
	{
		if(nowx==xg && nowy == yg && map[nowx][nowy]==startcolor )
			return 0;
		map[nowx][nowy]=0;
		if(map[nowx-1][nowy]==startcolor)
		{
			x.push(nowx-1);
			y.push(nowy);
		}
		if(map[nowx][nowy-1]==startcolor)
		{
			x.push(nowx);
			y.push(nowy-1);
		}
		if(map[nowx][nowy+1]==startcolor)
		{
			x.push(nowx);
			y.push(nowy+1);
		}
		if(map[nowx+1][nowy]==startcolor)
		{
			x.push(nowx+1);
			y.push(nowy);
		}
		if(x.empty()==1 && y.empty()==1)
			return 1;
		nowx=x.front();
		nowy=y.front();
		x.pop();
		y.pop();
	}
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool N[101][101];
int map[101][101],R,ga,gb,f=0,w,h;
bool block(int ,int);
int main()
{
	int i,j,a,n,L,b,c,d,X=0,Y=0,x,y;
	cin>>w>>h;
	while(w!=0||h!=0){
		memset(map,0,sizeof(map));
		memset(N,0,sizeof(N));
		cin>>a>>b;
		cin>>ga>>gb;
		cin>>n;
		for(i=0;i<n;i++){
			cin>>c>>d>>x>>y;
			if(d){
				map[y][x]=c;
				map[y+1][x]=c;
				map[y+2][x]=c;
				map[y+3][x]=c;
				map[y][x+1]=c;
				map[y+1][x+1]=c;
				map[y+2][x+1]=c;
				map[y+3][x+1]=c;

			}
			else{
				map[y][x]=c;
				map[y+1][x+1]=c;
				map[y+1][x+2]=c;
				map[y+1][x+3]=c;
				map[y+1][x]=c;
				map[y][x+1]=c;
				map[y][x+2]=c;
				map[y][x+3]=c;

			}
		}
		R=map[b][a];
		if(R==map[gb][ga]){
			if(block(a,b)) cout<<"OK"<<endl;
		        else cout<<"NG"<<endl;
		}
		else cout<<"NG"<<endl;
		//for(i=0;i<h;i++){
			//for(j=0;j<w;j++){
			//	cout<<map[i][j];
			//}
				
	//		cout<<endl;
		//}
		f=0;
		cin>>w>>h;
	}
	return 0;
}
bool block(int b,int a)
{
	//if(f==1) return 1;
	N[b][a]=1;
	if(ga==a&&gb==b) return 1;
	if(a+1!=w){
		if(map[b][a+1]==R&&N[b][a+1]==0) return block(b,a+1);}
	if(b+1!=h){
		if(map[b+1][a]==R&&N[b+1][a]==0) return block(b+1,a);}
	if(a-1!=-1){
		if(map[b][a-1]==R&&N[b][a-1]==0) return block(b,a-1);}
	if(b-1!=-1){
		if(map[b-1][a]==R&&N[b-1][a]==0) return block(b-1,a);}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int mp[101][101];
int sx, sy, gx, gy;
int w, h;

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

bool dfs(int y, int x, int c)
{
	if (y < 1 || y > h || x < 1 || x > w || mp[y][x] != c) return false;

	if (y == gy && x == gx) return true;

	mp[y][x] = 0;

	bool res = false;

	for (int i = 0; i < 4; ++i) res = res || dfs(y + dy[i], x + dx[i], c);

	return res;
}

int main()
{
	while (cin >> w >> h, w || h) {
		cin >> sx >> sy >> gx >> gy;
		int n; cin >> n;
		while (n--) {
			int c, d, x, y; cin >> c >> d >> x >> y;

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

		cout << (dfs(sy, sx, mp[sy][sx]) ? "OK" : "NG") << endl;
	}
}