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<queue>
#define MAX 100
using namespace std;

int map[MAX+2][MAX+2];
bool check[MAX+2][MAX+2];
int dx[4] = { -1,1,0,0};
int dy[4] = { 0,0,-1,1};

int main () {

  while ( true ) {

    queue<int> next;

    while( !next.empty() ) {
      next.pop();
    }
    
    int w,h;
    cin >> w >> h ;

    if ( w == 0 )
      break;

    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<MAX+2; i++ ) {
      for ( int j=0; j<MAX+2; j++ ) {
	map[i][j] = 0;
	check[i][j] = true;
      }
    }

    for ( int i=0; i<n; i++ ) {
      cin >> c >> d >> x >> y;
      
      //int count = 0;
      for ( int j=0; j<2; j++ ) {
	for ( int k=0; k<4; k++ ) {
	  //cout << count;
	  if ( d == 0 ) {
	    map[y+j][x+k] = c;
	  } else {
	    map[y+k][x+j] = c;
	  }
	}
      }
      //cout << endl;
    }

    next.push(y*1000 + x );

    int point;
    bool goal = false;

    int sc = map[ys][xs];

    /*
    for ( int i=0; i<=h; i++ ) {
      for ( int j=0; j<=w; j++ ) {
	cout << map[i+1][j+1];
      }
      cout << endl;
    }
    cout << endl;
    */
    int xn,yn;
    if ( map[yg][xg] == sc ) {
	
      while ( !next.empty() ) {
	point = next.front();
	next.pop();
	xn = point%1000;
	yn = point/1000;

	if (map[yn][xn] == 0 )
	  continue;

	if (point == yg*1000+xg) {
	  goal = true;
	  break;
	}

	for ( int i=0; i<4; i++ ) {
	  if ( check[yn+dy[i]][xn+dx[i]] ) {
	    check[yn+dy[i]][xn+dx[i]] = false;
	    if ( map[yn+dy[i]][xn+dx[i]] == sc ) {
	      next.push((yn+dy[i])*1000+ xn +dx[i]);
	    }
	  }
	}
      }
    }

    if ( goal )
      cout << "OK\n";
    else
      cout << "NG\n";
  }
}

C++: #include<iostream>
#include<algorithm>

using namespace std;

int board[101][101],w,h;
bool visited[101][101];
int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

bool search(int x,int y,int tarx,int tary,int c)
{
	visited[y][x]=true;
	if(board[y][x]!=c)		return false;
	if(x==tarx && y==tary)	return true;
	
	for(int k=0;k<4;k++){
		int xx=x+dx[k],yy=y+dy[k];
		if(xx<=0 || w<xx || yy<=0 || h<yy)	continue;
		if(!visited[yy][xx] && search(xx,yy,tarx,tary,c))	return true;
	}
	return false;
}

int main()
{
	for(;cin>>w>>h,w;){
		for(int i=1;i<=h;i++)for(int j=1;j<=w;j++)board[i][j]=-1,visited[i][j]=false;
		int xs,ys,xg,yg,n;	cin>>xs>>ys>>xg>>yg>>n;
		while(n--){
			int c,d,x,y;	cin>>c>>d>>x>>y;
			for(int i=0;i<(d?4:2);i++)for(int j=0;j<(d?2:4);j++)board[y+i][x+j]=c;
		}

		if(board[ys][xs]!=board[yg][xg])	cout<<"NG"<<endl;
		else	cout<<(search(xs,ys,xg,yg,board[ys][xs])?"OK":"NG")<<endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#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(), in()}, g = {in(), in()};
		int n = in();
		VV m(h + 1, V(w + 1));
		while (n--) {
			int c = in(), d = in(), x = in(), y = in();
			for (int i = 0; i < dy[d]; i++)
				for (int j = 0; j < dx[d]; j++)
					m[i + y][j + x] = c;
		}
		int c = m[s.y][s.x];
		m[s.y][s.x] = 0;
		function<bool(int, int)> f = [&](int x, int y) {
			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 (m[ty][tx] == c) {
					m[ty][tx] = 0;
					if (f(tx, ty))
						return true;
				}
				swap(d.x, d.y *= -1);
			}
			return false;
		};
		outl(c ? f(s.x, s.y) ? "OK" : "NG" : "NG");
	}
}

C++: #include<bits/stdc++.h>
#define P pair<int,int>
using namespace std;

int dx[]={0,0,0,0,1,1,1,1};
int dy[]={0,1,2,3,0,1,2,3};
int fx[]={-1,0,0,1};
int fy[]={0,-1,1,0};
signed main(){
	int a,b;
	while(cin>>a>>b,a||b){
		int c,d,e,f,g;
	scanf("%d%d%d%d%d",&c,&d,&e,&f,&g);
	c--;d--;e--;f--;
	char h[100][100]{};
	for(int i=0;i<g;i++){
		int j,k,l,m;
		scanf("%d%d%d%d",&j,&k,&l,&m);
		l--;m--;
		if(k){
			for(int n=0;n<8;n++)h[l+dx[n]][m+dy[n]]=j;
		}
		else{
			for(int n=0;n<8;n++)h[l+dy[n]][m+dx[n]]=j;
		}
	}
	queue<P>Q;
	int s[100][100];
for(int ss=0;ss<a;ss++){
	for(int qq=0;qq<b;qq++)s[ss][qq]=-1;
}
	s[c][d]=0;
	Q.push(P(c,d));
	while(Q.size()){
		P w=Q.front();
		Q.pop();
		if(w==P(e,f)){break;}
		for(int z=0;z<4;z++){
			int lx=(w).first+fx[z];
			int ly=(w).second+fy[z];
			if(lx>=0&&lx<a&&ly>=0&&ly<b&&h[lx][ly]==h[c][d]&&s[lx][ly]==-1){
				s[lx][ly]=s[(w).first][(w).second]+1;
				Q.push(P(lx,ly));
			}
		}
	}
if(s[e][f]!=-1)printf("OK\n");
else printf("NG\n");
	}
}