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 F[102][102];

void setblock(int c, int d, int x, int y){
  if(d==0){
    for(int i=0;i<4;i++){
      F[y][x+i]=c;
      F[y+1][x+i]=c;
    }
  }else{
    for(int i=0;i<4;i++){
      F[y+i][x]=c;
      F[y+i][x+1]=c;
    }
  }

}

void DFS(int Y, int X, int c){
  if(F[Y][X]!=c)
    return;

  F[Y][X] = 6;

  DFS(Y-1, X  ,c);
  DFS(Y  , X+1,c);
  DFS(Y+1, X  ,c);
  DFS(Y  , X-1,c);
}

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

  DFS(ys,xs,F[ys][xs]);
  if(F[yg][xg]==6){
    cout << "OK" << endl;
  }else{
    cout << "NG" << endl;
  }
}
  return 0;
}

C++: #include <iostream>

using namespace std;

int B[102][102], C, xg, yg;

bool rec(int x, int y) {
  if (B[x][y] != C)
    return false;
  if (x == xg && y == yg)
    return true;
  B[x][y] = 0;
  if (rec(x+1, y) || rec(x-1, y) || rec(x, y+1) || rec(x, y-1))
    return true;
  return false;
}

int main() {
  for (;;) {
    int w, h;
    cin >> w >> h;
    if (!w && !h) return 0;
    for (int x = 0; x < w+2; x++)
      for (int y = 0; y < h+2; y++)
        B[x][y] = 0;
    int xs, ys, n;
    cin >> xs >> ys >> xg >> yg >> n;
    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      for (int j = 0; j < 2; j++)
        for (int k = 0; k < 4; k++)
          (d ? B[x+j][y+k] : B[x+k][y+j]) = c;
    }
    if (!B[xs][ys]) {
      cout << "NG" << endl;
      continue;
    }
    C = B[xs][ys];
    cout << (rec(xs, ys) ? "OK" : "NG") << endl;
  }
}

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

typedef pair<int, int> P;


int w, h;
int xs, ys, xg, yg, n, c, d, x, y;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

void put_b(int a[][101]) {
  int X, Y;
  if(d == 0) X = 4, Y = 2;
  else X = 2, Y = 4 ;
  for(int i = 0; i < Y; i++)
    for(int j = 0; j < X; j++)
      a[y + i][x + j] = c;
}

bool bfs(int a[][101]) {
  int cs = a[ys][xs];
  if(cs != a[yg][xg]) return false;
  queue<P> que;
  que.push(P(xs, ys));
  while(que.size()) {
    P p = que.front();
    que.pop();
    if(p.first == xg && p.second == yg) return true;
    for(int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second +dy[i];
      if(0 <= nx && nx < w && 0 <= ny && ny < h && a[ny][nx] == cs) {
	que.push(P(nx, ny));
	a[ny][nx] = 0;
      }
    }
  }
  return false;
}
int main() {
  while(cin >> w >> h, w != 0 || h != 0) {
    int a[101][101] = {};
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;
  
    while(n--) {
      cin >> c >> d >> x >> y;
      put_b(a);
    }
    /*
      for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++)
      cout << a[i][j];
      cout << endl;
      }*/
    if(bfs(a) == true) cout << "OK" << endl;
    else cout << "NG" << endl;
  }
  return 0;
}

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

int F[102][102];
int color,gx,gy;
bool maze;

void DFS1(int Y,int X){
    if(F[Y][X]!=color)return;
    if(Y==gy&&X==gx)maze=true;
    
    F[Y][X]=0;
    
    DFS1(Y-1,X  );
    DFS1(Y  ,X-1);
    DFS1(Y  ,X+1);
    DFS1(Y+1,X  );
}

int main(){
    int N,M,sx,sy,n;
    cin >> N >> M;
    while(N!=0&&M!=0){
        cin >> sx >> sy >> gx >> gy >> n;
        int c,d,nx,ny;
        for(int i=0;i<n;i++){
            cin >> c >> d >> nx >> ny;
            for(int y=ny;y<ny+2*(1+d);y++){
                for(int x=nx;x<nx+2*(2-d);x++){
                    F[y][x] = c;
                }
            }
        }

        color = F[sy][sx];
        maze = false;
        if(color!=0){
            DFS1(sy,sx);
        }
        cout << (maze?"OK":"NG") << endl;
        for(int y=0;y<102;y++){
            for(int x=0;x<102;x++){
                F[y][x] = 0;
            }
        }
        cin >> N >> M;
    }
    return 0; 
}

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


#define rep(i,n) for(short i = 0; i < n; i++)
#define MAX 50

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

typedef pair<short,short> P;
P S,G;
short w,h;

short color;
	queue<P> que;
	
bool check(short maze[MAX+1][MAX+1])
{
	que.push(S);

	color = maze[S.second][S.first];
	if(color < 0)
		return false;

	short nx,ny;
	while(que.size()){
		P p = que.front();	que.pop();

		if(p == G){
			while(que.size())
				que.pop();
			return true;
		}

		rep(i,4){
			nx = p.first + dx[i], ny = p.second + dy[i];
			
			if(nx > -1 && ny > -1 && nx < w && ny < h &&
				maze[ny][nx] == color){
					maze[ny][nx] = -1;
					que.push(P(nx,ny));
			}

		}
	}


	
	return false;
}

int main(void)
{
	short n,c,d,x,y;
	short maze[MAX+1][MAX+1];
	while(cin >> w >> h, w){
	/*	rep(i,MAX){
			rep(j,MAX)
				maze[i][j] = -10;
		}*/
		cin >> S.first >> S.second;
		cin >> G.first >> G.second;
		S.first = S.first / 2;
		S.second= S.second/ 2;
		G.first /= 2;
		G.second /= 2;


		cin >> n;

		rep(i,n){
			cin >> c >> d >> x >> y;
			x /= 2;
			y /= 2;
			x++;
			y++;

			if(d == 0)
				maze[y][x] = maze[y][x+1] = c;
			else
				maze[y][x] = maze[y+1][x] = c;
		}
		
		if(/*maze[S.second][S.first] > 0 && */maze[S.second][S.first] == maze[G.second][G.first] && check(maze))
			cout << "OK\n";
		else
			cout << "NG\n";

	}
	
	return 0;
}