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>

using namespace std;

bool dfs(int xs,int ys,int xg,int yg,vector<vector<bool> >& canGo){
canGo[xs][ys]=false;
if(xs==xg&&ys==yg)return true;
if(canGo[xs+1][ys]){if(dfs(xs+1,ys,xg,yg,canGo))return true;}
if(canGo[xs-1][ys]){if(dfs(xs-1,ys,xg,yg,canGo))return true;}
if(canGo[xs][ys+1]){if(dfs(xs,ys+1,xg,yg,canGo))return true;}
if(canGo[xs][ys-1]){if(dfs(xs,ys-1,xg,yg,canGo))return true;}
return false;
}
int main(void){
int w,h,xs,ys,xg,yg,n;
bool ans,endflag;
vector<vector<bool> > blockmap;
while(true){
endflag=false;
cin>>w>>h>>xs>>ys>>xg>>yg>>n;
if(w==0&&h==0)endflag=true;
blockmap=vector<vector<bool> >(w+2,vector<bool>(h+2,true));
for(int i=0;i<n;i++){
    int c,d,x,y;
    cin>>c>>d>>x>>y;
    if(c!=2){
        blockmap[x+1][y+1]=false;
        blockmap[x+2][y+1]=false;
        blockmap[x+1][y+2]=false;
        blockmap[x+2][y+2]=false;
        if(d==0){
            blockmap[x+3][y+1]=false;
            blockmap[x+3][y+2]=false;
            blockmap[x+4][y+1]=false;
            blockmap[x+4][y+2]=false;
        }
        else {
            blockmap[x+1][y+2]=false;
            blockmap[x+2][y+3]=false;
            blockmap[x+1][y+4]=false;
            blockmap[x+2][y+4]=false;
        }
    }
}
ans=dfs(xs,ys,xg,yg,blockmap);
if(ans)cout<<"OK"<<endl;
else cout<<"NG"<<endl;
if(endflag)break;
}
return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int meiro[110][110];
int cc;
int w,h;
int xx[]={1,-1,0,0};
int yy[]={0,0,1,-1};

void dfs(int x,int y){
	meiro[y][x]=-1;
	int i;
	for(i=0;i<4;i++){
		if(x+xx[i]<0||w<=x+xx[i]||y+yy[i]<0||h<=y+yy[i])continue;
		if(meiro[y+yy[i]][x+xx[i]]!=cc)continue;
		dfs(x+xx[i],y+yy[i]);
	}
}

int main() {
	int xs,ys,xg,yg,n,c,d,x,y;
	while(w=in(),h=in()){
		cin>>xs>>ys>>xg>>yg>>n;
		xs--;ys--;xg--;yg--;
		int i;
		memset(meiro,0,sizeof(meiro));
		for(i=0;i<n;i++){
			cin>>c>>d>>x>>y;
			x--;y--;
//cout<<c<<" "<<d<<" "<<x<<" "<<y<<endl;
			if(d==0){
				meiro[y][x]=meiro[y][x+1]=meiro[y][x+2]=meiro[y][x+3]=meiro[y+1][x]=meiro[y+1][x+1]=meiro[y+1][x+2]=meiro[y+1][x+3]=c;
			}else{
				meiro[y][x]=meiro[y][x+1]=meiro[y+1][x]=meiro[y+1][x+1]=meiro[y+2][x]=meiro[y+2][x+1]=meiro[y+3][x]=meiro[y+3][x+1]=c;
			}
		}
		if(meiro[ys][xs]==0){
			cout<<"NG\n";
			continue;
		}
		cc=meiro[ys][xs];
//cout<<"hoge\n";
		dfs(xs,ys);
//cout<<"piyo\n";
		if(meiro[yg][xg]==-1)cout<<"OK\n";
		else cout<<"NG\n";
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

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

int H,W,sy,sx,gy,gx;
int N;

int C[100][100];
bool vis[100][100];

signed main(){
    while(cin>>W>>H,W||H){
        cin>>sx>>sy>>gx>>gy;
        cin>>N;
        sy--;sx--;gy--;gx--;
        memset(C,0,sizeof(C));
        rep(i,N){
            int c,d,x,y;
            cin>>c>>d>>x>>y;
            x--;y--;
            for(int dy=0;dy<2;dy++){
                for(int dx=0;dx<4;dx++){
                    int ny,nx;
                    if(!d)ny=y+dy,nx=x+dx;
                    else ny=y+dx,nx=x+dy;
                    C[ny][nx]=c;
                }
            }
        }
        if(!C[sy][sx]){
            cout<<"NG"<<endl;
            continue;
        }

        memset(vis,0,sizeof(vis));
        queue<pint>que;
        que.push(pint(sy,sx));
        vis[sy][sx]=true;
        while(que.size()){
            int y,x;
            tie(y,x)=que.front();
            que.pop();
            rep(i,4){
                int ny=y+dy[i],nx=x+dx[i];
                if(ny<0||ny>=H||nx<0||nx>=W||C[y][x]!=C[ny][nx]||vis[ny][nx])continue;
                vis[ny][nx]=true;
                que.push(pint(ny,nx));
            }
        }
        if(vis[gy][gx])cout<<"OK"<<endl;
        else cout<<"NG"<<endl;
    }
    return 0;
}