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<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

bool isMaze;
int blocks[105][105], xg, yg, w,h;

void dfs(int target, int x, int y){
    if(x < 0 || y < 0 || x >= w || y >= h) return;
    if(x == xg && y == yg){
        isMaze = true;
        return;
    }
    if(blocks[y][x] != target || blocks[y][x] == 0){
        return;
    }

    blocks[y][x] = 9;
    
    dfs(target, x + 1, y);
    dfs(target, x - 1, y);
    dfs(target, x, y + 1);
    dfs(target, x, y - 1);
    return;
}

int main(){
    int xs, ys, n;
    while(cin >> w >> h, w||h){
        rep(i,h) rep(j,w) blocks[i][j] = 0;
        cin >> xs >> ys >> xg >> yg >> n;
        xs--; ys--; xg--; yg--;
        rep(i,n){
            int c, d, x, y;
            cin >> c >> d >> x >> y;
            x--; y--;
            rep(j, (d?4:2)) rep(k, (d?2:4)) b[x + j][y + k] = c;
        }
        isMaze = false;
        dfs(blocks[ys][xs], xs, ys);

        if(isMaze) cout << "OK" << endl;
        else cout << "NG" << endl;
    }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
#include <iomanip>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int dx[] = {0, 1, 0, 1}, dy[] = {0, 0, 1, 1};
struct P{ int x, y; };
int main()
{
	int w, h;
	while(cin >> w >> h && (w || h))
	{
		vector<vint> field(w, vint(h, 0));
		P s, g;
		cin >> s.x >> s.y >> g.x >> g.y;
		s.x--, s.y--, g.x--, g.y--;

		int n;
		cin >> n;
		rep(i, n)
		{
			int c, d, x, y;
			cin >> c >> d >> x >> y; x--, y--;

			rep(i, 4)
				field[x + dx[i]][y + dy[i]] = c;
			rep(i, 4)
				field[x + dx[i] + 2 * !d][y + dy[i] + 2 * d] = c;
		}

		int c = field[s.x][s.y];
		if(!c || !field[g.x][g.y])
		{
			cout << "NG" << endl;
			continue;
		}

		queue<P> que;
		for(que.push(s); !que.empty(); que.pop())
		{
			P p = que.front();

			rep(i, 4)
			{
				int nx = p.x + dx[i], ny = p.y + dy[i];
				
				if(0 <= nx && nx < w && 0 <= ny && ny < h && field[nx][ny] == c)
				{
					field[nx][ny] = 0;
					P next; next.x = nx, next.y = ny;
					que.push(next);
				}
			}
		}

		cout << (field[g.x][g.y] ? "NG" : "OK") << endl;
	}
}

C++: #include<stdio.h>
#include<algorithm>
#include<functional>
int w,h;
int xs,ys,xg,yg;
int n;
int c,d,x,y;
int map[101][101];
int flg=0,temp;
void func(int ,int);
using namespace std;
int main(void){
	while(1){
		scanf("%d %d",&w,&h);
		if(w==0 && h==0){
			break;
		}
		int i,j,k;
		for(i=1;i<=w;i++){
			for(j=1;j<=h;j++){
				map[i][j]=0;
			}
		}
		scanf("%d %d %d %d %d",&xs,&ys,&xg,&yg,&n);
		for(i=0;i<n;i++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			if(d==0){
				for(k=0;k<=1;k++){
					for(j=0;j<=3;j++){
						map[y+k][x+j]=c;
					}
				}
			}
			else if(d==1){
				for(k=0;k<=3;k++){
					for(j=0;j<=1;j++){
						map[y+k][x+j]=c;
					}
				}
			}
		}
		/*for(i=1;i<=w;i++){
			for(j=1;j<=h;j++){
				printf("%d ",map[i][j]);
			}
			printf("\n");
		}*/
		flg=0;
		if(map[xs][ys]==map[xg][yg] && map[xs][ys]!=0){
			temp=map[xs][ys];
			map[xs][ys]=0;
			func(ys,xs);
		}
		if(flg==1){
			printf("OK\n");
		}
		else {
			printf("NG\n");
		}
	}
	return 0;
}
void func(int yy,int xx){
	int dy[4]={0,1,0,-1},dx[4]={1,0,-1,0};
	int i;
	for(i=0;i<4;i++){
		yy=yy+dy[i];
		xx=xx+dx[i];
		if(yy<=h && yy>=1 && xx<=w && xx>=1 && map[xx][yy]==temp){
			map[xx][yy]=0;
			if(xg==xx && yg==yy){
				flg=1;
				break;
			}
			func(yy,xx);
		}
	}
}

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

int a=0;
int F[102][102];
void DFS(int Xs,int Ys,int c,int Xg,int Yg){
  if(F[Xs][Ys]!=c){
    return;
  }else if(Xs==Xg&&Ys==Yg){
    a=1;
    return;
  }
 F[Xs][Ys]=0;
 DFS(Xs-1,Ys,c,Xg,Yg);
 DFS(Xs,Ys-1,c,Xg,Yg);
 DFS(Xs+1,Ys,c,Xg,Yg);
 DFS(Xs,Ys+1,c,Xg,Yg);
}

int main(){
  while(1){
   int w,h,xs,ys,xg,yg,n,C;
   cin>>w>>h;
   if(w==0&&h==0){
     break;
   }
   cin>>xs>>ys>>xg>>yg>>n;
   int c[n];
   int d[n];
   int x[n];
   int y[n];
   for(int i=0;i<n;i++){
     cin>>c[i]>>d[i]>>x[i]>>y[i];
     if(xs==x[i]&&ys==y[i]){
       C=c[i];
     }
      for(int j=0;j<4;j++){
        for(int k=0;k<2;k++){
          if(d[i]==0){
           F[x[i+j]][y[i+k]]=c[i];
         }else{
           F[x[i+k]][y[i+j]]=c[i];
         }
       }
     }
    }
    DFS(xs,ys,C,xg,yg);
    if(a==1){
      cout<<"OK"<<endl;
    }else{
      cout<<"NG"<<endl;
    }
  }
}