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>
#include<algorithm>
#include<cstring>
using namespace std;
bool N[102][102];
int map[102][102],R,ga,gb,w,h,f=0;
void block(int ,int);
int main()
{
	int i,n,c,d,X=0,Y=0,x,y;
	cin>>w>>h;
	while(w!=0||h!=0){
		memset(map,0,sizeof(map));
		memset(N,false,sizeof(N));
		cin>>X>>Y;
		cin>>ga>>gb;
		cin>>n;
		for(i=0;i<n;i++){
			cin>>c>>d>>x>>y;
			if(d){
				map[y][x]=c;
				map[y+1][x]=c;
				map[y+2][x]=c;
				map[y+3][x]=c;
				map[y][x+1]=c;
				map[y+1][x+1]=c;
				map[y+2][x+1]=c;
				map[y+3][x+1]=c;

			}
			else{
				map[y][x]=c;
				map[y+1][x+1]=c;
				map[y+1][x+2]=c;
				map[y+1][x+3]=c;
				map[y+1][x]=c;
				map[y][x+1]=c;
				map[y][x+2]=c;
				map[y][x+3]=c;

			}
		}
		R=map[Y][X];
		if(R==map[gb][ga]&&R!=0){
			block(Y,X);
			if(f==1) cout<<"OK"<<endl;
		        else cout<<"NG"<<endl;
		}
		else cout<<"NG"<<endl;
		//for(i=0;i<h;i++){
			//for(j=0;j<w;j++){
			//	cout<<map[i][j];
			//}
				
	//		cout<<endl;
		//}
		f=0;
		cin>>w>>h;
	}
	return 0;
}
void block(int b,int a)
{
	//if(f==1) return 1;
	N[b][a]=true;
	if(ga==a&&gb==b) f=1;
		if(map[b][a+1]==R&&N[b][a+1]==false) block(b,a+1); //???
		if(map[b+1][a]==R&&N[b+1][a]==false) block(b+1,a); //???
		if(map[b][a-1]==R&&N[b][a-1]==false) block(b,a-1); //???
		if(map[b-1][a]==R&&N[b-1][a]==false) block(b-1,a); //???
}

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

bool is_maze(vector<vector<int> > grid, const pair<int,int>& start, const pair<int,int>& goal)
{
  const int W = grid.size();
  const int H = grid[0].size();
  const int color = grid[start.first][start.second];
  queue<pair<int,int> > q;
  q.push(start);
  grid[start.first][start.second] = -1;
  while (!q.empty()) {
    const pair<int,int> p = q.front();
    q.pop();
    if (p == goal) {
      return true;
    }

    for (int d = 0; d < 4; d++) {
      static const int dir_w[] = {-1, 1, 0, 0};
      static const int dir_h[] = {0, 0, -1, 1};
      const int next_w = p.first + dir_w[d];
      const int next_h = p.second + dir_h[d];
      if (next_w >= 0 && next_w < W && next_h >= 0 && next_h < H
          && grid[next_w][next_h] == color) {
        q.push(make_pair(next_w, next_h));
        grid[next_w][next_h] = -1;
      }
    }
  }
  return false;
}

int main()
{
  int W, H;
  while (cin >> W >> H && W != 0) {
    vector<vector<int> > grid(W, vector<int>(H, 0));
    pair<int,int> start, goal;
    cin >> start.first >> start.second;
    --start.first;  --start.second;
    cin >> goal.first >> goal.second;
    --goal.first; --goal.second;
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      --x;  --y;
      if (d == 1) {
        // vert
        for (int i = 0; i < 4; i++) {
          grid[x][y+i] = grid[x+1][y+i] = c;
        }
      } else {
        // horiz
        for (int i = 0; i < 4; i++) {
          grid[x+i][y] = grid[x+i][y+1] = c;
        }
      }
    }
    cout << (is_maze(grid, start, goal) ? "OK" : "NG") << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <queue>
#include <functional>
using namespace std;

#define gcu getchar_unlocked
#define pcu putchar_unlocked
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_T T in(int c){T n=0;bool m=false;while(isspace(c)){c=gcu();}if(c=='-')m=true,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m?-n:n;}
_in() {return in<int>(gcu());}
_T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _vo _il void out
#define _vl _il void outl
_vo(bool b) {pcu('0'+b);}
_vo(const char *s){while(*s)pcu(*s++);}
_vo(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_vo(string s){for(char c:s)pcu(c);}
#endif
_T _vo(T n){static char buf[20];char *p=buf;
	if(n<0)pcu('-'),n*=-1;if(!n)*p++='0';else while(n)*p++=n%10+'0',n/=10;
	while (p!=buf)pcu(*--p);}
_vl(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _vo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _vo(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _vl(T&&... t){out(move(t)...);outl();}

using V = vector<int>;
using VV = vector<V>;
struct P {int x, y;};

int main() {
	int dx[] = {4, 2}, dy[] = {2, 4};
	for (int w, h; w = in(), h = in();) {
		P s = {in() - 1, in() - 1}, g = {in() - 1, in() - 1};
		int n = in();
		VV m(h, V(w));
		while (n--) {
			int c = in(), d = in(), x = in() - 1, y = in() - 1;
			for (int i = 0; i < dy[d]; i++)
				for (int j = 0; j < dx[d]; j++)
					m[i + y][j + x] = c;
		}
		function<bool(int, int, int)> f = [&](int x, int y, int c) {
			if (x == g.x && y == g.y)
				return true;
			P d = {1, 0};
			for (int i = 0; i < 4; i++) {
				int tx = x + d.x, ty = y + d.y;
				if (tx >= 0 && ty >= 0 && tx < w && ty < h && m[ty][tx] == c) {
					m[ty][tx] = 0;
					if (f(tx, ty, c))
						return true;
				}
				swap(d.x, d.y *= -1);
			}
			return false;
		};
		int c = m[s.y][s.x];
		m[s.y][s.x] = 0;
		outl(c ? f(s.x, s.y, c) ? "OK" : "NG" : "NG");
	}
}