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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

main(){
  int w,h;
  while(cin>>w>>h,w|h){
    int xs,ys,xg,yg,n;
    cin>>xs>>ys>>xg>>yg>>n;
    int maze[w][h];
    memset(maze,0,sizeof(maze));
    rep(i,n){
      int c,d,x,y;
      cin>>c>>d>>x>>y;
      rep(i,4-d*2)rep(j,2+d*2)maze[x+i][y+j]=c;
    }
    queue<PI> Q;
    if(maze[xs][ys]== 0 || maze[xs][ys]!=maze[xg][yg] || maze[xg][yg]==0){
      puts("NG");
      continue;
    }
    bool goal=false;
    Q.push(mp(xs,ys));
    int dp[w][h];
    memset(dp,0,sizeof(dp));
    dp[xs][ys]=1;
    while(!Q.empty()){
      int cx=Q.front().f,cy=Q.front().s;Q.pop();
      if(cx==xg && cy==yg){
        goal=true;
        break;
      }
      rep(i,4){
        int nx=cx+dx[i],ny=cy+dy[i];
        if(nx<0 || nx>=w || ny<0 || ny>=h || dp[nx][ny])continue;
        if(maze[nx][ny]!=maze[xs][ys])continue;
        dp[nx][ny]=1;
        Q.push(mp(nx,ny));
      }
    }
    cout<<(goal?"OK":"NG")<<endl;
  }
}

C++: #include <iostream>
using namespace std;
int board[100][100],w,h,xg,yg;
bool f(int y,int x){
	board[y][x] = -1;
	int dy[] = {-1,0,0,1},dx[] = {0,-1,1,0},i;
	if(y == yg - 1 && x == xg -1) return true;
	for(i = 0;i < 4;i++){
		if(y + dy[i] > -1 && y + dy[i] < h && x + dx[i] > -1 && x + dx[i] < w){
			if(board[y+dy[i]][x+dx[i]] == 2){
				if(f(y+dy[i],x+dx[i])) return true;
			}
		}
	}
	return false;
}
int main(){
	int xs,ys,n,i,c,d,x,y,j,k;
	while(cin >> w >> h && (w != 0 && h != 0)){
		cin >> xs >> ys;
		cin >> xg >> yg;
		cin >> n;
		for(i = 0;i < n;i++){
			cin >> c >> d >> x >> y;
			x -= 1;
			y -= 1;
			if(d == 0){
				for(j = 0;j < 2;j++){
					for(k = 0;k < 4;k++){
						board[y+j][x+k] = c;
					}
				}
			}
			else if(d == 1){
				for(j = 0;j < 2;j++){
					for(k = 0;k < 4;k++){
						board[y+k][x+j] = c;
					}
				}
			}
		}
		if(board[ys - 1][xs - 1] == 2){
			if(f(ys - 1,xs -1)) cout << "OK" << endl;
			else cout << "NG" << endl;
		}
		for(j = 0;j < 100;j++){
			for(i = 0;i < 100;i++){
				board[j][i] = -1;
			}
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<queue>
using namespace std;
int fx[4]={1,-1,0,0},fy[4]={0,0,1,-1};
int main(void)
{
	int **board;
	int bx,by;
	int sx,sy;
	int gx,gy;
	int bnum;
	while(1)
	{
		scanf("%d %d",&bx,&by);
		if(bx==0&&by==0)
		{
			break;
		}
		board=new int*[by];
		for(int f1=0;f1<by;f1++)
		{
			board[f1]=new int[bx];
			for(int f2=0;f2<bx;f2++)
			{
				board[f1][f2]=0;
			}
		}
		scanf("%d %d",&sx,&sy);
		scanf("%d %d",&gx,&gy);
		scanf("%d",&bnum);
		for(int f1=0;f1<bnum;f1++)
		{
			int c,d,x,y;
			scanf("%d %d %d %d",&c,&d,&x,&y);
			x--;y--;
			if(!d)
			{
				for(int f2=0;f2<2;f2++)
				{
					for(int f3=0;f3<4;f3++)
					{
						board[y+f2][x+f3]=c;
					}
				}
			}
			else
			{
				for(int f2=0;f2<4;f2++)
				{
					for(int f3=0;f3<2;f3++)
					{
						board[y+f2][x+f3]=c;
					}
				}
			}
		}
		for(int f1=0;f1<by;f1++)
		{
			for(int f2=0;f2<bx;f2++)
			{
				printf("%d",board[f1][f2]);
			}
			printf("\n");
		}
		int st_col=board[sy-1][sx-1];
		int gl_col=board[gy-1][gx-1];
		if(st_col!=gl_col||st_col==0||gl_col==0)
		{
			printf("NG\n");
			delete []board;
			continue;
		}
		queue<pair<int,int> > que;
		que.push(make_pair(sy,sx));
		int flg=0;
		while(!que.empty())
		{
			pair<int,int> buf=que.front();que.pop();
			if(buf.first==gy-1&&buf.second==gx-1)
			{
				printf("OK\n");
				delete []board;
				flg=1;
				break;
			}
			board[buf.first][buf.second]=9;
			for(int f1=0;f1<4;f1++)
			{
				if(buf.first+fy[f1]>=0&&buf.first+fy[f1]<by&&
					buf.second+fx[f1]>=0&&buf.second+fx[f1]<bx)
				{
					if(board[buf.first+fy[f1]][buf.second+fx[f1]]==st_col)
					{
						que.push(make_pair(buf.first+fy[f1],buf.second+fx[f1]));
					}
				}
			}
		}
		if(flg)
		{
			continue;
		}
		printf("NG\n");
		/*
		for(int f1=0;f1<by;f1++)
		{
			for(int f2=0;f2<bx;f2++)
			{
				printf("%d",board[f1][f2]);
			}
			printf("\n");
		}
		*/
		delete []board;
	}
}

C++: #include <iostream>
using namespace std;
int F[102][102];

void DFScolor(int X,int Y,int color,int Xg,int Yg){

	if(F[X][Y]!=color)
		return;
	
	F[X][Y]=0;
	DFScolor(X-1,Y,color,Xg,Yg);
	DFScolor(X,Y+1,color,Xg,Yg);
	DFScolor(X+1,Y,color,Xg,Yg);
	DFScolor(X,Y-1,color,Xg,Yg);
	
}


int main(){
while(1){
	int count=0;
	int H,W,xg,yg,xs,ys,n;
	cin>>H;
	if(H==0)
		count++;
	cin>>W;
	if(W==0)
		count++;
	if(count==2 || H<4 || W<4)
		return 0;
	cin>>xs>>ys;
	cin>>xg>>yg;	
	cin>>n;
	int c,d,x,y;
	int xb[n],yb[n],colo[n];
	for(int i=0;i<n;i++){
		
		cin>>c>>d>>x>>y;
		colo[i]=c;
		xb[i]=x;
		yb[i]=y;
		if(d==0){
			for(int o=y;o<y+2;o++){
				for(int j=x;j<x+4;j++){
					F[j][o]=c;
				}
			}
		}
		else if(d==1){
			for(int o=y;o<y+4;o++){
				for(int j=x;j<x+2;j++){
					F[j][o]=c;
				}
			}
		}
		
	}
	
	for(int i=0;i<n;i++){
		if(xb[i]==xs&&yb[i]==ys){
			DFScolor(xb[i],yb[i],colo[i],xg,yg);
			break;
		}
	}
	if(F[xg][yg]==0)
		cout<<"OK"<<endl;
	if(F[xg][yg]!=0)
		cout<<"NG"<<endl;
	
}
	return 0;
}