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 "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using pii = pair<int,int>;
#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1000000007;
void cioacc(){//accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
int main(){
  int di[] = {1,-1,0,0};
  int dj[] = {0,0,1,-1};
  int w,h;
  while(cin >> w >> h,w){
    vector<vector<int>> field(w+2,vector<int> (h+2,0));
    int sx,sy,gx,gy;
    cin >> sx >> sy >> gx >> gy;
    int n; cin >> n;
    REP(i,n){
      int c,d,x,y;
      cin >> c >> d >> x >> y;
      if(d==1){
        REP(j,2) REP(k,4) field[x+j][y+k] = c;
      }else{
        REP(j,4) REP(k,2) field[x+j][y+k] = c;
      }
    }
    bool flag = false;
    int color = field[sx][sy];
    queue<pair<int,int>> task;
    task.push(make_pair(sx,sy));
    while(color>0&&!task.empty()){
      pii u = task.front(); task.pop();
      if(u.first==gx && u.second==gy){
        flag = true;
        break;
      }
      REP(i,4){
        int ni = u.first + di[i];
        int nj = u.second + dj[i];
        if(field[ni][nj] == color){
          task.push(make_pair(ni,nj));
          field[ni][nj] = -1;
        }
      }
    }
    cout << (flag? "OK":"NG") << endl;
  }
}

C++: #include<cstdio>
#include<cstring>
#include<stack>
struct p{
	int x;
	int y;
};
using namespace std;
int main(){
	int w,h,sx,sy,gx,gy,n,c,d,x,y,i,j,k,o;
	int map[100][100];
	int used[100][100];
	int mx[4]={-1,0,1,0};
	int my[4]={0,-1,0,1};
	p v,u;
	stack<p> s;
	while(1){
		scanf("%d %d",&w,&h);
		if(w==0&&h==0){
			break;
		}
		memset(map,0,sizeof(map));
		memset(used,0,sizeof(used));
		scanf("%d %d",&sx,&sy);
		sx--;
		sy--;
		scanf("%d %d",&gx,&gy);
		gx--;
		gy--;
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			x--;
			y--;
			if(!d){
				for(j=y;j<y+2;j++){
					for(k=x;k<x+4;k++){
						map[j][k]=c;
					}
				}
			}
			else{
				for(j=y;j<y+4;j++){
					for(k=x;k<x+2;k++){
						map[j][k]=c;
					}
				}
			}
		}
		if(map[sy][sx]!=map[gy][gx]){
			printf("NG\n");
			continue;
		}
		v.x=sx;
		v.y=sy;
		s.push(v);
		o=0;
		while(!s.empty()){
			v=s.top();
			s.pop();
			if(used[v.y][v.x]){
				continue;
			}
			else{
				used[v.y][v.x]=1;
			}
			if(v.x==gx&&v.y==gy){
				o=1;
				while(!s.empty()){
					s.pop();
				}
				break;
			}
			for(i=0;i<4;i++){
				u=v;
				u.x+=mx[i];
				u.y+=my[i];
				if(u.x>=0&&u.x<w&&u.y>=0&&u.y<h&&map[u.y][u.x]==map[sy][sx]){
					s.push(u);
				}
			}
		}
		if(o){
			printf("OK\n");
		}
		else{
			printf("NG\n");
		}
	}
	return 0;
}

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

int F[102][102];

bool DFS(int Y, int X, int C, int yg, int xg)
{
  if(Y == yg && X == xg)
    return true;
  if(F[Y][X]!=C)
    return false;

  F[Y][X] = 0;

  bool tmp = 0;
  tmp += DFS(Y-1, X, C, yg, xg);
  tmp += DFS(Y+1, X, C, yg, xg);
  tmp += DFS(Y, X-1, C, yg, xg);
  tmp += DFS(Y, X+1, C, yg, xg);
  return tmp;
}

int main()
{
  while(1){
    F[102][102] = {}

    int w, h;
    cin >> w >> h;

    if(w == 0 && h == 0)
      return 0;

    int xs, ys;
    cin >> xs >> ys;

    int xg, yg;
    cin >> 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=0; j<2; j++)
          for(int k=0; k<4; k++)
            F[y+j][x+k] = c;
      else
        for(int j=0; j<4; j++)
          for(int k=0; k<2; k++)
            F[y+j][x+k] = c;
    }

    bool res = DFS(ys, xs, F[ys][xs], yg, xg);

    if(res)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
}

C++: #include<iostream>
using namespace std;
int dy[4]={-1,0,1,0};
int dx[4]={0,1,0,-1};
int t[200][200],vd[200][200];
int W,H,sx,sy,gx,gy,n,key;

bool inField(int y,int x){
  if(y<0||y>=H)return false;
  if(x<0||x>=W)return false;
  return true;
}

bool solve(int y,int x){
  if(y==gy&&x==gx)return true;
  for(int i=0;i<4;i++){
    int ny=y+dy[i],nx=x+dx[i];
    if(!inField(ny,nx))continue;
    if(t[ny][nx]!=key)continue;
    if(vd[ny][nx]==1)continue;
    vd[ny][nx]=1;
    if( solve(ny,nx) ) return true;
  }
  return false;
}

void init(){
  for(int i=0;i<200;i++)
    for(int j=0;j<200;j++)
      t[i][j]=vd[i][j]=-1;
}

int main(){
  int a,b,x,y;
  while(cin>>W>>H){
    if(W==0&&H==0)break;
    init();
    cin>>sx>>sy>>gx>>gy>>n;
    sx--,sy--,gx--,gy--;
    for(int k=0;k<n;k++){
      cin>>a>>b>>x>>y;
      x--,y--;
      for(int i=0;i<(b==0?2:4);i++)
	for(int j=0;j<(b==0?4:2);j++)
	  t[y+i][x+j]=a;
    }
    key=t[sy][sx];
    cout<<(key!=-1&&solve(sy,sx)?"OK":"NG")<<endl;
  }
  return 0;
}