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;
queue <pair<int,int> > blocks;
int w,h,xs,ys,n,xg,yg,c,d,x,y,area[105][105];
bool flag=false;
void solve(int x,int y,int c);

int main(){
    while(1){
        cin >> w >> h;
        if(w==0&&h==0)break;
        cin >> xs >> ys >> xg >> yg >> n;
        for(int i=0;i<n;i++){
            cin >> c >> d >> x >> y;
            if(d==0){
            for(int j=y;j<y+2;j++){
                for(int k=x;k<x+4;k++){
                    area[k][j]=c;
                }
            }
            }else{
            for(int j=y;j<y+4;j++){
                for(int k=x;k<x+2;k++){
                    area[k][j]=c;
                    }
                }
            }
        }
        if(area[xs][ys]!=area[xg][yg]);
        else {
        blocks.push(make_pair(xs,ys));
        solve(blocks.front().first,blocks.front().second,area[xs][ys]);
        }
        if(flag==true)cout << "OK" << endl;
        else cout << "NG" << endl;
        flag=false;
        fill(area[0],area[104],0);
    }

return 0;
}

void solve(int x,int y,int c){
    if(!blocks.empty()){
    blocks.pop();
    if(x==xg&&y==yg){
        flag=true;
        while(!blocks.empty())blocks.pop();
    }else{
        area[x][y]=0;
        if(area[x-1][y]==c)blocks.push(make_pair(x-1,y));
        if(area[x][y-1]==c)blocks.push(make_pair(x,y-1));
        if(area[x+1][y]==c)blocks.push(make_pair(x+1,y));
        if(area[x][y+1]==c)blocks.push(make_pair(x,y+1));
        solve(blocks.front().first,blocks.front().second,c);
        }
    }
}

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



typedef vector<vector<int> > Matrix;
int w,h,xs,ys,xg,yg,n,color;
string res = "NG";
void write(Matrix &board,int c, int d, int x, int y){
  int width,length;
  if(d == 0){ width = 4; length =2; }
  if(d == 1){ width = 2; length =4; }
  for(int i=0; i<width; ++i){
    for(int j=0; j<length; ++j)
      board[x+i][y+j]=c;
  }
}
void func(Matrix &board,int nx,int ny){
  if(board[nx][ny] == 0){
    res = "NG";
    return;
  }
  if( nx==xg && ny==yg){
    res = "OK";
    return;
  }
  board[nx][ny] = 0;
  if( 0<=nx-1 && color==board[nx-1][ny])
    func(board,nx-1,ny);
  if( 0<=ny-1 && color==board[nx][ny-1])
    func(board,nx,ny-1);
  if( nx+1<=w && color==board[nx+1][ny])
    func(board,nx+1,ny);
  if( ny+1<=h && color==board[nx][ny+1])
    func(board,nx,ny+1);
}


int main(void){
  int c,d,x,y;
  while(1){
    cin >> w >> h;
    if( w==0 && h==0) return 0;
    cin >> xs >> ys >> xg >> yg >> n;
    xs--; ys--; xg--; yg--;
    Matrix board(w, vector<int>(h,0));
    for(int i=0; i<n; ++i){
      cin >> c >> d >> x >> y;
      --x; --y;
      write(board,c,d,x,y);
    }
    color = board[xs][ys];
    func(board,xs,ys);
    cout << res << endl;
    board.clear();
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
#include <sstream>
#include <cstdio>
#include <complex>
#include <cstring>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

int m[100][100];
int sx,sy,gx,gy;
int w,h;
bool dfs(int x,int y,int c){
  if(x==gx&&y==gy)return true;
  if(m[y][x] == 0)return false;
  m[y][x] = 0;
  //cout<<x<<" "<<y<<" "<<c<<endl;
  int dx[] = {0,0,-1,1}, dy[] = {1,-1,0,0};
  bool judge=false;
  rep(i,4){
    if(m[y+dy[i]][x+dx[i]] == c)
      judge |= dfs(x+dx[i],y+dy[i],c);
  }
  return judge;
}


int main(void){
  while(cin>>h>>w,h+w){
    rep(i,h)rep(j,w)m[i][j]=0;
  
    cin>>sx>>sy>>gx>>gy;
 
    int n,c,d,x,y;
    cin>>n;
    rep(i,n){
      cin>>c>>d>>x>>y;
      //cout<<c<<" "<<d<<" "<<x<<" "<<y<<endl;
      if(d){
	rep(i,4)rep(j,2)m[i+y][j+x] = c;
      }else{
	//cout<<x<<" "<<y<<endl;
	rep(i,2)rep(j,4)m[i+y][j+x] = c;      
      }
    }
    //m[sy][sx] = 8; m[gy][gx] = 9;
    //cout<<"  ";rep(i,w)cout<<i%10<<" ";cout<<endl;
    //rep(i,h){cout<<i%10<<" ";rep(j,w)cout<<m[i][j]<<" ";cout<<endl;}
    cout<<(dfs(sx,sy,m[sy][sx])?"OK":"NG")<<endl;
  }
}

C++: #include <cstdio>
#include <cstring>
int block[100][100];
int visited[100][100];
bool memo[100][100];
int w, h, xs, ys, xg, yg, n;
int d[2][4]={{0,0,-1,1},{-1,1,0,0}};
bool dfs(int x, int y) {
    if (visited[y][x]) return memo[y][x];
    if (x==xg-1&&y==yg-1) return true;
    visited[y][x]=1;
    for (int i=0; i<4; i++) {
        int nx=x+d[0][i], ny=y+d[1][i];
        if (nx>=0&&nx<w&&ny>=0&&ny<h&&block[y][x]==block[ny][nx]) {
            if (dfs(nx,ny)) {
                memo[y][x]=true;
                return true;
            }
        }
    }
    memo[y][x]=false;
    return false;
}
int main() {
    while (scanf("%d%d",&w,&h)) {
        if (!w&&!h) break;
        memset(block, 0, sizeof(block));
        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) {
                for (int j=y-1; j<y+3; j++) {
                    for (int k=x-1; k<x+1; k++) {
                        block[j][k]=c;
                    }
                }
            } else {
                for (int j=x-1; j<x+3; j++) {
                    for (int k=y-1; k<y+1; k++) {
                        block[k][j]=c;
                    }
                }
            }
        }
        /*for (int i=0; i<h; i++) for (int j=0; j<w; j++) {
            printf("%d%c",block[i][j],j==w-1?'\n':' ');
        }*/
        if (dfs(xs,ys)) puts("OK");
        else puts("NG");
    }
}