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 gx, int gy )
{
	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<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("NA\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;
				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("NA\n");
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool N[102][102];
int map[102][102],R,ga,gb,w,h,f=0;
void block(int ,int);
int main()
{
	int i,n,c,d,X=0,Y=0,x,y;
	cin>>w>>h;
	while(w!=0||h!=0){
		memset(map,0,sizeof(map));
		memset(N,false,sizeof(N));
		cin>>X>>Y;
		cin>>ga>>gb;
		cin>>n;
		for(i=0;i<n;i++){
			cin>>c>>d>>x>>y;
			if(d){
				map[y][x]=c;
				map[y+1][x]=c;
				map[y+2][x]=c;
				map[y+3][x]=c;
				map[y][x+1]=c;
				map[y+1][x+1]=c;
				map[y+2][x+1]=c;
				map[y+3][x+1]=c;

			}
			else{
				map[y][x]=c;
				map[y+1][x+1]=c;
				map[y+1][x+2]=c;
				map[y+1][x+3]=c;
				map[y+1][x]=c;
				map[y][x+1]=c;
				map[y][x+2]=c;
				map[y][x+3]=c;

			}
		}
		R=map[Y][X];
		if(R==map[gb][ga]&&R!=0){
			block(Y,X);
			if(f==1) cout<<"OK"<<endl;
		        else cout<<"NG"<<endl;
		}
		else cout<<"NG"<<endl;
		//for(i=0;i<h;i++){
			//for(j=0;j<w;j++){
			//	cout<<map[i][j];
			//}
				
	//		cout<<endl;
		//}
		f=0;
		cin>>w>>h;
	}
	return 0;
}
void block(int b,int a)
{
	//if(f==1) return 1;
	N[b][a]=true;
	if(ga==a&&gb==b) f=1;
		if(map[b][a+1]==R&&N[b][a+1]==false) block(b,a+1); //???
		if(map[b+1][a]==R&&N[b+1][a]==false) block(b+1,a); //???
		if(map[b][a-1]==R&&N[b][a-1]==false) block(b,a-1); //???
		if(map[b-1][a]==R&&N[b-1][a]==false) block(b-1,a); //???
}