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 <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int w, h;
int sx, sy;
int gx, gy;
int n;
int field[120][120];

void PrintField() {
  REP(y, h + 2) {
    REP(x, w + 2) {
      printf("%d", field[y][x]);
    }
    puts("");
  }
}

int main() {
  while (scanf("%d %d", &w, &h), w|h) {
    MEMSET(field, 0);
    scanf("%d %d", &sx, &sy);
    scanf("%d %d", &gx, &gy);
    scanf("%d", &n);
    REP(i, n) {
      int c, d, sx, sy;
      scanf("%d %d %d %d", &c, &d, &sx, &sy);
      int ex = sx + 2;
      int ey = sy + 2;
      if (d == 0) { ex += 2; }
      if (d == 1) { ey += 2; }
      FOR(y, sy, ey) {
        FOR(x, sx, ex) {
          field[y][x] = c;
        }
      }
    }
    w += 3;
    int sc = field[sy][sx];
    queue<int> que;
    que.push(sy * w + sx);
    while (!que.empty()) {
      int x = que.front() % w;
      int y = que.front() / w;
      que.pop();
      if (sc == 0) { break; }
      if (x == gx && y == gy) {
        puts("OK");
        goto next;
      }
      REP(dir, 4) {
        const int dx[4] = { 1, 0, -1, 0 };
        const int dy[4] = { 0, 1, 0, -1 };
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        if (field[ny][nx] != sc) { continue; }
        field[ny][nx] = 9;
        que.push(ny * w + nx);
      }
    }
    puts("NG");
next:;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
const int limit = 100;
int w, h, n;
int xs, ys, xg, yg;
int maze[limit][limit];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int flag = 0;

void dfs(int y, int x){
    maze[y][x] = 0;
    //4近傍を探索
    for(int i=0; i < 4; i++){
            int nx = x + dx[i], ny = y + dy[i];
            if(0 <= nx and nx < h and 0 <= ny and ny < w and maze[ny][nx] != 0){
                if(nx == (xg-1) and ny == (yg-1)) { flag = 1; return; }
                dfs(ny, nx);
            }
        }
    return;
}


int main(){
    while(1){
    flag = 0;
    cin >> w >> h;
    if(w == 0 and h == 0) break;
    cin >> xs >> ys >> xg >> yg >> n;
    //ボードを初期化
    for(int i=0; i < w; i++)for(int j=0; j < h; j++) maze[i][j] = 0;
    //ボードにブロックを敷き詰める
    for(int i=0; i < n; i++){
        int c = 0, d = 0, x = 0, y = 0;
        cin >> c >> d >> x >> y;
        if(d)for(int dx=0; dx < 2; dx++)for(int dy=0; dy < 4; dy++) maze[y-1+dy][x-1+dx] = c;
        else for(int dy=0; dy < 2; dy++)for(int dx=0; dx < 4; dx++) maze[y-1+dy][x-1+dx] = c;
    }
    //迷路を単純化する
    for(int i=0; i < w; i++)for(int j=0; j < h; j++) if(maze[i][j] != maze[ys-1][xs-1]) maze[i][j] = 0;
    //debugの為に出力
    /*
    for(int i=0; i < w; i++){
        for(int j=0; j < h; j++) cout << maze[i][j];
        cout << endl;
    }
    */
    //search
    dfs(ys-1, xs-1);
    if(flag) cout << "OK" << endl;
    else cout << "NG" << endl;
    }
    return 0;
}

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

bool dfs(int x,int y,int color);
int w,h;
int xs,ys;
int xg,yg;
int board[111][111];
int cheak[111][111];
int xd[]={0,-1,0,1};
int yd[]={-1,0,1,0};
int main(){
  int n;
  int c,d,x,y;
  int a[]={1,0,1,0,1,0,1};
  int b[]={0,1,1,2,2,3,3};
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;
    memset(board,0,sizeof(board));
    memset(cheak,0,sizeof(cheak));
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;
    for(int i=0;i<n;i++){
      cin >> c >> d >> x >> y;
      board[y-1][x-1]=c;
      for(int j=0;j<7;j++){
	if(d==0) board[y+a[j]-1][x+b[j]-1]=c;
	else board[y+b[j]-1][x+a[j]-1]=c;
      }
    }
    if(board[ys][xs]!=0){
      if(dfs(xs-1,ys-1,board[ys-1][xs-1])==true) cout << "OK" << endl;
      else cout << "NG" << endl;
    }
    else cout << "NG" << endl;
  }
}

bool dfs(int x,int y,int color){
  cheak[y][x]=1;
  if(x==xg-1 && y==yg-1) return true;
  for(int i=0;i<4;i++){
    int xn=x+xd[i];
    int yn=y+yd[i];
    if(xn>=0 && xn<w && yn>=0 && yn<h && board[yn][xn]==color && cheak[yn][xn]==0){
      if(dfs(xn,yn,board[yn][xn])==true) return true;
    }
  }
  return false;
}