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;

int w, h;
int gx, gy;
int vx[] = {0, 1, 0, -1};
int vy[] = {1, 0, -1, 0};
int block[100][100];
bool check[100][100];
bool flag;

void dfs(int y, int x) {

  if(y == gy && x == gx) {

    flag = true;
    return;

  } else {

    for(int i = 0; i < 4; ++i) {

      int ny = y + vy[i];
      int nx = x + vx[i];

      if(ny >= 0 && ny < h && nx >= 0 && nx < w && !check[ny][nx] && block[ny][nx] && block[ny][nx] == block[y][x]) {

        check[ny][nx] = true;
        dfs(ny, nx);

      }

    }

  }

}

int main() {

  int sx, sy;
  int n;
  int c, d, x, y;

  while(cin >> w >> h) {

    if(w == 0 && h == 0) break;

    cin >> sx >> sy;
    cin >> gx >> gy;
    cin >> n;

    --sx;
    --sy;
    --gx;
    --gy;

    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
        block[i][j] = 0;
        check[i][j] = false;
      }
    }

    for(int i = 0; i < n; ++i) {

        cin >> c >> d >> x >> y;

        --x;
        --y;

        int bw, bh;

        if(d) {
          bw = 2;
          bh = 4;
        } else {
          bw = 4;
          bh = 2;
        }

        for(int j = 0; j < bh; ++j) {
          for(int k = 0; k < bw; ++k) {
            block[y + j][x + k] = c;
          }
        }

    }

    flag = false;
    check[sy][sx] = true;
    dfs(sy, sx);

    cout << (flag ? "OK" : "NG") << endl;

  }

}

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

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<2;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;
  cin >> w >> h;
  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);
  }

  char S=F[ys][xs];
  DFS(xs,ys,F[xs][ys]);
  if(F[ys][xs]==6){
    cout << "OK" << endl;
  }else{
    cout << "NO" << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

bool isMaze;

void dfs(int blocks[105][105], int target, int x, int y, int gx, int gy){
    if(x == gx && y == gy){
        isMaze = true;
        return;
    }
    if(blocks[y][x] == target){
        blocks[y][x] = 0;
    }else{
        return;
    }
    dfs(blocks, target, x + 1, y, gx, gy);
    dfs(blocks, target, x - 1, y, gx, gy);
    dfs(blocks, target, x, y + 1, gx, gy);
    dfs(blocks, target, x, y - 1, gx, gy);
}

int main(){
    int w, h, xs, ys, xg, yg, n;
    while(cin >> w >> h, w||h){
        int blocks[105][105] = {0};
        cin >> xs >> ys >> xg >> yg >> n;
        rep(i,n){
            int c, d, x, y;
            cin >> c >> d >> x >> y;
            if(d == 0){
                rep(j,2) rep(k,4) blocks[y + j][x + k] = c;
            }else{
                rep(j,4) rep(k,2) blocks[y + j][x + k] = c;
            }
        }
        isMaze = false;
        if(blocks[ys][xs] != 0) dfs(blocks, blocks[ys][xs], xs, ys, xg, yg);

        if(isMaze) cout << "OK" << endl;
        else cout << "NG" << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <utility>
#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)
using namespace std;

typedef pair<int,int> pii;
int dx[]={0,0,1,-1}, dy[]={1,-1,0,0};
int sx,sy,gx,gy;
int w,h,n;
int g[1000][1000];
bool valid(int y, int x){ return 0<=x && x<w && 0<=y && y<h; }

int main(){
    while(cin>>w>>h && w){
        cin>>sx>>sy>>gx>>gy;
        sx--,sy--,gx--,gy--;
        cin>>n;
        rep(i,h)rep(j,w)g[i][j]=0;
        rep(i,n){
            int c,d,x,y;
            cin>>c>>d>>x>>y;
            x--,y--;
            g[y][x]=c;
            if(d==0)x++;else y++;
            g[y][x]=c;
        }
        int C=g[sy][sx];
        rep(i,h)rep(j,w)g[i][j]= (g[i][j]==C) ? C : 0;
        bool vis[1000][1000]={};
        vis[sy][sx]=true;
        if(C){
            stack<pii> s;
            vis[sy][sx]=true;
            s.emplace(sy,sx);
            while(s.size()){
                int y,x;
                tie(y,x) = s.top();s.pop();
                rep(d,4){
                    int nx=x+dx[d],ny=y+dy[d];
                    if(!valid(nx,ny))continue;
                    if(vis[ny][nx])continue;
                    vis[ny][nx]=true;
                    s.emplace(ny,nx);
                }
            }
        }
        puts(vis[gy][gx] ? "OK" : "NG");
    }
}