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>
#include<map>
using namespace std;

const int size = 110;
int board[size][size];
int visited[size][size];
int xblocks[2][8] = { {0, 1, 2, 3, 0, 1, 2, 3},
		      {0, 0, 0, 0, 1, 1, 1, 1} };
int yblocks[2][8] = { {0, 0, 0, 0, 1, 1, 1, 1},
		      {0, 1, 2, 3, 0, 1, 2, 3} };
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main() {
  int w,h,xs,ys,xg,yg,n,c,d,x,y;
  while(cin>>w>>h, w|h) {
    for(int i=0; i<size; ++i)
      for(int j=0; j<size; ++j) {
	board[i][j] = 0;
	visited[i][j] = 0;
      }

    cin>>xs>>ys;
    cin>>xg>>yg;
    cin>>n;
    for(int i=0; i<n; ++i) {
      cin>>c>>d>>x>>y;
      for(int j=0; j<8; ++j)
	board[x+xblocks[d][j]][y+yblocks[d][j]] = c;
    }

    queue<pair<int,int> > Q;
    Q.push(pair<int,int>(xs,ys));
    while(!Q.empty()) {
      pair<int,int> pos = Q.front(); Q.pop();
      int nx = pos.first,ny = pos.second;
      if(visited[ny][nx]) continue;
      visited[ny][nx] = 1;
      if(nx == xg && ny == yg) {
	cout<<"OK"<<endl;
	goto NEXT;
      }

      for(int i=0; i<4; ++i) {
	if(nx+dx[i] > 0 && nx+dx[i] <= w &&
	   ny+dy[i] > 0 && ny+dy[i] <= h &&
	   board[ny][nx] == board[ny+dy[i]][nx+dx[i]])
	  Q.push(pair<int,int>(nx+dx[i],ny+dy[i]));
      }
      board[ny][nx] = -1;
    }
    cout<<"NG"<<endl;
  NEXT:
    ;
  }
}

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>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int c[110][110];
bool vis[110][110];
int h,w,n,x1,y1,x2,y2;
void DFS(int y,int x){
	vis[y][x]=true;
	int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
	lol(i,4){
		int xx=x+dx[i],yy=y+dy[i];
		if(xx<0||yy<0||xx==w||yy==h)continue;
		if(c[y][x]==c[yy][xx]&&!vis[yy][xx])DFS(yy,xx);
	}
}
int main(){
	while(cin>>w>>h&&w+h){
		cin>>x1>>y1>>x2>>y2>>n;
		x1--,x2--,y1--,y2--;
		lol(i,h)lol(j,w)c[i][j]=0,vis[i][j]=false;
		lol(i,n){
			int x,y,a,b;
			cin>>a>>b>>x>>y;x--,y--;
			int xx=2,yy=4;if(b==0)xx=4,yy=2;
			lol(j,yy)lol(k,xx){
				c[j+y][k+x]=a;
			}
		}
		DFS(y1,x1);
		if(vis[y2][x2]==true)cout<<"OK"<<endl;
		else cout<<"NG"<<endl;
	}
	return 0;
}

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

int n;
int w,h;
int xs,ys;
int xg,yg;
int color;
int c,d,xb,yb;

int b[100][100];

bool flag;

void saiki(int y,int x,int c){
	if(b[y][x] != c || flag || b[y][x]==0)return;
	b[y][x]=-1;
	if(y==yg&&x==xg)flag = 1;
	if(x-1>=0)saiki(y,x-1,c);
	if(y-1>=0)saiki(y-1,x,c);
	if(x+1<w)saiki(y,x+1,c);
	if(y+1<h)saiki(y+1,x,c);
}

int main(){
	while(cin >> w >> h && w ){
		/*ú»*/
		for(int i=0;i<100;i++)for(int j=0;j<100;j++)b[i][j]=0;
		/*üÍ*/
		cin >> xs >> ys;
		cin >> xg >> yg;
		xs-=1;ys-=1;xg-=1;yg-=1;
		
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> c >> d >> xb >> yb;
			xb-=1;yb-=1;
			if(d==0){
				for(int i=0;i<4;i++){
					b[yb  ][xb+i]=c;
					b[yb+1][xb+i]=c;
				}
			}else{
				for(int i=0;i<4;i++){
					b[yb+i][xb  ]=c;
					b[yb+i][xb+1]=c;
				}				

			}
		}
		flag = 0;
		saiki(ys,xs,b[ys][xs]);
/*		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++)cout << b[i][j];
			cout << endl;
		}*/
		if(flag)cout << "OK";
		else cout << "NG";
		cout << endl;
	}
}