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[102][102];

void DFS(int x, int y, int c){
      if(F[x][y]!=c)
            return;

      F[x][y] = -1;

      DFS(x+1, y  , c);
      DFS(x  , y+1, c);
      DFS(x-1, y  , c);
      DFS(x  , y-1, c);
}

int main(){
      int w,h, xs,ys, xg,yg, n;
      while(1){
            cin >> w >> h;
            if(w==0)    break;
            cin >> xs >> ys;
            cin >> xg >> yg;
            cin >> n;

            int c, d, x, y;
            for(int i=1; i<=n; i++){
                  cin >> c >> d >> x >> y;
                  if(d==0){
                        for(int fx=x; fx<=x+3; fx++){
                        F[fx][y]   = c;
                        F[fx][y+1] = c;
                        }
                  }
                  else if(d==1){
                        for(int fy=y; fy<=y+3; fy++){
                        F[x][fy]   = c;
                        F[x+1][fy] = c;
                        }
                  }
            }

            DFS(xs, ys, F[x][y]);
            if(F[xg][yg]==-1)
                  cout << "OK" << endl;
            else
                  cout << "NG" << endl;
      }

      return 0;
}

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

int map[MAX][MAX];
int xs, ys, xg, yg;
int n;
int w, h;
int c, d, x, y;
int count = 0;
int color;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void jadge(int x, int y){
	if(0 <= x && x < w && 0 <= y && y < h && map[x][y] == color && count == 0){
		if(x == xg && y == yg){
			count = 1;
			return;
		}
		for(int i=0; i < 4; i++){
			jadge(x+dx[i], y+dy[i]);
		}
	}
}
int main(){
	while(1){
		count = 0;

		for(int i=0; i < MAX; i++){
			for(int j=0; j< MAX; j++){
				map[i][j] = 0;
			}
		}

		cin >> w >> h;
		if(w == 0 && h == 0) break;

		cin >> xs >> ys;
		cin >> xg >> yg;
		xs--; ys--; xg--; yg--;
		cin >> n;
		for(int i=0; i < n; i++){
			cin >> c >> d >> y >> x;

			for(int i=0;  i < 4; i++){
				for(int j=0; j < 2; j++){
					if(d == 1){
						map[x-1+i][y-1+j] = c;
					}
					else{
						map[x-1+j][y-1+i] = c;
					}
				}
			}
		}
		if(map[xs][ys] != map[xg][yg]){
			cout << "NG" << endl;
			continue;
		}
		else if(map[xs][ys] == 0 || map[xg][yg] == 0){
			cout << "NG" << endl;
			continue;
		}
		color = map[xs][ys];

		jadge(xs, ys);

		if(count == 1) cout << "OK" << endl;
		else cout << "NG" << 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>>h>>w;
    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++: #include<iostream>
using namespace std;

int a=0;
int F[102][102];
void DFS(int Xs,int Ys,int c,int Xg,int Yg){
  if(F[Xs][Ys]!=c){
    return;
  }else if(Xs==Xg&&Ys==Yg){
    a=1;
    return;
  }
 F[Xs][Ys]=0;
 DFS(Xs-1,Ys,c,Xg,Yg);
 DFS(Xs,Ys-1,c,Xg,Yg);
 DFS(Xs+1,Ys,c,Xg,Yg);
 DFS(Xs,Ys+1,c,Xg,Yg);
}

int main(){
  while(1){
    a=0;
   int w,h,xs,ys,xg,yg,n,C;
   cin>>w>>h;
   if(w==0&&h==0){
     break;
   }
   cin>>xs>>ys>>xg>>yg>>n;
   int c[n],d[n],x[n],y[n];
   for(int i=0;i<n;i++){
     cin>>c[i]>>d[i]>>x[i]>>y[i];
     if(xs==x[i]&&ys==y[i]){
       C=c[i];
     }
      for(int j=0;j<4;j++){
        for(int k=0;k<2;k++){
          if(d[i]==0){
           F[x[i]+j][y[i]+k]=c[i];
         }else{
           F[x[i]+k][y[i]+j]=c[i];
         }
       }
     }
    }
    DFS(xs,ys,C,xg,yg);
    if(a==1){
      cout<<"OK"<<endl;
    }else{
      cout<<"NG"<<endl;
    }
  }
}