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 <bits/stdc++.h>

#define rep_(v, f, l) for (int v = (f); v < (l); ++v)
#define rep(v, n) rep_(v, 0, n)
#define lep(v, n) rep_(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }

struct UF {
    vector<int> data;
    UF(int n) : data(n, -1) {}
    bool merge(int x, int y){
        x = root(x);
        y = root(y);
        if (x != y){
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return x != y;
    }
    bool isSame(int x, int y){
        return root(x) == root(y);
    }
    int root(int x){
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x){
        return -data[root(x)];
    }
};

int main()
{
    int w, h;
    while (w = in(), h = in()){
        UF uf(w * h);
        int sx, sy;
        sx = in() - 1;
        sy = in() - 1;
        int gx, gy;
        gx = in() - 1;
        gy = in() - 1;
        int n = in();
        int field[128][128] = {};
        rep(i, n){
            int c, d, x, y;
            c = in();
            d = in();
            x = in() - 1;
            y = in() - 1;
            rep(j, 4) rep(k, 2){
                if (!d) field[y + k][x + j] = c;
                else field[y + j][x + k] = c;
            }
        }
        rep(i, h){
            rep(j, w){
                if (i + 1 < h && field[i][j] == field[i + 1][j]){
                    uf.merge(i * w + j, i * w + j + w);
                }
                if (j + 1 < w && field[i][j] == field[i][j + 1]){
                    uf.merge(i * w + j, i * w + j + 1);
                }
            }
        }

        if (uf.isSame(sy * w + sx, gy * w + gx)){
            puts("OK");
        }
        else {
            puts("NG");
        }
    }
    return (0);
}

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

using namespace std;

int W, H, GoalX, GoalY, TargetColor;
int MapData[101][101];
const int Dx[] = {1,0,-1,0}; // right,down,left,up
const int Dy[] = {0,1,0,-1};


void setBlock(int c, int d, int x, int y) {
	int lenX, lenY;

	if (d == 1) {
		lenX = x + 2;
		lenY = y + 4;
	}
	else {
		lenX = x + 4;
		lenY = y + 2;
	}

	for (int i = y; i < lenY; ++i) {
		for (int j = x; j < lenX; ++j) {
			MapData[i][j] = c;
		}
	}
}

bool solve(int x, int y) {
	int nowX, nowY;

	if (x == GoalX && y == GoalY) {
		return true;
	}

	MapData[y][x] = 0;
	for (int i = 0; i < 4; ++i) {
		nowX = x + Dx[i];
		nowY = y + Dy[i];

		if (nowX<1 || nowX>W || nowY<1 || nowY>H) {
			continue;
		}
		if (MapData[nowY][nowX] == TargetColor && solve(nowX, nowY)) {
			return true;
		}
	}
	return false;
}

int main() {
	int n, c, d, x, y, startX, startY;

	while (cin >> W >> H, W) {
		cin >> startX >> startY >> GoalX >> GoalY >> n;
		memset(MapData, 0, sizeof(MapData));
		for (int i = 0; i < n; ++i) {
			cin >> c >> d >> x >> y;
			setBlock(c, d, x, y);
		}

		TargetColor = MapData[startY][startX];
		//cout << "startColor:" << TargetColor << endl;
		if (TargetColor == 0) {
			cout << "NG" << endl;
			continue;
		}

		if (solve(startX, startY)) {
			cout << "OK" << endl;
		}
		else {
			cout << "NG" << endl;
		}
	}

	return 0;
}

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);
    }

    int res=solve(xs,ys,fie[xs][ys]);
    if(res) 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];
bool memo[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(memo[y][x])return ;
	if(field[y][x] != c)return ;
	memo[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));
		memset(memo,0,sizeof(memo));
		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 << (memo[yg][xg]==1?"OK":"NG") << endl;
	}
	return 0;
}