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>
int m[101][101],w,h,r,gx,gy;
void dfs(int x,int y,int c){
	if(!(x-gx)&&!(y-gy))r=1;
	if(x>1&&m[x-1][y]==c)m[x-1][y]=0,dfs(x-1,y,c);
	if(x<=w&&m[x+1][y]==c)m[x+1][y]=0,dfs(x+1,y,c);
	if(y>1&&m[x][y-1]==c)m[x][y-1]=0,dfs(x,y-1,c);
	if(y<=h&&m[x][y+1]==c)m[x][y+1]=0,dfs(x,y+1,c);
	return ;
}

int main(){
	//whÍ{[hÌå«³AnÍubNÌÂ
	int n,c,d,sx,sy,bx,by;
	
	while(std::cin>>w>>h,w&&h){
		for(int y=0;y<101;y++){
			for(int x=0;x<101;x++){
				m[x][y] = 0;
			}
		}
		r=0;
		std::cin>>sx>>sy;
		std::cin>>gx>>gy;
		std::cin>>n;
		for(int i=0;i<n;i++){
			std::cin>>c>>d>>bx>>by;
			int bw,bh;
			if(d)bw=2,bh=4;
			else{bh=2,bw=4;}
			for(int y=by;y<=by+bh;y++){
				for(int x=bx;x<=bx+bw;x++){
					m[x][y] = c;
				}
			}
		}
		if(m[sx][sy])c = m[sx][sy],m[sx][sy]=0,dfs(sx,sy,c);
		if(r)std::cout<<"OK"<<std::endl;
		else{std::cout<<"NG"<<std::endl;}
	}
	return 0;
}

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


int board[100][100], h, w, xs, ys, xg, yg, n;
bool mem[100][100];
int col;


bool dfs(int x, int y)
{
  bool b = false;
  if(x == xg && y == yg)
    return true;
  if(mem[x][y])
    return false;

  mem[x][y] = true;
  b = false;
  if(x < w - 1 && board[x + 1][y] == col)
  {
    b |= dfs(x + 1, y);
  }
  if(!b && x > 0 && board[x - 1][y] == col)
  {
    b |= dfs(x - 1, y);
  }
  if(!b && y < h - 1 && board[x][y + 1] == col)
  {
    b |= dfs(x, y + 1);
  }
  if(!b && y > 0 && board[x][y - 1] == col)
  {
    b |= dfs(x, y - 1);
  }
  return b;
}


int main()
{
  while(true)
  {
    cin >> w >> h;
    if(!w)
      break;
    cin >> xs >> ys >> xg >> yg >> n;
    xs--; ys--; xg--; yg--;
    for(int i = 0; i < n; i++)
    {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      x--; y--;
      board[x][y] = d;
      if(d == 0)
        board[x][y + 1] = c; 
      else
        board[x + 1][y] = c;

    }
    col = board[xs][ys];
    if(dfs(xs, ys))
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
}

C++: #include <iostream>

using namespace std;

int table[100][100];
int memo[100][100];

int w, h;
int xs, ys, xg, yg;
int n;

void solve(int xs, int ys, int c)
{
	if( memo[xs][ys] ) {
		return;
	}

	if( !( -1 < xs && xs < w && -1 < ys && ys < h && table[xs][ys] == c ) ) {
		memo[xs][ys] = 1;
		return;
	}
	
	if( xs == xg && ys == yg ) {
		memo[xs][ys] = 1;
		cout << "OK" << endl;
		throw "OK";
	}

	memo[xs][ys] = 1;	

	solve(xs+1, ys, c);
	solve(xs, ys+1, c);	
	solve(xs-1, ys, c);
	solve(xs, ys-1, c);

	return;
}

int main(int argc, char* argv[])
{
	while(1) {

		for( int i = 0; i < 100; i++ ) {
			for( int j = 0; j < 100; j++ ) {
				table[i][j] = 0;
				memo[i][j] = 0;
			}
		}		

		cin >> w >> h;

		if( w == 0 && h == 0 )
			break;
			
		cin >> xs >> ys;
		cin >> xg >> yg;
		cin >> n;

		for( int i = 0; i < n; i++ ) {

			int c; cin >> c;
			int d; cin >> d;
			int x, y; cin >> x >> y;

			if( d == 0 ) {

				table[x][y] = c;
				table[x][y+1] = c;

				table[x+1][y] = c;
				table[x+1][y+1] = c;

				table[x+2][y] = c;
				table[x+2][y+1] = c;

				table[x+3][y] = c;
				table[x+3][y+1] = c;																
				

			} else {

				table[x][y] = c;
				table[x+1][y] = c;

				table[x][y+1] = c;
				table[x+1][y+1] = c;

				table[x][y+2] = c;
				table[x+1][y+2] = c;
				
				table[x][y+3] = c;
				table[x+1][y+3] = c;								
			}
		}

		try {
			if( table[xs][ys] )
				solve(xs, ys, table[xs][ys]);
			cout << "NG" << endl;
		} catch(...) {
		}
	}

    return 0;
}

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){
	if(field[y][x] != col)
		return;
	if(x == gx && y == gy){
		flag = true;
		return;
	}
	DFS(x+1, y);
	if(flag)
		return;
	DFS(x, y+1);
	if(flag)
		return;
	DFS(x-1, y);
	if(flag)
		return;
	DFS(x, y-1);
}

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";
	}
}

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

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

void dfs(int x , int y ,int w , int h){
	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 || board[tx][ty]!=sc || board[tx][ty]==0 || b ) continue;
		dfs( tx , ty , w , h );
	}
}

int main(){
	int w , h;
	while(cin >> w >> h , w|h){
		int xs , ys , 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){
				for(int k=y;k<y+2;++k){
					for(int j=x;j<x+4;++j){
						board[j][k]=c;
					}
				}
			} else {
				for(int k=y;k<y+4;++k){
					for(int j=x;j<x+2;++j){
						board[j][k]=c;
					}
				}
			}
		}
		sc=board[xs][ys];
		int gc=board[xg][yg];
		if(sc!=gc) ;
		else  dfs( xs , ys , w , h );
		cout << ( b ? "OK" : "NG") << endl;
	}
	return 0;
}