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;

struct board{
  int mass[102][102]={0};
};

bool goal_flag;
void DFS(int x,int y,int gx,int gy,board b,int c){
    if(b.mass[y][x]!=c)return;
    if(!b.mass[y][x])return;
    b.mass[y][x]=-1;
    if(x==gx&&y==gy)goal_flag=true;
    DFS(x+1,y,gx,gy,b,c);
    DFS(x-1,y,gx,gy,b,c);
    DFS(x,y+1,gx,gy,b,c);
    DFS(x,y-1,gx,gy,b,c);
}

int main(){
  while(1){
    int h,w;
    int start_x,start_y;
    int goal_x,goal_y;
    int n;

    cin>>w>>h;
    if(w==0&&h==0)break;
    cin>>start_x>>start_y;
    cin>>goal_x>>goal_y;
    cin>>n;

    board b;

    int c,d,x,y;
    for(int i=0;i<n;i++){
        cin>>c>>d>>x>>y;
        for(int j=0;j<2;j++){
          for(int k=0;k<4;k++){
            if(d==0){b.mass[y+j][x+k]=c;}
            else if(d==1){b.mass[y+k][x+j]=c;}
          }
        }
    }
    goal_flag=false;
    DFS(start_x,start_y,goal_x,goal_y,b,b.mass[start_y][start_x]);
    if(goal_flag)cout<<"OK";
    else cout<<"NG";
    cout<<endl;
  }
}

C++: 
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int i,j;
int a[105][105];
int used[105][105];
int h,w;

void search(int y,int x,int c){
  if(x<0 || y<0 || y>=h || x>=w || a[y][x]!=c)return;
  if(used[y][x])return;
  used[y][x]++;
  int i;
  int dx[4]={-1,0,1,0};
  int dy[4]={0,-1,0,1};
  rep(i,4)
    search(y+dy[i],x+dx[i],c);
  return;
}

int main(void) {
  while(1){
    cin>>w>>h;
    if(h==0 && w==0)break;
    rep(i,h)rep(j,w)used[i][j]=0;
    int xs,ys,xg,yg,n;
    cin>>xs>>ys;
    xs--;ys--;
    cin>>xg>>yg;
    xg--;yg--;
    cin>>n;
    rep(i,n){
      int c,d,x,y;
      cin>>c>>d>>x>>y;
      x--;y--;
      int p[8]={0,0,0,0,1,1,1,1};
      int q[8]={0,1,2,3,0,1,2,3};
      if(d)
	rep(j,8)
	  a[y+q[j]][x+p[j]]=c;
      else
	rep(j,8)
	  a[y+p[j]][x+q[j]]=c;
    }  
    if(a[ys][xs]==0){
      cout<<"NG"<<endl;
      continue;
    } 
    search(ys,xs,a[ys][xs]);//dfs
    if(used[yg][xg])
      cout<<"OK"<<endl;
    else
      cout<<"NG"<<endl;
  }
  return 0;
}

C++: // AOJ 0207
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct UF {
	vector <int> data;
	int cnt;
	UF(int n) : data(n, -1), cnt(n) {}
	bool merge(int a, int b){
		a = root(a), b = root(b);
		if (a != b){
			cnt--;
			if (data[b] < data[a]) std::swap(a, b);
			data[a] += data[b];
			data[b] = a;
		}
		return a != b;
	}
	bool same(int a, int b){
		return root(a) == root(b);
	}
	int root(int a){
		return data[a] >= 0 ? data[a] = root(data[a]) : a;
	}
	int size(int a){
		return ~data[root(a)];
	}
	int count(){
		return cnt;
	}
};

int main()
{
	int w, h;
	while (cin >> w >> h, w){
		int sx, sy, gx, gy, n;
		cin >> sx >> sy >> gx >> gy;
		cin >> n;

		int field[128][128] = {};
		for (int i = 0; i < n; i++){
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			x--; y--;
			for (int j = 0; j < 4; j++){
				for (int k = 0; k < 2; k++){
					int jj = j, kk = k;
					if (d == 0) swap(jj, kk);
					jj += y; kk += x;
					field[jj][kk] = c;
				}
			}
		}

		UF uf(128*128);

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (field[i][j] == 0) continue;
				if (field[i][j] == field[i][j + 1]){
					uf.merge(i * w + j, i * w + j + 1);
				}
				if (field[i][j] == field[i + 1][j]){
					uf.merge(i * w + j, i * w + w + j);
				}
			}
		}

		sy--; sx--; gx--; gy--;
		if (uf.same(sy * w + sx, gy * w + gx)){
			cout << "OK" << endl;
		}
		else {
			cout << "NG" << endl;
		}

	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int bord[101][101]={},playx[2],playy[2];
int movex[]={1,0,-1,0},movey[]={0,1,0,-1};
int serch(int x,int y,int color)
{
    if(color==0)return 0;
    int now=0;
    bord[y][x]=0;
    if(x==playx[1]&&y==playy[1])return 1;
    for(int i=0;i<4&&now==0;i++){
        if(bord[movey[i]+y][movex[i]+x]==color)now=serch(movex[i]+x,movey[i]+y,color);
    }
    return now;
}
int main()
{
    int  h,w;
    while(cin>>h>>w,h,w){
        for(int i=0;i<=h+1;i++){
                for(int j=0;j<=w+1;j++){
                        bord[i][j]=0;
                }
        }
        for(int i=0;i<2;i++)cin>>playx[i]>>playy[i];
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            int co,mu,x,y;
            cin>>co>>mu>>x>>y;
            for(int j=0;j<4;j++){
                if(mu==1){
                    bord[y+j][x]=co;
                    bord[y+j][x+1]=co;
                }
                else{
                    bord[y][x+j]=co;
                    bord[y+1][x+j]=co;
                }
            }
        }
        if(serch(playx[0],playy[0],bord[playy[0]][playx[0]]))cout<<"OK\n";
        else cout<<"NG\n";
    }
}