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 <iostream>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <cmath>
#include <set>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<25;
const double pi=acos(-1);
const double eps=1e-8;
const vi emp;

int W,H,sx,sy,gx,gy,color;
int dx[8]={0,0,0,0,1,1,1,1},dy[8]={0,1,2,3,0,1,2,3},cx[4]={-1,0,0,1},cy[4]={0,-1,1,0};
vvi field;
vvb used;

bool solve(int x,int y,int color,vvb& used){
	used[x][y]=1;
	if(x==gx&&y==gy) return 1;
	for(int i=0;i<4;i++){
		int tmpx=x+cx[i],tmpy=y+cy[i];
		if(tmpx>=0&&tmpx<W&&tmpy>=0&&tmpy<H&&field[tmpx][tmpy]==color&&!used[tmpx][tmpy]){
			if(solve(tmpx,tmpy,color,used)) return 1;
		}
	}
	return 0;
}

int main(){
	while(1){
		cin>>W>>H;
		if(!W) break;
		field=vvi(W,vi(H,-1));
		cin>>sx>>sy>>gx>>gy;
		sx--;
		sy--;
		gx--;
		gy--;
		int n;
		cin>>n;
		int c,d,x,y;
		for(int i=0;i<n;i++){
			cin>>c>>d>>x>>y;
			x--;
			y--;
			if(d){
				for(int i=0;i<8;i++) field[x+dx[i]][y+dy[i]]=c;
			}
			else{
				for(int i=0;i<8;i++) field[x+dy[i]][y+dx[i]]=c;
			}
		}
		if(field[sx][sy]==-1||field[gx][gy]==-1) cout<<"NG"<<endl;
		else {
			int color=field[sx][sy];
			used=vvb(H,vb(W));
			if(solve(sx,sy,color,used)) cout<<"OK"<<endl;
			else cout<<"NG"<<endl;
		}
	}
}

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

	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]&&visited[x][y]==false){
			visited[x][y]=true;
			if(dfs(x+1,y))return true;
		}
		if(ban[x][y+1]==ban[xs][ys]&&visited[x][y]==false){
			visited[x][y]=true;
			if(dfs(x,y+1))return true;
		}
		if(x>=2&&ban[x-1][y]==ban[xs][y]&&visited[x][y]==false){
			visited[x][y]=true;
			if(dfs(x-1,y))return true;
		}
		if(y>=2&&ban[x][y-1]==ban[xs][y]&&visited[x][y]=false;){
			visited[x][y]=true;
			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,visited[i][j]=false;
	
	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 <iostream>
using namespace std;
int F[102][102];

void DFScolor(int X,int Y,int color){

	if(F[X][Y]!=color)
		return;
	
	F[X][Y]=9;
	DFScolor(X-1,Y,color);
	DFScolor(X,Y+1,color);
	DFScolor(X+1,Y,color);
	DFScolor(X,Y-1,color);
	
}


int main(){
while(1){
	int H,W,xg,yg,xs,ys,n,count=0;
	cin>>H;
	if(H==0)
		count++;
	cin>>W;
	if(W==0)
		count++;
	if(count==2)
		return 0;
	cin>>xs>>ys;
	cin>>xg>>yg;	
	cin>>n;
	int c,d,x,y;
	int xb[n],yb[n],colo[n];
	for(int i=0;i<n;i++){
		
		cin>>c>>d>>x>>y;
		colo[i]=c;
		xb[i]=x;
		yb[i]=y;
		if(d==0){
			for(int o=y;o<y+2;o++){
				for(int j=x;j<x+4;j++){
					F[j][o]=c;
				}
			}
		}
		else if(d==1){
			for(int o=y;o<y+4;o++){
				for(int j=x;j<x+2;j++){
					F[j][o]=c;
				}
			}
		}
		
	}
	
	for(int i=0;i<n;i++){
		if(xb[i]==xs&&yb[i]==ys){
			DFScolor(xb[i],yb[i],colo[i],xg,yg);
			
			break;
		}
	}
	if(F[xg][yg]==9)
		cout<<"OK"<<endl;
	else
		cout<<"NG"<<endl;
	
}
}

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

typedef pair<int,int> P;

#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)

const int dy[4] = {-1, 1, 0, 0};
const int dx[4] = {0, 0, -1, 1};
const int MAX_W = 102, MAX_H = 102;
int maze[MAX_H][MAX_W];
int w, h, xs, ys, xg, yg, color;

void solve() {
  memset(maze, 0, sizeof maze);
  int n;
  cin>>xs>>ys>>xg>>yg>>n;
  rep(i,n) {
    int c, d, x, y;
    cin>>c>>d>>x>>y;
    //cout<<c<<d<<x<<y<<endl;
    if (d == 0) {
      REP(j,x,x+4) REP(k,y,y+2) maze[k][j] = c;
    } else {
      REP(j,x,x+2) REP(k,y,y+4) maze[k][j] = c;
    }
  }
  
  /*
  rep(i,h+1) {
    rep(j,w+1) cout<<maze[i][j];
    cout<<endl;
  }*/
  
  if (maze[ys][xs] == 0) {
    cout<<"NG"<<endl;
    return;
  } else {
    color = maze[ys][xs];
  }
  
  bool flag = false;
  queue<P> que;
  que.push(P(xs, ys));
  while (!que.empty() && !flag) {
    P p = que.front(); que.pop();
    int tx = p.first, ty = p.second;
    if (tx == xg && ty == yg) {
      flag = true; continue;
    }
    if (maze[ty][tx] == 0) continue;
    maze[ty][tx] = 0;
    
    rep(z,4) {
      int ttx = tx + dx[z], tty = ty + dy[z];
      if (ttx < 1 || tty < 1 || ttx >= w || tty >= h) continue;
      if (maze[tty][ttx] != color) continue;
      que.push(P(ttx, tty));
    }
  }
  
  if (flag) {
    cout<<"OK"<<endl;
  } else {
    cout<<"NG"<<endl;
  }
}

int main (int argc, char const* argv[]) {
  while (cin>>w>>h, w||h) {
    solve();
  }
  return 0;
}