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 map[101][101];
int w,h; 
int color;
int xg,yg;

bool dfs(int y,int x) {
 
  if ( map[y][x] != color ) return 0;
  map[y][x] = 10;
  //if ( y-1 >= 1 ) dfs( y-1 , x );
  if ( y+1 <= h ) dfs( y+1 , x );
  //if ( x-1 >= 1 ) dfs( y , x-1 );
  if ( x+1 <= w ) dfs( y , x+1 );
  
  if ( map[yg][xg] == 10 ) return 1; 
  return 0;

}

int main() {
  while ( cin >> w >> h ,w+h ) {
    int xs,ys; cin >> xs >> ys;
    cin >> xg >> yg;
    int n; cin >> n;
    while ( n > 0 ) {
      n--;
      int c,d,x,y;
      cin >> c >> d >> x >> y;
      if ( d == 1 ) {

	for ( int i = y; i < y+4; i++ ) {
	  for ( int j = x; j < x+2; j++ ) map[i][j] = c;
	}
      
      } else if ( d == 0 ) {
	for ( int i = y; i < y+2; i++ ) {
	  for ( int j = x; j < x+4; j++ ) map[i][j] = c;
	}
      }
      
    }
    color = map[ys][xs];
    
    if ( dfs(ys,xs) ) cout << "OK" << endl;
    else cout << "NG" << endl;
    
  }

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool N[101][101];
int map[101][101],R,ga,gb,f=0,w,h;
int block(int ,int);
int main()
{
	int i,j,a,n,L,b,c,d,X=0,Y=0,x,y;
	cin>>w>>h;
	while(w!=0||h!=0){
		memset(map,0,sizeof(map));
		memset(N,0,sizeof(N));
		cin>>a>>b;
		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[b][a];
		if(block(a,b)) cout<<"OK"<<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;
}
int block(int b,int a)
{
	//if(f==1) return 1;
	N[b][a]=1;
	if(ga==a&&gb==b) return 1;
	if(a+1!=w){
		if(map[b][a+1]==R&&N[b][a+1]==0) return block(b,a+1);}
	if(b+1!=h){
		if(map[b+1][a]==R&&N[b+1][a]==0) return block(b+1,a);}
	if(a-1!=-1){
		if(map[b][a-1]==R&&N[b][a-1]==0) return block(b,a-1);}
	if(b-1!=-1){
		if(map[b-1][a]==R&&N[b-1][a]==0) return block(b-1,a);}
	return 0;
}

C++: #include<iostream>
#include<queue>
#define MAX 100
using namespace std;

int map[MAX+2][MAX+2];
bool check[MAX+2][MAX+2];
int dx[4] = { -1,1,0,0};
int dy[4] = { 0,0,-1,1};

int main () {

  while ( true ) {

    queue<int> next;

    while( !next.empty() ) {
      next.pop();
    }
    
    int w,h;
    cin >> w >> h ;

    if ( w == 0 )
      break;

    int xs,ys;
    cin >> xs >> ys;
    
    int xg, yg;
    cin >> xg >> yg;

    int n;
    cin >> n;

    int c,d,x,y;

    for ( int i=0; i<MAX+2; i++ ) {
      for ( int j=0; j<MAX+2; j++ ) {
	map[i][j] = 0;
	check[i][j] = true;
      }
    }

    for ( int i=0; i<n; i++ ) {
      cin >> c >> d >> x >> y;
      
      //int count = 0;
      for ( int j=0; j<2; j++ ) {
	for ( int k=0; k<4; k++ ) {
	  //cout << count;
	  if ( d == 0 ) {
	    map[y+j][x+k] = c;
	  } else {
	    map[y+k][x+j] = c;
	  }
	}
      }
      //cout << endl;
    }

    next.push(ys*1000 + xs );

    int point;
    bool goal = false;

    int sc = map[ys][xs];

    /*
      for ( int i=0; i<=h; i++ ) {
      for ( int j=0; j<=w; j++ ) {
      cout << map[i+1][j+1];
      }
      cout << endl;
      }
      cout << endl;
    */
    int xn,yn;
    if ( map[yg][xg] == sc ) {
      if ( sc == 0 || map[yg][xg] == 0 ) {
      }
      else {
	while ( !next.empty() ) {
	  point = next.front();
	  next.pop();
	  xn = point%1000;
	  yn = point/1000;

	  if (map[yn][xn] == 0 )
	    continue;

	  if (point == yg*1000+xg) {
	    goal = true;
	    break;
	  }

	  for ( int i=0; i<4; i++ ) {
	    if ( check[yn+dy[i]][xn+dx[i]] ) {
	      check[yn+dy[i]][xn+dx[i]] = false;
	      if ( map[yn+dy[i]][xn+dx[i]] == sc ) {
		next.push((yn+dy[i])*1000+ xn +dx[i]);
	      }
	    }
	  }
	}
      }
    }

    if ( goal )
      cout << "OK\n";
    else
      cout << "NG\n";
  }
}

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

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int board[100][100];

void init_board() {
    int i, j;
    for (i=0; i<100; i++) {
        for (j=0; j<100; j++) {
            board[i][j] = 0;
        }
    }
}
void put_block(int c, int d, int x, int y) {
    int i, j;
    int i_stop = d? 2+x: 4+x;
    int j_stop = d? 4+y: 2+y;
    for (i=x; i<i_stop; i++) {
        for (j=y; j<j_stop; j++) {
            board[i][j] = c;
        }
    }
}

string trace(int xs, int ys, int xg, int yg) {
    int x, y, nx, ny, i, c;
    queue<pair<int, int> > q;
    q.push(make_pair(xs, ys));
    c = board[xs][ys];
    if (c == 0) {
        return "NG";
    }
    board[xs][ys] = 0;
    while (q.size() > 0) {
        x = q.front().first;
        y = q.front().second;
        q.pop();
        if (x==xg && y==yg) {
            return "OK";
        }
        for (i=0; i<4; i++) {
            nx = x+dx[i];
            ny = y+dy[i];
            if (nx<0 || ny<0 || nx>=100 || ny>=100) {
                continue;
            }
            if (board[nx][ny] == c) {
                q.push(make_pair(nx, ny));
                board[nx][ny] = 0;
            }
        }
    }
    return "NG";
}

int main() {
    int w, h, xs, ys, xg, yg, n, c, d, x, y, i;
    while (true) {
        cin >> w >> h;
        if (w==0 && h==0) {
            break;
        }
        cin >> xs >> ys;
        cin >> xg >> yg;
        cin >> n;
        init_board();
        for (i=0; i<n; i++) {
            cin >> c >> d >> x >> y;
            put_block(c, d, x, y);
        }
        cout << trace(xs, ys, xg, yg) << endl;
    }

    return 0;
}