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 n, w, h, xs, ys, xg, yg, f[105][105] = { 0 }, dx[] = { 0,1,0,-1 }, dy[] = { 1,0,-1,0 }, used[105][105] = { 0 }, color = 0;
bool flag;
void dfs(int y, int x) {
	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (xg == nx&&yg == ny) {
			flag = true;
			return;
		}
		if (used[ny][nx]) continue;
		if (1 <= nx&&nx <= w && 1 <= ny&&ny <= h&&f[ny][nx] == color) {
			used[ny][nx] = 1;
			dfs(ny, nx);
		}
	}
	return;
}

int main() {
	while (1) {
		flag = false;
		cin >> w >> h;
		if (w == 0 && h == 0) return 0;
		cin >> xs >> ys >> xg >> yg >> n;
		for (int i = 0; i < n; i++) {
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			if (d == 0) {
				for (int j = 0; j < 2; j++) {
					for (int k = 0; k < 4; k++) {
						f[y + j][x + k] = c;
					}
				}
			}
			else {
				for (int j = 0; j < 4; j++) {
					for (int k = 0; k < 2; k++) {
						f[y + j][x + k] = c;
					}
				}
			}
		}
		used[ys][xs] = 1;
		color = f[ys][xs];
		dfs(ys, xs);
		if (flag) cout << "OK" << endl;
		else cout << "NG" << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int fie[102][102];
int check[102][102];
typedef struct state{
  int x;
  int y;
  int cost;
  state(int x,int y,int cost):x(x),y(y),cost(cost){}
};
int w,h;
int xg,yg;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
void makefie(int c,int d,int x,int y){
  int xm=0,ym=0;
  //cout << "oo";
  //fflush(stdout);
  if(d){ xm=2; ym=4;}
  else{ xm=4; ym=2;}
  for(int i=x; i < x+xm; i++){
    for(int j=y; j < y+ym; j++)
      fie[i][j]=c;
  }
  //  cout << "ee";
  // fflush(stdout);
}
int solve(int x,int y,int c){
  queue <state> q;
  q.push( state(x,y,0) );
  // cout << x << " " << y << " "<<endl;
  while(!q.empty()){
    state p = q.front(); q.pop();
    int cost = p.cost;
    for(int i=0;i<4;i++){
      int nx=p.x+dx[i];
      int ny=p.y+dy[i];
      if(nx <= 0 || nx >w || ny <= 0 || ny>h) continue;
      if(fie[nx][ny]!=c) continue;
      if(nx==xg && ny==yg) return 1;
      if(check[nx][ny]!=-1) continue;
      check[nx][ny]=cost+1;
      q.push( state(nx,ny,cost+1) );
      //  cout << nx << " " << ny << " "<< cost+1<<endl;
    }
  }
  return 0;
}
main(){
  int n;
  int xs,ys;
  while(1){
    cin >> w >>h;
    if(w==0 && h==0) break;
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;
    memset(fie,0,sizeof(fie));
    memset(check,-1,sizeof(check));
    for(int i=0;i<n;i++){
      int c,d,x,y;
      cin >> c >> d >> x >> y;
      makefie(c,d,x,y);
    }

    if(fie[xs][ys]==0){ cout << "NG"<<endl; continue;}
    int res=solve(xs,ys,fie[xs][ys]);
    if(res) cout << "OK" <<endl;
    else cout << "NG" << endl;
  }
}

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

//bool b;
//int xg , yg;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int board[105][105];
int  sc;
/*
void dfs(int x , int y ,int w , int h){
	if(x==xg&&y==yg) {
		b=true;
		return;
	}
	for(int i=0;i<4;++i){
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(tx<=0 || ty<=0 || tx>w || ty>h || board[tx][ty]!=sc || board[tx][ty]==0 || b ) continue;
		dfs( tx , ty , w , h );
	}
}
*/

void dfs(int x , int y , int w , int h){
	if(x<=0 || y<=0 || x>w || y >h) return;
	if(board[x][y]==sc){
		board[x][y]=10;
		for(int i=0;i<4;++i){
			dfs(x+dx[i] , y+dy[i] , w , h);
		}
	}
}
int main(){
	int w , h;
	while(cin >> w >> h , w|h){
		int xs , ys , n;
		int xg , yg;
		memset(board,0,sizeof(board));
		cin >> xs >> ys >> xg >> yg >> n;
		for(int i=0;i<n;++i){
			int c , d , x , y;
			cin >> c >> d >> x >> y;
			if(!d){
				for(int k=y;k<y+2;++k){
					for(int j=x;j<x+4;++j){
						board[j][k]=c;
					}
				}
			} else {
				for(int k=y;k<y+4;++k){
					for(int j=x;j<x+2;++j){
						board[j][k]=c;
					}
				}
			}
		}
		sc=board[xs][ys];
		dfs( xs , ys , w , h );
		cout << ( board[xg][yg]==10 ? "OK" : "NG") << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

#define rep(i, j) FOR(i, 0, j)
#define rep2(i, k) for(int i = 1; i <= k; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define INF (1 << 30)

using namespace std;
 
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int field[105][105], use[105][105];
int h, w, sx, sy, gx, gy, n;
int scol;

void make_map(int x,int y,int chk,int color){
  if(chk == 0){
    for(int i=y;i<y+2;i++){
      for(int j=x;j<x+4;j++) field[i][j] = color;
    }
  }
  else {
    for(int i=y;i<y+4;i++){
      for(int j=x;j<x+2;j++) field[i][j] = color;
    }
  }
}

bool dfs(int x,int y){
  use[y][x] = true;
  if(x == gx && y == gy) return true;
  for(int i=0;i<4;i++){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(nx >= 0 && nx < w && ny >= 0 && ny < h && field[ny][nx] == field[sy][sx] && !use[ny][nx]) dfs(nx,ny);
  }
  return use[gy][gx];
}

int main() {
	while(cin >> w >> h, w) {
		memset(field, 0, sizeof(field));
		memset(use, false, sizeof(use));
		int scol = 0;
		cin >> sx >> sy >> gx >> gy;
		--sx; --sy; --gx; --gy;
		cin >> n;
		while(n--){
     		int c,d,x,y; cin >> c >> d >> x >> y; --x; --y;
      		make_map(x,y,d,c);
    	}
		if(dfs(sx, sy)) cout << "OK" << endl;
		else cout << "NG" << endl; 
	}
	return 0;
}