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


#define rep(i,n) for(int i = 0; i < n; i++)
#define MAX 50

int dx[] = {0,0,-1,1},
	dy[] = {1,-1,0,0};

typedef pair<int,int> P;
P S,G;
int w,h;
int maze[MAX][MAX];
int color;

bool check()
{
	queue<P> que;
	que.push(S);
	color = maze[S.second][S.first];

	int nx,ny;
	while(que.size()){
		P p = que.front();	que.pop();

		if(p == G)
			return true;

		rep(i,4){
			nx = p.first + dx[i], ny = p.second + dy[i];
			
			if(nx > -1 && ny > -1 && nx < w && ny < h &&
				maze[ny][nx] == color){
					maze[ny][nx] = -1;
					que.push(P(nx,ny));
			}

		}
	}


	
	return false;
}

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

	while(cin >> w >> h, w){
		cin >> S.first >> S.second;
		cin >> G.first >> G.second;
		S.first = S.first / 2;
		S.second= S.second/ 2;

		cin >> n;

		rep(i,n){
			cin >> c >> d >> x >> y;
			x /= 2;
			y /= 2;
			x++;
			y++;

			if(d == 0)
				maze[y][x] = maze[y][x+1] = c;
			else
				maze[y][x] = maze[y+1][x] = c;
		}
		
		if(check())
			cout << "OK\n";
		else
			cout << "NG\n";
/*
		rep(i,y){
			rep(j,x)
				printf("%2d", maze[i][j]);
			cout << endl;
		}

*/
	}
	
	return 0;
}

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

int C[102][102];

void meiro(int W,int H,int c){
  if(C[W][H]!=c){
    return;
  }

  C[W][H] = -1;

  meiro(W-1,H  ,c);
  meiro(W  ,H+1,c);
  meiro(W+1,H  ,c);
  meiro(W  ,H-1,c);
}

int main(){
  while(1){
    int w,h;
    cin >> w >> h;
    if(w==0 && h==0){
      break;
    }

    for(int i=0;i<102;i++){
      for(int j=0;j<102;j++){
        C[i][j] = 10;
      }
    }

    int xs,ys,xg,yg;
    cin >> xs >> ys >> xg >> yg;

    int n;
    cin >> n;

    int c,d,x,y;
    for(int i=0;i<n;i++){
      cin >> c >> d >> x >> y;

      if(d==0){
        for(int j=x;j<x+4;j++){
          for(int k=y;k<y+2;k++){
            C[j][k]=c;
          }
        }
      }else{
        for(int j=x;j<x+2;j++){
          for(int k=y;k<y+4;k++){
            C[j][k]=c;
          }
        }
      }
    }

    meiro(xs,ys,C[xs][ys]);

    if(C[xg][yg] == -1){
      cout << "OK" << endl;
    }else{
      cout << "NG" << endl;
    }
  }

  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 0, -1, 0};
int dy[]={0, -1, 0, 1};

int main(){
    while(1){
        int w, h; cin>>w>>h;
        if(w == 0) return 0;

        int sx, sy, gx, gy; cin>>sx>>sy>>gx>>gy;
        sx--, sy--, gx--, gy--;

        int n; cin>>n;
        vector<vector<int>> board(h, vector<int>(w, 0));
        while(n--){
            int c, d, x, y; cin>>c>>d>>x>>y;
            x--, y--;
            if(d == 0){
                for(int i=y; i<=y+1; i++){
                    for(int j=x; j<=x+3; j++){
                        board[i][j] = c;
                    }
                }
            }
            else{
                for(int i=y; i<=y+3; i++){
                    for(int j=x; j<=x+1; j++){
                        board[i][j] = c;
                    }
                }
            }
        }

        queue<P> que;
        que.push(P(sy, sx));
        int color = board[sy][sx];
        board[sy][sx] = 0;
        if(color == 0){
            cout << "NG" << endl;
            continue;
        }

        while(que.size()){
            int cy, cx;
            tie(cy, cx) = que.front();
            que.pop();

            if(cy == gy && cx == gx){
                cout << "OK" << endl;
                goto END;
            }    

            for(int i=0; i<4; i++){
                int ny = cy + dy[i];
                int nx = cx + dx[i];

                if(0 <= ny && ny < h && 0 <= nx && nx < w){
                    if(board[ny][nx] == color){
                        board[ny][nx] = 0;
                        que.push(P(ny, nx));
                    }
                }
            }
        }

        cout << "NG" << endl;
        END:;
    }
}

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

int w,h;
int field[101][101];
int gx,gy;
int sc;
void init()
{
	for(int i=0;i<w;i++){
		for(int j=0;j<h;j++){
			field[i][j] = 0;
		}
	}
}
bool search(int x,int y)
{
	bool flag = false;
	if(x == gx && y == gy)
		flag = true;
	else{
		int vectorx[4] = {-1,0,1,0},vectory[4] = {0,-1,0,1};
		for(int i=0;i<4;i++){
			if(x+vectorx[i] >= 0 && y+vectory[i] >= 0 && x+vectorx[i] < w && y+vectory[i] < h){
				if(field[x+vectorx[i]][y+vectory[i]] == sc){
					field[x][y] = 0;
					flag |= search(x+vectorx[i],y+vectory[i]);
				}
			}
		}
	}
	return flag;
}
int main()
{
	while(cin >> w >> h){
		if(w == 0 && h == 0)
			break;
		int sx,sy;
		cin >> sx >> sy;
		cin >> gx >> gy;
		int n,c,x,y;
		bool d;
		init();
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> c >> d >> x >> y;
			int vectorx,vectory;
			if(d == 0){
				vectorx = 4;vectory = 2;
			}else{
				vectorx = 2;vectory = 4;
			}
			for(int j=0;j<vectorx;j++){
				for(int k=0;k<vectory;k++){
					field[x+j][y+k] = c;
				}
			}
		}
		sc = field[sx][sy];
		if(search(sx,sy))
			cout << "OK" << endl;
		else
			cout << "NG" << endl;
	}
	return 0;
}