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 <bits/stdc++.h>
using namespace std;

int board[102][102];
int xg , yg;
int w, h;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int sc;
bool b;

void dfs(int x , int y){
	if(x==xg&&y==yg) {b=true;return;}
	for(int i=0;i<4;++i){
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(tx<=0||ty<=0||tx>w||ty>h) continue;
		if(board[tx][ty]!=sc || board[tx][ty]==0) continue;
		if(b) continue;
		dfs(tx,ty);
	}
}

int main(){
	while(cin >> w >> h , w||h){
		int xs , ys;
		int n;
		b=false;
		memset(board,0,sizeof(board));
		cin >> xs >> ys >> xg >> yg >> n;
		for(int i=0;i<n;++i){
			int c , d , x , y;
			cin >> c >> d >> x >> y;
			if(d==0){
				for(int k=0;k<2;++k){
					for(int j=0;j<4;++j){
						board[x+j][y+k]=c;
					}
				}
			} else if(d==1){
				for(int k=0;k<4;++k){
					for(int j=0;j<2;++j){
						board[x+j][y+k]=c;
					}
				}
			}
		}
		sc=board[xs][ys];
		dfs(xs,ys);
		cout << ( b ? "OK" : "NG") << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = j;i<k;i++)
int w,h,xs,ys,xg,yg,n;
int m[102][102];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool DFS(int x,int y,int c){
  if(m[x][y]!=c)return false;
  if(x==xg && y == yg)return true;
  m[x][y]=-1;
  bool ans =false;
  rep(i,0,4){
    ans = max(ans,DFS(x+dx[i],y+dy[i],c));
  }
  return ans;
}
int main(){
  cin >>w>>h;
  while(w!=0){
    cin>>xs>>ys>>xg>>yg>>n;
    xs++;
    ys++;
    xg++;
    yg++;
    rep(i,0,102)rep(j,0,102)m[i][j]=-1;
    rep(i,0,n){
      int c,d,x,y;
      cin >>c>>d>>x>>y;
      x++;
      y++;
      if(d==0){
        rep(j,x,x+4)rep(k,y,y+2)m[j][k]=c;
      }else{
        rep(j,x,x+2)rep(k,y,y+4)m[j][k]=c;
      }
    }
    string ans[2]={"NG","OK"};
    if(m[xs][ys]==-1)cout <<"NG"<<endl;
    else cout << ans[DFS(xs,ys,m[xs][ys])]<<endl;
    cin >> w>>h;
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
int w,h,xs,ys,xg,yg,n,c,d,x,y,nok,sc;
int fld[101][101];
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
void dfs(int fx,int fy){
    if(fld[fy][fx]!=sc||fx<0||fx>w-1||fy<0||fy>h-1)return;
    if(fx==xg-1&&fy==yg-1){
        if(sc==fld[fy][fx])nok=1;
    }
    fld[fy][fx]=0;
    for(int i=0;i<4;i++){
        dfs(fx+dx[i],fy+dy[i]);
    }
}
signed main(){
    for(;;){
        cin>>w>>h;
        if(w==0&&h==0)break;
        cin>>xs>>ys;
        cin>>xg>>yg;
        cin>>n;
        nok=0;
        fill(fld[0],fld[0]+101*101,0);
        for(int i=0;i<n;i++){
            cin>>c>>d>>x>>y;
            x--,y--;
            if(d==0){
                for(int j=0;j<2;j++){
                    for(int k=0;k<4;k++){
                        fld[y+j][x+k]=c;
                    }
                }
            }
            if(d==1){
                for(int j=0;j<4;j++){
                    for(int k=0;k<2;k++){
                        fld[y+j][x+k]=c;
                    }
                }
            }
        }
        sc=fld[ys-1][xs-1];
        /*for(int j=0;j<h;j++){
            for(int k=0;k<w;k++){
                cout<<fld[j][k];
            }
            cout<<endl;
        }*/
        if(fld[0][0]!=0){
            dfs(0,0);
        }
        if(nok==1)cout<<"OK"<<endl;
        else cout<<"NG"<<endl;
    }
    return 0;
}

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

using namespace std ;

int mx[] = { 1 ,0 ,0 ,-1 } ;
int my[] = { 0 ,1 ,-1 ,0 } ;

int w ,h ;
int gx ,gy ;

int ok_or_ng( int x ,int y ,bool came[][ 110 ] )
{
	came[ y ][ x ] = false ;

	if( x == gx && y == gy )
		return 1 ;

	for( int i = 0 ; i < 4 ; i++ )
	{
		int xx = x + mx[ i ] ;
		int yy = y + my[ i ] ;
		if( xx > 0 && xx <= w && yy > 0 && yy <= h && came[ yy ][ xx ] )
			return ok_or_ng( xx ,yy ,came ) ;
	}
	return 0 ;
}

void wide( int x ,int y ,int c ,int map[][ 110 ] )
{

	for( int i = 0 ; i < 2 ; i++ )
	{
		for( int j = 0 ; j < 4 ; j++ )
			map[ y + i ][ x + j ] = c ;
	}

	return ;
}

void high( int x ,int y ,int c ,int map[][ 110 ] )
{

	for( int i = 0 ; i < 4 ; i++ )
	{
		for( int j = 0 ; j < 2 ; j++ )
			map[ y + i ][ x + j ] = c ;
	}

	return ;
}

int main()
{
	while( cin >> w >> h )
	{
		if( w == h && w == 0 )
		return 0 ;
		bool came[ 110 ][ 110 ] = { false } ;
		int map[ 110 ][ 110 ] = { 0 } ;

		int sx ,sy ;

		cin >> sx >> sy ;
		cin >> gx >> gy ;

		int n ;
		cin >> n ;					// 0 w	1 h

		int c ,d ,x, y ;
		for( int i = 0 ; i < n ; i++ )
		{
			cin >> c >> d >> x >> y ;
			d ? high( x ,y ,c ,map ) : wide( x ,y ,c ,map ) ;
		}

		c = map[ gy ][ gx ] ;

		for( int i = 1 ; i <= h ; i++ )
		{
			for( int j = 1 ; j <= w ; j++ )
			{
				if( map[ i ][ j ] == c )
					came[ i ][ j ] = true ;
			}
		}

		ok_or_ng( sx ,sy ,came ) ? printf( "OK\n" ) : printf( "NG\n" ) ;
	}
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
int field[128][128];
int dx[] = {-1,0,0,1},dy[] = {0,-1,1,0};
int w,h;
void func(int x,int y,int c){
	if(x < 0 || y < 0 || x >= w || y >= h)return ;
	if(field[y][x] == -1 || field[y][x] == 0)return ;
	if(field[y][x] != c)return ;
	field[y][x] = -1;
	for(int i = 0; i < 4; i++){
		func(x+dx[i],y+dy[i],c);
	}
}
int main(){
	while(scanf("%d%d",&w,&h),w,h){
		int xs,ys,xg,yg,n;
		memset(field,0,sizeof(field));
		scanf("%d%d%d%d%d",&xs,&ys,&xg,&yg,&n);
		for(int i = 0; i < n; i++){
			int c,d,x,y;
			scanf("%d%d%d%d",&c,&d,&x,&y);
			if(d == 0){
				for(int j = x; j < x + 4; j++){
					field[y][j] = c;
					field[y+1][j] = c;
				}
			}
			else{
				for(int j = y; j < y + 4; j++){
					field[j][x] = c;
					field[j][x+1] = c;
				}
			}
		}
		func(xs,ys,field[ys][xs]);
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
				printf("%2d",field[i][j]);
			}
			puts("");
		}
		cout << (field[yg][xg]==-1?"OK":"NG") << endl;
	}
	return 0;
}