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[105][105];
int xg,yg;


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

  F[Y][X] = -1;
  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,n;
  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0)
      break;
for(int i=0; i<20; i++){
      for(int j=0; j<20; j++){
        F[i][j]=0;
      }
    }
    
    cin >> xs >> ys >> xg >> yg >> n;


    for(int i=0;i<n;i++){
      int c,d,x,y;
      cin >> c >> d >> x >> y;
      if(d == 0){
        for(int j=0;j<4;j++){
          F[x+j][y] = c;
          F[x+j][y+1] = c;
        }
      } else {
        for(int j=0;j<4;j++){
          F[x][y+j] = c;
          F[x+1][y+j] = c;
        }
      }
    }
if(F[xs][ys]==0){
      cout << "NG" << endl;
      continue;
    }
  DFS(xs,ys,F[xs][ys]);

  if(F[xg][yg] == -1){
    cout << "OK" << endl;
  } else {
    cout << "NG" <<endl;
  }
  }
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#define INF (2000000000)
using namespace std;

typedef vector<int> VI;
typedef vector<int>::iterator VII;
typedef vector<double> VD;
typedef vector<double>::iterator VDI;

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

int map[200][200];
bool done[200][200];

int xg, yg;

void Search(int y, int x)
{
	done[y][x] = true;
	for (int i = 0; i < 4; i++){
		if (!done[y + dy[i]][x + dx[i]] && map[y][x] == map[y + dy[i]][x + dx[i]]){
			Search(y + dy[i], x + dx[i]);
		}
	}
}

int main()
{
	int w, h;
	while (scanf("%d %d", &w, &h), w + h){
		memset(map, 0, sizeof(map));
		memset(done, 0, sizeof(done));
		
		int xs, ys;
		scanf("%d %d %d %d", &xs, &ys, &xg, &yg);
		
		int n;
		scanf("%d", &n);
		
		for (int i = 0; i < n; i++){
			int c, d, x, y;
			scanf("%d %d %d %d", &c, &d, &x, &y);
			
			int a = 2, b = 4;
			if (d == 1){
				swap(a, b);
			}
			
			for (int j = y; j < y + a; j++){
				for (int k = x; k < x + b; k++){
					map[j][k] = c;
				}
			}
		}
		
		if (map[ys][xs]) Search(ys, xs);
		
		printf(done[yg][xg] ? "OK\n" : "NG\n");
	}
	
	return 0;
}

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

using namespace std;

void PutBlock(vector< vector<int> >&, int, int, int, int);
void CheckMaze1(vector< vector<int> >&, int&, int, int, int, int, int);
void CheckMaze2(vector< vector<int> >&, int&, int, int, int, int, int);

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

  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0) break;

    vector<int> _data(w, 0);
    vector< vector<int> > data(h, w);

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

    cin >> n;

    for(i=0; i<n; ++i){
      cin >> c >> d >> y >> x;
      PutBlock(data, c, d, x-1, y-1);
    }

    color = data[xs][ys];

    ans = 0;
    CheckMaze1(data, ans, color, xs, ys, xg, yg);

    if(ans == 1)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }

  return 0;
}

void PutBlock(vector< vector<int> >& data, int c, int d, int x, int y){
  int i, j;
  if(d == 0){
    for(i=x; i<x+2; ++i){
      for(j=y; j<y+4; ++j)
	data[i][j] = c;
    }
  }else{
    for(i=x; i<x+4; ++i){
      for(j=y; j<y+2; ++j)
	data[i][j] = c;
    }
  }
}

void CheckMaze1(vector< vector<int> >& data, int& ans, int color, int x, int y, int xg, int yg){
  data[x][y] = -1;
  if(x == xg && y == yg) ans = 1;

  CheckMaze2(data, ans, color, x-1, y, xg, yg);
  CheckMaze2(data, ans, color, x, y-1, xg, yg);
  CheckMaze2(data, ans, color, x+1, y, xg, yg);
  CheckMaze2(data, ans, color, x, y+1, xg, yg);
}

void CheckMaze2(vector< vector<int> >& data, int& ans, int color, int x, int y, int xg, int yg){
  if(x > -1 && x < data.size() && y > -1 && y < data[0].size() && data[x][y] == color)
    CheckMaze1(data, ans, color, x, y, xg, yg);
}

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

int weight, hight;
int g[101][101];
int sc;
int sx, sy, gx, gy;
bool flag = false;
const int vx[4] = {0, 0, -1, 1};
const int vy[4] = {1, -1, 0, 0};

void dfs(int x, int y){
    if(x == gx && y == gy){
        flag = true;
        return;
    }

    for(int i = 0;i < 4;i++){
        int nx = x + vx[i];
        int ny = y + vy[i];
        
        if(nx >= 0 && nx < weight && ny >= 0 && ny < hight && g[ny][nx] == sc){
            g[y][x] = -1;
            dfs(nx, ny);
        }
    }
    
    return;
}

int main(){ 
    while(1){
        cin >> weight >> hight;

        if(weight == 0 && hight == 0){
            break;
        }
        
        int dn, c, d, x, y;
        flag = false;
        g[101][101] = {};
        
        cin >> sx >> sy >> gx >> gy >> dn;
        
        for(int i = 0;i < dn;i++){
            cin >> c >> d >> x >> y;
            
            if(d == 0){
                for(int h = 0;h < 2;h++){
                    for(int w = 0;w < 4;w++){
                        g[y+h][x+w] = c;
                    }
                }
            }else{
                for(int h = 0;h < 4;h++){
                    for(int w = 0;w < 2;w++){
                        g[y+h][x+w] = c;
                    }
                }
            }
        }
        
        sc = g[sy][sx];
        dfs(sx, sy);
        
        if(flag){
            cout << "OK" << endl;
        }else{
            cout << "NG" << endl;
        }
    }
    
    return 0;
}