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 Array[200][200];
bool is_goal = false;
#define START 20
#define GOAL 30

void DFS(int h,int w,int c){

  if(Array[h][w] = START){
    DFS(h-1,w,Array[h-1][w]);
    DFS(h,w+1,Array[h][w+1]);
    DFS(h+1,w,Array[h+1][w]);
    DFS(h,w-1,Array[h][w-1]);
  }

  if(!is_goal){

    if(Array[h][w] == GOAL){
      is_goal = true;
      return;
    }

    if(Array[h][w] != c || Array[h][w] == '.'){
      return;
    }

    char tmp=Array[h][w];

    Array[h][w] = '.';

    DFS(h-1,w,tmp);
    DFS(h,w+1,tmp);
    DFS(h+1,w,tmp);
    DFS(h,w-1,tmp);
  }

}

int main(){

    while(1){
      int H,W;
      int Start[2],Goal[2];
      int BlockNum;
      int color,dir,xpos,ypos;

      cin >> H >> W;

      if(H == 0 && W == 0){
        break;
      }

      cin >> Start[0] >> Start[1];
      Array[Start[0]][Start[1]] = START;

      cin >> Goal[0] >> Goal[1];
      Array[Goal[0]][Goal[1]] = GOAL;

      cin >> BlockNum;

      for(int i=0;i<BlockNum;i++){
        cin >> color >> dir >> xpos >> ypos;
        for(int i=0;i<4;i++){
          for(int j=0;j<2;j++){
            if(!dir){//?¨?
              Array[xpos+i][ypos+j] = color;
            }
            else{
              Array[xpos+j][ypos+i] = color;
            }
          }
        }
      }
      DFS(Start[0],Start[1],Array[Start[0]][Start[1]]);

      if(is_goal){
        cout << "OK" << endl;
      }
      else{
        cout << "NG" << endl;
      }
  }
    return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;

struct P {
	int x, y;
};

int main() {
	int W, H;
	while ( cin >> W >> H, W || H ) {
		int sx, sy; cin >> sx >> sy; --sx, --sy;
		int gx, gy; cin >> gx >> gy; --gx, --gy;
		int n; cin >> n;
		int m[5][100][100] = {0};
		for (int i = 0; i < n; ++i) {
			int c, d, X, Y; cin >> c >> d >> X >> Y; --X, --Y, --c;
			int w, h;
			if (d == 0) w = 4, h = 2;
			else w = 2, h = 4;
			for (int y = 0; y < h; ++y) {
				for (int x = 0; x < w; ++x) {
					m[c][Y+y][X+x] = 1;
				}
			}
		}
		int c = -1;
		for (int i = 0; i < 5; ++i) {
			if (m[i][gy][gx]) c = i;
		}
		bool ans = false;
		if (c > -1) {
			queue<P> Q; Q.push( (P){sx, sy} );
			while ( !Q.empty() ) {
				P p = Q.front(); Q.pop();
				if (p.x < 0 || p.x >= W || p.y < 0 || p.y >= H) continue;
				if (!m[c][p.y][p.x]) continue;
				m[c][p.y][p.x] = 0;

				if (p.x == gx && p.y == gy) {
					ans = true;
					break;
				}

				Q.push( (P){p.x+1, p.y} );
				Q.push( (P){p.x-1, p.y} );
				Q.push( (P){p.x, p.y+1} );
				Q.push( (P){p.x, p.y-1} );
			}
		}
		if (ans) cout << "OK" << endl;
		else cout << "NG" << endl;
	}
}

C++: #include <cstdio>
#include <cstring>

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

int dfs(int x,int y,int cl) {
	if(x==xg-1&&y==yg-1) return 1;
	for(int i=0;i<4;i++) {
		int nx=x+dx[i],ny=y+dy[i];
		if(0<=nx&&nx<w&&0<=ny&&ny<h&&map[nx][ny]==cl) {
			if(dfs(nx,ny,cl)) return 1;
		}
	}
	return 0;
}

int main() {
	while(1) {
		memset(map,0,sizeof(map));
		scanf("%d %d",&w,&h);
		if(w==0&&h==0) break;
		scanf("%d %d",&xs,&ys);
		scanf("%d %d",&xg,&yg);
		scanf("%d",&n);
		for(int i=0;i<n;i++) {
			scanf("%d %d %d %d",&c,&d,&x,&y);
			int dx,dy;
			if(d) dx=2,dy=4; else dx=4,dy=2;
			for(int i=0;i<dx;i++) {
				for(int j=0;j<dy;j++) {
					map[x-1+i][y-1+j]=c;
				}
			}
		}
		if(map[xs-1][ys-1]!=map[xg-1][yg-1]) {
			printf("NG\n");
		}else {
			printf("%s\n",dfs(xs-1,ys-1,c)?"OK":"NG");
		}
	}
}

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

int F[102][102];
int xg,yg,ok;

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

	

	if(X==xg&&Y==yg){
		cout << "OK"<<endl;
		ok++;
	}

	F[X][Y]='.';

	Maze(X-1,Y  ,C);
	Maze(X  ,Y+1,C);
	Maze(X+1,Y  ,C);
	Maze(X  ,Y-1,C);
	
}



int main(){	
	while(1){
		ok=0;

		int w,h,xs,ys,n,c,d,x,y;

		cin >>w>>h;
		if(w==0&&h==0)
			return 0;

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


		for(int i=1; i<=w; i++)
			for(int j=1; j<=h; j++)
				F[j][i]=0;

		for(int i=1; i<=w;i++){
				F[w+1][i]=6;
				F[0][i]=6;
		}
		for(int i=1; i<=h;i++){
				F[i][h+1]=6;
				F[i][0]=6;
		}

		int N=0;
		while(N<n){
			cin >>c>>d>>x>>y;

			if(d){
				for(int i=x; i<x+2; i++)
					for(int j=y; j<y+4; j++){
						F[i][j]=c;
				}
			}else{
				for(int i=x; i<x+4; i++)
					for(int j=y; j<y+2; j++){
						F[i][j]=c;
					}
			}
			N++;
		}

/*for(int i=0; i<=h+1; i++){
	for(int j=0; j<=w+1; j++){
		cout <<F[j][i];
		}
	cout << endl;
}*/
		
		Maze(xs,ys,F[xs][ys]);

		if(ok==0)
			cout <<"NG"<<endl;

	}

}