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 <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(), in()}, g = {in(), in()};
		int n = in();
		VV m(h + 2, V(w + 2));
		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 <queue>
#include <cstring>
#include <iostream>
using namespace std;
int W, H, sx, sy, gx, gy, N, c, d, x, y, r[111][111], vis[111][111];
int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
int main() {
	while(cin >> W >> H, W | H) {
		cin >> sx >> sy >> gx >> gy; sx--, sy--, gx--, gy--;
		memset(r, 0, sizeof(r));
		cin >> N;
		for(int i = 0; i < N; i++) {
			cin >> c >> d >> x >> y; x--, y--;
			int zx = (d ? 2 : 4), zy = (d ? 4 : 2);
			for(int j = 0; j < zy; j++) {
				for(int k = 0; k < zx; k++) {
					r[y + j][x + k] = c;
				}
			}
		}
		memset(vis, 0, sizeof(vis)); vis[sy][sx] = 1;
		queue<pair<int, int> > que; que.push(make_pair(sx, sy));
		while(!que.empty()) {
			pair<int, int> v = que.front(); que.pop();
			for(int i = 0; i < 4; i++) {
				int tx = v.first + dx[i], ty = v.second + dy[i];
				if(0 <= tx && tx < W && 0 <= ty && ty < H && r[ty][tx] == r[v.second][v.first]) {
					vis[ty][tx] = 1;
					que.push(make_pair(ty, tx));
				}
			}
		}
		cout << (vis[gy][gx] ? "OK" : "NG") << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define INF 1010001000
#define loop(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
typedef pair<int, int> i_i;

bool dfs(vector<vector<int> > &g);

int sx, sy, gx, gy;
int n, m; 
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};

int main()
{

while(cin >> n >> m,(n|m)){
 m++; n++;
 vector<vector<int> > graph(m,vector<int>(n,0));
 
 cin>>sx>>sy>>gx>>gy;
 int bl;  cin>>bl;
 loop(i, bl){
  int a,b,c,t; cin>>a>>b>>c>>t;
  if(!b){
   for(int j=t;j<t+2;j++){
    for(int k=c;k<c+4;k++){
     graph[j][k] = a;
    }
   } 
  } else {
   for(int j=c;j<c+2;j++){
    for(int k=t;k<t+4;k++){
     graph[j][k] = a;
    }
   }
  }
 }
cout << ((dfs(graph)) ? "OK" : "NG") << endl;
}
return 0;
}

bool dfs(vector<vector<int> > &g) {
int cl = g[sy][sx];
vector<vector<bool> > vsed(m,vector<bool>(n,false));
queue<i_i > q;
q.push(mp(sx,sy));
while(!q.empty()){
int x = q.front().first, y = q.front().second;
q.pop();
if(x == gx && y == gy){
return true;
}
if(vsed[y][x]){
continue;
} else {
vsed[y][x]=true;
}

loop(i, 4) {
int nx = x +dx[i], ny = y +dy[i];
if(0<nx && nx < n && 0<ny && ny < m){
if(g[ny][nx] == cl){
q.push(mp(nx,ny));
}
} 
}

}
return false;
}

C++: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int color[200][200];
bool flg[200][200];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int w,h,n,c,d,x,y;

void dfs(int ny,int nx,int gy,int gx){
	flg[ny][nx]=true;
	rep(i,4){
		int nex=nx+dx[i];
		int ney=ny+dy[i];
		if(nex>=w || nex<0 || ney>=h || ney<0)continue;
		if(color[ney][nex]==color[ny][nx] && flg[ney][nex]==false)dfs(ney,nex,gy,gx);
	}
	return;
}

int main(){
	pair<int,int> s,g;
	while(1){
		rep(i,200)rep(j,200)color[i][j]=flg[i][j]=0;
		cin>>w>>h;
		if(w==0 && h==0)break;
		cin>>s.first>>s.second;
		cin>>g.first>>g.second;
		cin>>n;
		rep(i,n){
			cin>>c>>d>>x>>y;
			if(d==0){
				rep(j,2){
					rep(k,4){
						color[y-1+j][x-1+k]=c;
					}
				}
			}else{
				rep(j,4){
					rep(k,2){
						color[y-1+j][x-1+k]=c;
					}
				}
			}
		}
		dfs(s.second-1,s.first-1,g.second-1,g.first-1);
		if(flg[g.second-1][g.first-1])cout<<"OK"<<endl;
		else cout<<"NG"<<endl;
	}
	return 0;
}