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 <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define EACH( v, c ) for ( auto &v : c )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

const int dir[][2] = {
	1, 0,
	0, 1,
	-1, 0,
	0, -1
};

string solve( VVI board, int sy, int sx, int gy, int gx )
{
	const int H = board.size(), W = board[0].size();

	int color = board[ sy ][ sx ];
	
	queue< PII > que;
	que.push( MP( sy, sx ) );

	set< PII > visited;
	visited.insert( MP( sy, sx ) );

	while ( !que.empty() )
	{
		auto cur = que.front();
		que.pop();

		if ( cur == MP( gy, gx ) )
		{
			return "OK";
		}

		REP( d, 0, 4 )
		{
			auto next = cur;

			next.fst += dir[d][0];
			next.snd += dir[d][1];

			if ( !( 0 <= next.fst && next.fst < H && 0 <= next.snd && next.snd < W ) || 
				 EXIST( visited, next ) ||
				 board[ next.fst ][ next.snd ] != color )
			{
				continue;
			}

			que.push( next );
			visited.insert( next );
		}
	}

	return "NG";
}			

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	while ( true )
	{
		int w, h;
		cin >> w >> h;

		if ( !( w | h ) )
		{
			break;
		}

		int sx, sy, gx, gy, n;
		cin >> sx >> sy >> gx >> gy >> n;

		sx--;
		sy--;
		gx--;
		gy--;

		VVI board( h, VI( w, 0 ) );

		REP( i, 0, n )
		{
			int c, d, x, y;
			cin >> c >> d >> x >> y;

			x--;
			y--;

			if ( !d )
			{
				REP( j, 0, 2 )
				{
					REP( k, 0, 4 )
					{
						board[ y + j ][ x + k ] = c;
					}
				}
			}
			else
			{
				REP( j, 0, 4 )
				{
					REP( k, 0, 2 )
					{
						board[ y + j ][ x + k ] = c;
					}
				}
			}
		}

		cout << solve( board, sy, sx, gy, gx ) << endl;
	}

	return 0;
}

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

char F[102][102];

void DFS(int x,int y,char c){
  if(F[x][y]!=c)
    return;
  F[x][y]='@';
  DFS(x-1,y,c);
  DFS(x,y+1,c);
  DFS(x+1,y,c);
  DFS(x,y-1,c);
}
void Block(int c,int d,int x,int y){
  char b;
  if(c=0)
    b='+';
  if(c=1)
    b='-';
  if(c=2)
    b='*';
  if(c=3)
    b='/';
  F[x][y]=b;
  F[x][y+1]=b;
  F[x+1][y]=b;
  F[x+1][y+1]=b;
  if(d==1){
    F[x][y+2]=b;
    F[x][y+3]=b;
    F[x+1][y+2]=b;
    F[x+1][y+3]=b;
  }else{
    F[x+2][y]=b;
    F[x+2][y+1]=b;
    F[x+3][y]=b;
    F[x+3][y+1]=b;
  }
}

int main(){
  int w=1,h;
  int xs,ys;
  int xg,yg;
  int n;
  int c,d,x,y;
  while(w>0){
    cin>>w>>h;
    if(w==0&&h==0)
      break;
    for(int i=0;i<w;i++){
      for(int j=0;j<h;j++){
        F[i][j]='.';
      }
    }
    cin>>xs>>ys;
    cin>>xg>>yg;
    cin>>n;
    for(int i=0;i<n;i++){
      cin>>c>>d>>x>>y;
      Block(c,d,x,y);
    }
    DFS(xs,yscout<<endl;,F[xs][ys]);
    if(F[xg][yg]=='@')
      cout<<"OK"<<endl;
    else
      cout<<"NG"<<endl;
  }
}

C++: #include<iostream>

int field[102][102];//y,x
bool flag;
int w,h,n,sx,sy,gx,gy,col;

void erase(){
	for(int i=0; i<102; i++)
		for(int j=0; j<102; j++)
			field[i][j] = 0;
}

void DFS(int x, int y){
	int xx = x+1;
	int yy = y;

	if(xx == gx && yy == gy){
		flag = true;
		return;
	}
	if(xx <= w)
		if(field[yy][xx] == col)
			DFS(xx, yy);
	if(flag)
		return;

	xx = x;
	yy = y+1;
	if(yy <= h)
		if(field[yy][xx] == col)
			DFS(xx, yy);
	if(flag)
		return;

	xx = x-1;
	yy = y;
	if(xx >= 1)
		if(field[yy][xx] == col)
			DFS(xx, yy);
	if(flag)
		return;
			
	xx = x;
	yy = y-1;
	if(yy >= 1)
		if(field[yy][xx] == col)
			DFS(xx, yy);
}

int main(){
	int c,d,bx,by;
	using std::cin;
	while(true){
		flag = false;
		erase();
		cin >> w >> h;
		if(!w && !h)
			return 0;
		cin >> sx >> sy;
		cin >> gx >> gy;
		cin >> n;
		for(int i=0; i<n; i++){
			cin >> c >> d >> bx >> by;
			if(d==0)
				for(int h=by; h<by+2; h++)
					for(int w=bx; w<bx+4; w++)
						field[h][w] = c;
			else
				for(int h=by; h<by+4; h++)
					for(int w=bx; w<bx+2; w++)
						field[h][w] = c;
		}
		col = field[gy][gx];
		DFS(sx, sy);
		if(flag)
			std::cout << "OK\n";
		else
			std::cout << "NG\n";
	}
}