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

int C[102][102];
int ans;
int xg,yg;

void meiro(int W,int H,int c){
  if(W==xg && H==yg){
    ans = 1;
    return;
  }

  if(C[W][H]!=c){
    return;
  }

  C[W][H] = '.';

  meiro(W-1,H  ,c);
  meiro(W  ,H+1,c);
  meiro(W+1,H  ,c);
  meiro(W  ,H-1,c);
}

int main(){
  while(1){
    int w,h;
    cin >> w >> h;

    if(w==0 && h==0){
      break;
    }

    int xs,ys;
    cin >> xs >> ys;

    cin >> xg >> yg;

    int n;
    cin >> n;
    int c[n],d[n],x[n],y[n];

    int cbase;
    for(int i=0;i<w;i++){
      for(int j=0;j<h;j++){
        C[w][h] = 0;
      }
    }
    for(int i=0;i<n;i++){
      cin >> c[i] >> d[i] >> x[i] >> y[i];

      if(d[i]==0){
        for(int j=x[i];j<x[i]+4;j++){
          for(int k=y[i];k<y[i]+2;k++){
            C[j][k]=c[i];
          }
        }
      }
      if(d[i]==1){
        for(int j=x[i];j<x[i]+2;j++){
          for(int k=y[i];k<y[i]+4;k++){
            C[j][k]=c[i];
          }
        }
      }

      if(x[i]==xs &&y[i]==ys){
        cbase = c[i];
      }
    }
    ans = 0;
    meiro(xs,ys,C[xs][ys]);

    if(ans==1){
      cout << "OK" << endl;
    }else{
      cout << "NG" << endl;
    }
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int b[100][100];
int vis[100][100];

int w, h;
bool in(int x, int y) {
	return 0 <= x && x < w && 0 <= y && y < h;
}


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

	while(cin >> w >> h, w + h) {
		int xs, ys, xg, yg, n;
		cin >> xs >> ys >> xg >> yg >> n;
		xs--, ys--, xg--, yg--;

		memset(b, 0, sizeof b);
		for(int i = 0; i < n; i++) {
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			x--, y--;
			int H = 2, W = 4;
			if(d == 1) swap(H, W);
			for(int dx = 0; dx < W; dx++) {
				for(int dy = 0; dy < H; dy++) {
					b[y + dy][x + dx] = c;
				}
			}
		}

		if(b[ys][xs] == 0 || b[ys][xs] != b[yg][xg]) {
			cout << "NG" << endl;
			continue;
		}

		typedef pair<int, int> P;
		queue<P> q;
		q.push({ xs, ys });
		memset(vis, 0, sizeof vis);
		vis[ys][xs] = 1;
		while(q.size()) {
			int x = q.front().first, y = q.front().second;
			q.pop();
			int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
			for(int k = 0; k < 4; k++) {
				int nx = x + dx[k], ny = y + dy[k];
				if(in(nx, ny) && b[y][x] == b[ny][nx] && vis[ny][nx] == 0) {
					vis[ny][nx] = 1;
					q.push({ nx, ny });
				}
			}
		}
		if(vis[yg][xg]) {
			cout << "OK" << endl;
		}
		else {
			cout << "NG" << endl;
		}
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int w,h,f[100][100];
bool flag,used[100][100];
int xs,ys,xg,yg;
int color;

void dfs(int y,int x)
{
	if(y == yg && x == xg)
	{
		flag = true;
		return;
	}

	rep(i,4)
	{
		int nx = x + dx[i];
		int ny = y + dy[i];

		if(0 <= nx && nx < w && 0 <= ny && ny < h && !used[ny][nx])
		{
			if(f[ny][nx] == color)
			{
				used[ny][nx] = true;
				dfs(ny,nx);
			}
		}
	}
	return;
}

int main()
{
	int n;

	while(cin >> w >> h)
	{
		if(w == 0 && h == 0) break;

		cin >> xs >> ys >> xg >> yg;
		cin >> n;

		xs--;
		ys--;
		xg--;
		yg--;

		memset(f,0,sizeof(f));
		rep(i,100) rep(j,100) used[i][j] = false;
		flag = false;
		rep(i,n)
		{
			int c,d,x,y;
			cin >> c >> d >> x >> y;
		
			x--;
			y--;

			if(d == 0)
			{
				rep(j,4) f[y][x+j] = c;
				rep(j,4) f[y+1][x+j] = c;
			}
			else if(d == 1)
			{
				rep(j,4) f[y+j][x] = c;
				rep(j,4) f[y+j][x+1] = c;
			}
		}

		color = f[ys][xs];
		dfs(ys,xs);

		if(flag) cout << "OK" << endl;
		else cout << "NG" << endl;
	}
		return 0;
}

C++: #include<iostream>
#include<queue>

using namespace std ;

int map[ 100 ][ 100 ] ;
int w ,h ;
int gx ,gy ;
int way ;

struct ST
{
	int x ;
	int y ;
} ;

ST s ,d ;

void ans()
{
	queue< ST > que ;
	int mx[ 4 ] = { 1 ,0 ,0 ,-1 } ;
	int my[ 4 ] = { 0 ,1 ,-1 ,0 } ;
	bool came[ 100 ][ 100 ] = { false } ;
	que.push( s ) ;
	came[ s.y ][ s.x ] = true ;
	while( ! que.empty() )
	{
		s = que.front() ;
		if( s.x == gx && s.y == gy )
		{
			cout << "OK" << endl ;
			return ;
		}
		que.pop() ;
		for( int i = 0 ; i < 4 ; i++ )
		{
			s.x += mx[ i ] ;
			s.y += my[ i ] ;
			if( came[ s.y ][ s.x ] == false && map[ s.y ][ s.x ] == way )
			{
				came[ s.y ][ s.x ] = true ;
				que.push( s ) ;
				s.x -= mx[ i ] ;
				s.y -= my[ i ] ;
			}
		}
	}
	cout << "NG" << endl ;
}


int main()
{
	while( cin >> w >> h ,h )
	{
		int n ;
		int color ,muki ,x ,y ;
		cin >> s.x >> s.y ;
		cin >> gx >> gy ;
		cin >> n ;
		while( n )
		{
			n-- ;
			cin >> color >> muki >> x >> y ;
			x-- ;
			y-- ;
			if( muki )
			{
				for( int i = 0 ; i < 4 ; i++ )
				{
					for( int j = 0 ; j < 2 ; j++ )
					{
						map[ y + i ][ x + j ] = color ;
					}
				}
			}
			else
			{
				for( int i = 0 ; i < 2 ; i++ )
				{
					for( int j = 0 ; j < 4 ; j++ )
					{
						map[ y + i ][ x + j ] = color ;
					}
				}
			}
		}
		gx-- ;
		s.x-- ;
		gy-- ;
		s.y-- ;
		way = map[ s.y ][ s.x ] ;
		if( map[ s.y ][ s.x ] == way )
		{
			ans() ;
		}
		else
		{
			cout << "NA" << endl ;
		}
	}
	return 0 ;
}