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 F[105][105];
int xg,yg,w,h;
  
  
void DFS(int Y,int X,int c){
  if(F[Y][X] != c){
    return;
  }
  
  F[Y][X] = -1;
  DFS(Y+1,X  ,c);
  DFS(Y-1  ,X,c);
  DFS(Y,X+1  ,c);
  DFS(Y  ,X-1,c);
}
  
  
int main(){
  int xs,ys,n;
  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0)
      break;
for(int i=0; i<105; i++){
      for(int j=0; j<105; j++){
        F[i][j] = 10;
      }
    }
      
    cin >> xs >> ys >> xg >> yg >> n;
  
  
    for(int i=0;i<n;i++){
      int c,d,x,y;
      cin >> c >> d >> x >> y;
      //if(c == 0)
        //c = 2345;
      if(d == 0){
        for(int j=0;j<4;j++){
          F[x+j][y] = c;
          F[x+j][y+1] = c;
        }
      } else {
        for(int j=0;j<4;j++){
          F[x][y+j] = c;
          F[x+1][y+j] = c;
        }
      }
    }
 
  DFS(xs,ys,F[xs][ys]);
  
  if(F[xg][yg] == -1){
    cout << "OK" << endl;
  } else {
    cout << "NG" <<endl;
  }
  }
}

C++: #include <iostream>
#include <queue>
#include <cstring>
const int MAX_W = 100, MAX_H = 100;

typedef std::pair<int,int> P;
int map[MAX_H + 1][MAX_W + 1], w, h;

int bfs(P sp, P gp, int c){
	//½±Æª é©ði[
	int f[MAX_H + 1][MAX_W + 1] = {{0}};
	std::queue<P> q;
	q.push(P(sp.first, sp.second));
	
	while(q.size()){
		P p = q.front(); q.pop();
		//S[Å«½ç¬÷
		if(p.first == gp.first && p.second == gp.second && map[p.second][p.first] == c)return 1;
		
		int vy[4] = {-1,1,0,0}, vx[4] = {0,0,-1,1};
		for(int i=0;i<4;i++){
			int nx = p.first + vx[i],
				ny = p.second + vy[i];
			if(nx >= 0 && nx <= w && ny >= 0 && ny <= h && 
				 map[ny][nx] == c && !f[ny][nx]){
				q.push(P(nx,ny));
				f[ny][nx] = 1;
			}
		}
	}

	//ÅãÜÅS[Å«È¢Èç¸s
	return 0;
}

int main(){
	P sp, gp;
	while(std::cin >> w >> h, w && h){
		memset(map,0,sizeof(map));
		std::cin >> sp.first >> sp.second;
		std::cin >> gp.first >> gp.second;

		int n;
		std::cin >> n;
		//ubNÌÝu
		while(n--){
			int c, d, x, y;
			std::cin >> c >> d >> x >> y;
			
			int mx, my;
			if(d)//w < h
				my = 4, mx = 2;
			else//w > h
				mx = 4, my = 2;
			for(int i=y;i<y+my;i++)
				for(int j=x;j<x+mx;j++)
					map[i][j] = c;
		}

		int sc = map[sp.second][sp.first];

		//bfsÅS[Å«é©mF
		if(bfs(sp, gp, sc))std::cout << "OK" << std::endl;
		else{std::cout << "NG" << std::endl;}
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int W, H, xs, ys, xg, yg, N;
int g[128][128];
int dy[] = {0,-1,0,1}, dx[] = {-1,0,1,0};
int start;

int dfs(int y, int x){
  //  printf("%d %d\n", y, x);
  if(y == yg && x == xg) return 1;
  g[y][x] = 0;
  rep(d, 4){
    int ny = y+dy[d], nx = x+dx[d];
    if(ny < 0 || ny >= H || nx < 0 || nx >= W || g[ny][nx] != start) continue;
    if(dfs(ny, nx)) return 1;
  }
  return 0;
}

int main(){
  while(scanf("%d%d", &W, &H) && W+H){
    scanf("%d%d%d%d%d", &xs, &ys, &xg, &yg, &N); xs--; ys--; xg--; yg--;
    memset(g, -1, sizeof(g));
    rep(i, N){
      int c, d, x, y;
      scanf("%d%d%d%d", &c, &d, &x, &y); x--; y--;
      if(!d){
	rep(i, 2) rep(j, 4) g[i+y][j+x] = c;
      }else{
	rep(i, 4) rep(j, 2) g[i+y][j+x] = c;
      }
    }
    start = g[ys][xs];
    /*    rep(i, H){
      rep(j, W) printf("%d", g[i][j]); puts("");
      }*/

    puts(dfs(ys, xs)?"OK":"NG");
  }

  return 0;
}

C++: #include <cstdio>
#include <functional>
#include <vector>
#include <algorithm>

int testcase_ends() {
  int w, h;
  scanf("%d %d", &w, &h);
  if (w == 0 && h == 0)
    return 1;

  int xs, ys;
  scanf("%d %d", &xs, &ys);

  int xg, yg;
  scanf("%d %d", &xg, &yg);

  std::vector<std::vector<int>> g(h+2, std::vector<int>(w+2));

  int n;
  scanf("%d", &n);

  for (int i=0; i<n; ++i) {
    int c, d, x, y;
    scanf("%d %d %d %d", &c, &d, &x, &y);

    int dh=2, dw=2;
    (d? dh:dw) += 2;

    for (int j=0; j<dh; ++j)
      for (int k=0; k<dw; ++k)
	g[y+j][x+k] = c;
  }

  // for (int i=0; i<h; ++i)
  //   for (int j=0; j<w; ++j)
  //     fprintf(stderr, "%d%c", g[i][j], j+1<w? ' ':'\n');

  if (g[ys][xs] != g[yg][xg])
    return !printf("NG\n");

  const int dy[]={-1, 0, 1, 0};
  const int dx[]={0, -1, 0, 1};
  std::function<bool (int, int)> dfs=[&](int x, int y) {
    if (x == xg && y == yg) return true;

    int cs=g[y][x];
    g[y][x] = -cs;  // mark visited
    for (int k=0; k<4; ++k) {
      int cd=g[y+dy[k]][x+dx[k]];
      if (cs == cd && dfs(x+dx[k], y+dy[k]))
	return true;
    }
    return false;
  };

  printf("%s\n", dfs(xs, ys)? "OK":"NG");
  return 0;
}      

int main() {
  while (!testcase_ends()) {}
}