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<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<utility>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+9
#define EPS 1e-9
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = j; i < (k); i++)
typedef long long ll;
int stage[1024][1024];
int xg,yg;
int w,h;
bool dfs(int x,int y,int col){
	if(x == xg && y == yg && stage[x][y] == col){
		return true;
	}
	rep(i,4){
		if(stage[x+dx[i]][y+dy[i]] == 0)continue;
		if(col == stage[x+dx[i]][y+dy[i]]){
			return dfs(x + dx[i], y + dy[i], col);
		}
	}
	return false;
}
int main(){
	while(scanf("%d%d",&w,&h),w|h){
		memset(stage,0,sizeof(stage));
		int xs,ys;
		scanf("%d%d",&xs,&ys);
		scanf("%d%d",&xg,&yg);

		int n;
		scanf("%d",&n);

		rep(q,n){
			int c,d,x,y;
			scanf("%d%d%d%d",&c,&d,&x,&y);
			if(d == 0){
				reps(i,x,x+4){
					reps(j,y,y+2){
						stage[j][i] = c;
					}
				}

			}
			else{
				reps(i,x,x+2){
					reps(j,y,y+4){
						stage[j][i] = c;
					}
				}
			}
		}
		(dfs(xs,ys,stage[xs][ys])) ? puts("OK") : puts("NG");
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
#define MAX 100
static const int dx[4] = {1, 0, -1, 0};
static const int dy[4] = {0, -1, 0, 1};
int w, h, sx, sy, gx, gy, G[MAX+2][MAX+2];
bool dfs( int x, int y, int color ){
if ( color == 0 || G[x][y] != color ) return false;
if ( x == gx && y == gy ) return true;
G[x][y] = 0;
for ( int r = 0; r < 4; r++ ){
if ( dfs(x + dx[r], y + dy[r], color) ) return true;
}
return false;
}
main(){
int n, c, d, px, py, ww, hh;
while( cin >> w >> h && w ){
cin >> sx >> sy >> gx >> gy >> n;
for ( int y = 0; y < h+2; y++ )
for ( int x = 0; x < w+2; x++ ) G[x][y] = 0;
for ( int i = 1; i <=n; i++ ){
cin >> c >> d >> px >> py;
if ( d == 0 ) { ww = 4; hh = 2; }
else { ww = 2; hh = 4; }
for ( int y = 0; y <= hh-1; y++ )
for ( int x = 0; x <= ww-1; x++ ) G[px+x][py+y] = c;
}
if ( dfs(sx, sy, G[sx][sy]) ) cout << "OK" << endl;
else cout << "NG" << endl;
}
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
int field[128][128];
int dx[] = {-1,0,0,1},dy[] = {0,-1,1,0};
int w,h;
void func(int x,int y,int c){
	if(x < 0 || y < 0 || x >= w || y >= h)return ;
	if(field[y][x] == -1 || field[y][x] == 0)return ;
	if(field[y][x] != c)return ;
	field[y][x] = -1;
	for(int i = 0; i < 4; i++){
		func(x+dx[i],y+dy[i],c);
	}
}
int main(){
	while(scanf("%d%d",&w,&h),w,h){
		int xs,ys,xg,yg,n;
		memset(field,0,sizeof(field));
		scanf("%d%d%d%d%d",&xs,&ys,&xg,&yg,&n);
		for(int i = 0; i < n; i++){
			int c,d,x,y;
			scanf("%d%d%d%d",&c,&d,&x,&y);
			if(d == 0){
				for(int j = x; j < x + 4; j++){
					field[y][j] = c;
					field[y+1][j] = c;
				}
			}
			else{
				for(int j = y; j < y + 4; j++){
					field[j][x] = c;
					field[j][x+1] = c;
				}
			}
		}
		func(xs,ys,field[ys][xs]);
		cout << (field[yg][xg]==-1?"OK":"NG") << endl;
	}
	return 0;
}

C++: // based on http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2784229#1
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
#include <array>
#include <complex>
#include <utility>
#include <map>
int needleX[4] = { -1,0,1,0 };
int needleY[4] = { 0,1,0,-1 };
bool dfs(int x, int y, int goalx, int goaly, std::vector<std::vector<bool>>&canGo) {
  if (x == goalx && y == goaly) { return true; }
  canGo[y][x] = false;
  for (int i = 0; i < 4; i++) {
    int next_x = x + needleX[i];
    int next_y = y + needleY[i];
 
    if (canGo[next_y][next_x]) {
      bool flag = dfs(next_x, next_y, goalx, goaly, canGo);
      if (flag)
	return true;
    }
  }
  return false;
}
int main(void) {
  int h, w, n;
  std::cin >> w >> h;
  std::vector<std::string> ans;
  while(h!=0|| w!=0){
    int color=-1, orient, leftX, leftY;
    int sx, sy, gx, gy;
    std::cin >> sx >> sy >> gx >> gy;
    std::vector<std::vector<int>> board(h + 2, std::vector<int>(w + 2, 0));
    std::vector<std::vector<bool>> canGo(h + 2, std::vector<bool>(w + 2, false));
    std::cin >> n;
    int start = -1, goal = -1;
    for (int i = 0; i < n; i++) {
      std::cin >> color >> orient >> leftX >> leftY;
 
 
      if (orient == 0) {
	for (int j = 0; j <= 3; j++) {
	  for (int k = 0; k <= 1; k++) {
	    board[leftY + k][leftX + j] = color;
	    if (leftX + j == sx && leftY + k == sy) {
	      start = color;
	    }
	    else if (leftX + j == gx && leftY + k == gy) {
	      goal = color;
	    }
	  }
	}
 
      }
      else {
	for (int j = 0; j <= 1; j++) {
	  for (int k = 0; k <= 3; k++) {
	    board[leftY + k][leftX + j] = color;
	    if (leftX + j == sx && leftY + k == sy) {
	      start = color;
	    }
	    else if (leftX + j == gx && leftY + k == gy) {
	      goal = color;
	    }
	  }
	}
      }
    }
    /*for (int i = 0; i <= w; i++) {
      for (int j = 0; j <= h; j++) {
      std::cout << board[i][j] << " ";
      }
      std::cout << std::endl;
      }*/
    if (start != -1) {
      for (int i = 0; i <= h; i++) {
	for (int j = 0; j <= w; j++) {
	  if (board[i][j] == start) {
	    canGo[i][j] = true;
	  }
	}
      }
    }

    if (start != goal || start == -1) {
      ans.push_back("NG");
      continue;
    }

    bool result = dfs(sx, sy, gx, gy, canGo);
    if (result) { ans.push_back("OK"); }
    else { ans.push_back("NG"); }
    std::cin >> w >> h;
  }
  int anssize = ans.size();
  for (int i = 0; i < anssize; i++) {
    std::cout << ans[i] << std::endl;
  }
  return 0;
}