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>

#define rep(i, a, b) for(int i = a; i < b; i ++)

using namespace std;

int w, h, xs, ys, xg, yg, n;
int map[100][100] = {0};
int map2[100][100] = {0};
void exp(int x, int y, int c) {
  if(x <= 0 || w <= x || y <= 0 || h <= y) return;
  if(map[x][y] != c || map2[x][y] == 1) return;

  map2[x][y] = 1;

  exp(x-1, y, c);
  exp(x+1, y, c);
  exp(x, y-1, c);
  exp(x, y+1, c);

  return;
}

int main() {
  while(cin >> w >> h >> xs >> ys >> xg >> yg >> n, w) {
    rep(i, 0, h) {
      rep(j, 0, w) {
	map[j][i] = 0;
	map2[j][i] = 0;
      }
    }

    int block[4];
    int start[4];
    rep(i, 0, n) {
      rep(lp, 0, 4) {
	cin >> block[lp];
      }

      switch(block[1]) {
      case(0):
	rep(y, 0, 2) {
	  rep(x, 0, 4) {
	    map[block[2]+x][block[3]+y] = block[0];
	  }
	}
	break;
      case(1):
	rep(y, 0, 4) {
	  rep(x, 0, 2) {
	    map[block[2]+x][block[3]+y] = block[0];
	  }
	}
	break;
      }
    }
    if(map[xs][ys] == 0 || map[xg][yg] == 0 || map[xs][ys] != map[xg][yg]) {
      cout << "NG" << endl;
    }
    else {
      exp(xs, ys, map[xs][ys]);
      cout << ((map2[xg][yg] == 1)?("OK"):("NG")) << endl;
    }
  }    
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

int n, m;
int cl[ 100 ][ 100 ];

int main() {
	queue<PII> q;
	set<PII> s;
	while ( ~scanf("%d %d", &n, &m) && n && m ) {
		CLR( cl );
		bool can = false;
		int sx, sy, gx, gy;
		scanf( "%d %d", &sx, &sy );
		--sx;
		--sy;
		scanf( "%d %d", &gx, &gy );
		--gx;
		--gy;
		int k;
		scanf( "%d", &k );
		for ( int i = 0; i < k; i++ ) {
			int c, d, x, y;
			scanf( "%d %d %d %d", &c, &d, &x, &y );
			--x;
			--y;
			if ( d ) {
				for ( int i = 0; i < 4; i++ ) {
					for ( int j = 0; j < 2; j++ ) {
						cl[ y + i ][ x + j ] = c;
					}
				}
			}
			else {
				for ( int i = 0; i < 2; i++ ) {
					for ( int j = 0; j < 4; j++ ) {
						cl[ y + i ][ x + j ] = c;
					}
				}
			}
		}
		q.push( MP( sx, sy ) );
		while ( q.size() ) {
			int x = q.front().first;
			int y = q.front().second;
			q.pop();
			if ( s.count( MP( x, y ) ) ) {
				continue;
			}
			s.insert( MP( x, y ) );
			if ( x == gx && y == gy ) {
				can = true;
				break;
			}
			for ( int i = 0; i < 4; i++ ) {
				if ( x + dx[ i ] < 0 || x + dx[ i ] == n || y + dy[ i ] < 0 || y + dy[ i ] == m || cl[ y ][ x ] != cl[ y + dy[ i ] ][ x + dx[ i ] ] ) {
					continue;
				}
				q.push( MP( x + dx[ i ], y + dy[ i ] ) );
			}
		}
		printf( "%s\n", can ? "OK" : "NG" );
		while ( q.size() ) {
			q.pop();
		}
		s.clear();
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int board[100][100],w,h,xg,yg;
bool f(int y,int x){
	board[y][x] = 0;
	int dy[] = {-1,0,0,1},dx[] = {0,-1,1,0},i;
	if(y == yg - 1 && x == xg -1) return true;
	for(i = 0;i < 4;i++){
		if(y + dy[i] > -1 && y + dy[i] < h && x + dx[i] > -1 && x + dx[i] < w){
			if(board[y+dy[i]][x+dx[i]] == 2){
				if(f(y+dy[i],x+dx[i])) return true;
			}
		}
	}
	return false;
}
int main(){
	int xs,ys,n,i,c,d,x,y,j,k;
	while(cin >> w >> h && (w != 0 && h != 0)){
		cin >> xs >> ys;
		cin >> xg >> yg;
		cin >> n;
		for(i = 0;i < n;i++){
			cin >> c >> d >> x >> y;
			x -= 1;
			y -= 1;
			if(d == 0){
				for(j = 0;j < 2;j++){
					for(k = 0;k < 4;k++){
						board[y+j][x+k] = c;
					}
				}
			}
			else if(d == 1){
				for(j = 0;j < 2;j++){
					for(k = 0;k < 4;k++){
						board[y+k][x+j] = c;
					}
				}
			}
		}
		if(f(ys - 1,xs -1)) cout << "OK" << endl;
		else cout << "NG" << endl;
		for(j = 0;j < 100;j++){
			for(i = 0;i < 100;i++){
				board[j][i] = 0;
			}
		}
	}
	return 0;
}