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>
using namespace std;

int F[102][102];

void setblock(int c, int d, int x, int y){
  if(d==0){
    for(int i=0;i<4;i++){
      F[y][x+i]=c;
      F[y+1][x+i]=c;
    }
  }else{
    for(int i=0;i<4;i++){
      F[y+i][x]=c;
      F[y+i][x+1]=c;
    }
  }

}

void DFS(int Y, int X, int c){
  if(F[Y][X]!=c)
    return;

  F[Y][X] = 6;

  DFS(Y-1, X  ,c);
  DFS(Y  , X+1,c);
  DFS(Y+1, X  ,c);
  DFS(Y  , X-1,c);
}

int main(){
  int w,h,xs,ys,xg,yg,n;
  while(true){
  cin >> w >> h;
  for(int i=0;i<h;i++){
   for(int k=0;k<w;k++){
     F[i][k]=-1;
   }
  }
  if(w==0&&h==0){
    break;
  }
  cin >> xs >> ys;
  cin >> xg >> yg;
  cin >> n;
  for(int i=0; i<n; i++){
    int c,d,x,y;
    cin >> c >> d >> x >> y;
    setblock(c,d,x,y);
  }

  DFS(ys,xs,F[ys][xs]);
  if(F[yg][xg]==6){
    cout << "OK" << endl;
  }else{
    cout << "NG" << endl;
  }
}
  return 0;
}

C++: #include<cstdio>
#include<iostream>

using namespace std ;

int mx[] = { 1 ,0 ,0 ,-1 } ;
int my[] = { 0 ,1 ,-1 ,0 } ;

int w ,h ;
int gx ,gy ;

int ok_or_ng( int x ,int y ,bool came[][ 110 ] )
{
	came[ y ][ x ] = false ;

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

	for( int i = 0 ; i < 4 ; i++ )
	{
		int xx = x + mx[ i ] ;
		int yy = y + my[ i ] ;
		if( xx > 0 && xx <= w && yy > 0 && yy <= h && came[ yy ][ xx ] )
			return ok_or_ng( xx ,yy ,came ) ;
	}
	return 0 ;
}

void wide( int x ,int y ,int c ,int map[][ 110 ] )
{

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

	return ;
}

void high( int x ,int y ,int c ,int map[][ 110 ] )
{

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

	return ;
}

int main()
{
	while( cin >> w >> h ,w & h )
	{
		bool came[ 110 ][ 110 ] = { false } ;
		int map[ 110 ][ 110 ] = { 0 } ;

		int sx ,sy ;

		cin >> sx >> sy ;
		cin >> gx >> gy ;

		int n ;
		cin >> n ;					// 0 w	1 h

		int c ,d ,x, y ;
		for( int i = 0 ; i < n ; i++ )
		{
			cin >> c >> d >> x >> y ;
			d ? high( x ,y ,c ,map ) : wide( x ,y ,c ,map ) ;
		}

		c = map[ gy ][ gx ] ;

		for( int i = 1 ; i <= h ; i++ )
		{
			for( int j = 1 ; j <= w ; j++ )
			{
				if( map[ i ][ j ] == c )
					came[ i ][ j ] = true ;
			}
		}

		ok_or_ng( sx ,sy ,came ) ? printf( "OK\n" ) : printf( "NG\n" ) ;
	}

	return 0 ;
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <cassert>
#include <utility>

using namespace std;

typedef pair<int, int> P;

#define mp(a,b) make_pair(a,b)
#define DEBUG(x) cerr<<#x<<":"<<x<<endl;

int W, H;
int SX, SY, GX, GY;

int field[100][100];

inline bool inRange(int x, int y) {
	return 0 <= x && x < W && 0 <= y && y < H;
}

void setBlock(int c, int d, int x, int y) {
	if(d == 0) {
		for(int dy = 0; dy < 2; dy++) {
			for(int dx = 0; dx < 4; dx++) {
				field[x + dx][y + dy] = c;
			}
		}
	}
	else if(d == 1) {
		for(int dy = 0; dy < 4; dy++) {
			for(int dx = 0; dx < 2; dx++) {
				field[x + dx][y + dy] = c;
			}
		}
	}
	else
		assert(false);
}

void pv() {
	cerr << endl;
	cerr << "-----" << endl;
	for(int y = 0; y < H; y++) {
		for(int x = 0; x < H; x++)
			cerr << field[x][y];
		cerr << endl;
	}
	cerr << "-----" << endl;
}

bool visit[100][100];

int main() {
	for(; cin >> W >> H, W || H;) {
		cin >> SX >> SY >> GX >> GY;
		SX--, SY--, GX--, GY--;

		memset(field, 0, sizeof field);
		memset(visit, 0, sizeof visit);
		
		int N; cin >> N;
		for(int i = 0; i < N; i++) {
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			x--, y--;
			setBlock(c, d, x, y);
		}

		queue<P> Q;
		Q.push(mp(SX, SY));
		visit[SX][SY] = true;

		int C = field[SX][SY];

		bool f = false;
		while(!Q.empty()) {
			P p = Q.front(); Q.pop();
			if(p.first == GX && p.second == GY) {
				f = true;
				break;
			}
			for(int i = 0; i < 4; i++) {
				int dx[4] = {1, 0, -1, 0};
				int dy[4] = {0, 1, 0, -1};
				int nx = p.first + dx[i];
				int ny = p.second + dy[i];
				if(inRange(nx, ny) && field[nx][ny] == C && visit[nx][ny] == false) {
					visit[nx][ny] = true;
					Q.push(mp(nx, ny));
				}
			}
		}

		cout << (f ? "OK" : "NG") << endl;
	}
}

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



typedef vector<vector<int> > Matrix;
int w,h,xs,ys,xg,yg,n,color;
string res = "NG";
void write(Matrix &board,int c, int d, int x, int y){
  int width,length;
  if(d == 0){ width = 4; length =2; }
  if(d == 1){ width = 2; length =4; }
  for(int i=0; i<width; ++i){
    for(int j=0; j<length; ++j)
      board[x+i][y+j]=c;
  }
}
void func(Matrix &board,int nx,int ny){
  if(board[nx][ny] == 0){
    res = "NG";
    return;
  }
  if( nx==xg && ny==yg){
    res = "OK";
    return;
  }
  board[nx][ny] = 0;
  if( 0<=nx-1 && color==board[nx-1][ny])
    func(board,nx-1,ny);
  if( 0<=ny-1 && color==board[nx][ny-1])
    func(board,nx,ny-1);
  if( nx+1<=w && color==board[nx+1][ny])
    func(board,nx+1,ny);
  if( ny+1<=h && color==board[nx][ny+1])
    func(board,nx,ny+1);
}


int main(void){
  int c,d,x,y;
  while(cin >> w >> h,w||h){
    if( w==0 && h==0) return 0;
    res = "NG";
    cin >> xs >> ys >> xg >> yg >> n;
    xs--; ys--; xg--; yg--;
    Matrix board(w, vector<int>(h,0));
    for(int i=0; i<n; ++i){
      cin >> c >> d >> x >> y;
      --x; --y;
      write(board,c,d,x,y);
    }
    color = board[xs][ys];
    func(board,xs,ys);
    cout << res << endl;
    board.clear();
  }
  return 0;
}