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 m[102][102];
int w, h, xs, ys, xg, yg, f, t;

void solve(int x, int y){
  if(x < 0 || y < 0 || x >= w || y >= h || f || m[x][y] == -1) return;
  if(x == xg && y == yg){
    f = 1;
    return;
  }
  m[x][y] = -1;
  if(m[x + 1][y] == t) solve(x + 1, y);
  if(m[x - 1][y] == t) solve(x - 1, y);
  if(m[x][y + 1] == t) solve(x, y + 1);
  if(m[x][y - 1] == t) solve(x, y - 1);
}

int main(void){
  int n, c, d, x, y;
  for( ; ; ){
    cin >> w >> h;
    if(!w && !h) break;

    for(int i = 0;i <= 101; i++)
      for(int j = 0;j <= 101; j++) m[i][j] = 0;

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

    for(int i = 0;i < n; i++){
      cin >> c >> d >> x >> y;
      x--; y--;
      if(!d){
        for(int i = 0;i < 4; i++) m[x + i][y] = m[x + i][y + 1] = c;
      } else {
        for(int i = 0;i < 4; i++) m[x][y + i] = m[x + 1][y + i] = c;
      }
    }

    f = 0;
    t = m[xs][ys];
    solve(xs, ys);

    cout << (f? "OK" : "NG") << endl;
  }
}

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

#define BLANK 0

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

bool dfs(int color, int x, int y, int gx, int gy, vector<vector<int> >& blocks){
  if(x == gx && y == gy){ return true; }
  blocks[x][y] = BLANK;
  for(int i = 0; i < 4; i++)
    if(blocks[x + dx[i]][y + dy[i]] == color)
      if(dfs(color, x + dx[i], y + dy[i], gx, gy, blocks))
        return true;
  return false;
}



int main(void){
  while(true){
    int w, h;
    cin >> w >> h;
    if(w == 0 && h == 0){ break; }

    int sx ,sy, gx, gy;
    cin >> sx >> sy;
    cin >> gx >> gy;

    int n;
    cin >> n;

    vector<vector<int> > blocks(w + 2, vector<int>(h + 2, BLANK));
    for(int i = 0; i < n; i++){
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      int now_w = (d == 0) ? 4 : 2;
      int now_h = (d == 0) ? 2 : 4;
      for(int j = 0; j < now_w; j++)
        for(int k = 0; k < now_h; k++)
          blocks[x + j][y + k] = c;
    }

    // ????????????
    if(blocks[sx][sy] == BLANK){
      cout << "NG" << endl;
      return 0;
    }
    
    if(dfs(blocks[sx][sy], sx, sy, gx, gy, blocks)){ cout << "OK" << endl; }
    else{ cout << "NG" << endl; }
  }

  return 0;
}

C++: #include<iostream>
#define rep(i,n) for(int i=0;i<n;i++)
#define ck(a,b) 0<=a&&a<b
using namespace std;
int dy[]={-1,0,1,0},dx[]={0,-1,0,1};
int w,h,xs,ys,xg,yg,n,c,d,x,y,f[100][100];
bool dfs(int y,int x){
	f[y][x]=0;
	if(y==yg&&x==xg)return 1;
	bool r=0;
	rep(d,4){
		int ny=y+dy[d],nx=x+dx[d];
		if(ck(ny,h)&&ck(nx,w)&&f[ny][nx]==c)r=r||dfs(ny,nx);
	}
	return r;
}
int main(){
	while(cin>>w>>h,w){
		rep(i,h)rep(j,w)f[i][j]=0;
		cin>>xs>>ys>>xg>>yg>>n;
		rep(i,n){
			cin>>c>>d>>x>>y;
			rep(j,(d?2:4))rep(k,(d?4:2))f[y+k][x+j]=c;
		}
		c=f[ys][xs];
		cout<<(c>0&&dfs(ys,xs)?"OK":"NG")<<endl;
	}
	return 0;
}

C++: #include <iostream>
int m[101][101];
int w,h,r,gx,gy;
void dfs(int x,int y,int c){
	if(!(x-gx)&&!(y-gy))r=1;
	if(x>0&&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>0&&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;
	//spÍX^[gn_ÌÀWAgpÍS[n_ÌÀW
	point sp,bp;
	
	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;
			d?bw=2,bh=4: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])dfs(sx,sy,m[sx][sy]),m[sx][sy] = 0;
		if(r)std::cout<<"OK"<<std::endl;
		else{std::cout<<"NG"<<std::endl;}
	}
	return 0;
}

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

int map[102][102];

void route(int h, int w, int color) {
	if (map[h][w] != color)
		return;
	map[h][w] = 0;
	route(h, w + 1, color);
	route(h + 1, w, color);
	route(h, w - 1, color);
	route(h - 1, w, color);
}


int main() {
	int h, w, xs, ys, xg, yg, n;
	int c, d, x, y,color;

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

		for (int i = 0; i<n; i++) {
			cin >> c >> d >> x >> y;
			if (d == 0) {
				for (int j = 0; j < 4; j++)
					map[j + x][y] = map[j + x][y+1] = c;
			}
			else {
				for (int j = 1; j <= 4; j++)
					map[x][y + j] = map[x + 1][y + j] = c;
			}
		}
		color = map[xs][ys];
		route(xs, ys, color);
		if (map[xg][yg]==0)
			cout << "OK" << endl;
		else
			cout << "NG";
	}

	return 0;
}