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;

struct board{
  int mass[102][102];
};

bool goal_flag;
void DFS(int x,int y,int gx,int gy,board b,int c){
    if(b.mass[y][x]!=c || b.mass[y][x]==0)return;
    b.mass[y][x]=0;
    if(x==gx&&y==gy){goal_flag=true;return;}
    DFS(x+1,y,gx,gy,b,c);
    DFS(x-1,y,gx,gy,b,c);
    DFS(x,y+1,gx,gy,b,c);
    DFS(x,y-1,gx,gy,b,c);
}

int main(){
  int h,w;
  int start_x,start_y;
  int goal_x,goal_y;
  int n;
  int c,d,x,y;
  while(1){
    cin>>w>>h;
    if(w==0&&h==0)break;
    cin>>start_x>>start_y;
    cin>>goal_x>>goal_y;
    cin>>n;

    board b;
    for(int i=0;i<h+2;i++){
      for(int j=0;j<w+2;j++){
        b.mass[i][j]=0;
      }
    }

    for(int i=0;i<n;i++){
        cin>>c>>d>>x>>y;
        for(int j=0;j<2;j++){
          for(int k=0;k<4;k++){
            if(d==0){b.mass[y+j][x+k]=c;}
            else if(d==1){b.mass[y+k][x+j]=c;}
          }
        }
    }
    goal_flag=false;
    DFS(start_x,start_y,goal_x,goal_y,b,b.mass[start_y][start_x]);
    if(goal_flag)cout<<"OK";
    else cout<<"NG";
    cout<<endl;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

int mp[101][101];
int w, h;
int gx, gy;

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

bool dfs(int y, int x, int c)
{
	if (y < 1 || y > h || x < 1 || x > w || mp[y][x] != c) return false;

	if (y == gy && x == gx) return true;

	mp[y][x] = 0;

	bool res = false;

	for (int i = 0; i < 4; ++i) res = res || dfs(y + dy[i], x + dx[i], c);

	return res;
}

int main()
{
	while (cin >> w >> h, w || h) {
		int sx, sy;
		cin >> sx >> sy >> gx >> gy;
		int n; cin >> n;
		while (n--) {
			int c, d, x, y; cin >> c >> d >> x >> y;
			if (d) {
				for (int i = 0; i < 4; ++i) for (int j = 0; j < 2; ++j) mp[y+i][x+j] = c;
			} else {
				for (int i = 0; i < 2; ++i) for (int j = 0; j < 4; ++j) mp[y+i][x+j] = c;
			}
		}

		cout << (dfs(sy, sx, mp[sy][sx]) ? "OK" : "NG") << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

typedef pair<int, int> P;
#define rep(i,a) for(int i=0;i<(a);++i)
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

const int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int w,h,sx,sy,gx,gy,n;
int board[102][102];

int main()
{
    while(cin >> w >> h && w) {
        memset(board, 0, sizeof(board));
        cin >> sx >> sy >> gx >> gy >> n;
        
        int c, d, x, y;
        rep(k, n) {
            cin >> c >> d >> x >> y;
            if(d == 0)
                rep(i, 2) rep(j, 4) 
                    board[i + y][j + x] = c;
             else 
                rep(i, 4) rep(j, 2) 
                    board[i + y][j + x] = c;
        }

        int e = board[sy][sx];
        queue<P> que;
        que.push(P(sx, sy));
        bool ok = false;
        while(!que.empty()) {
            P p = que.front(); que.pop();
            x = p.first; y = p.second;
            if(x == gx && y == gy) {
                ok = true;
                break;
            }
            if(board[y][x] != e) 
                continue;

            board[y][x] = -1;
            for(int k = 0; k < 4; k++) {
                int nx = x + dx[k];
                int ny = y + dy[k];
                if(nx >= 1 && nx <= w && ny >= 0 && ny <= h && board[ny][nx] == e) 
                    que.push(P(nx, ny));
            }
        }

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

C++: #include <bits/stdc++.h>
using namespace std;

struct UnionFindTree {
    vector<int> par;
    vector<int> rank;
    vector<int> siz;

    void init(int n) {
        par.resize(n);
        rank.resize(n);
        siz.resize(n);
        for (int i = 0; i < n; i++) {
            par[i] = i;
            rank[i] = 0;
            siz[i] = 1;
        }
    }
    int find(int x) {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) rank[x]++;
        par[y] = x;
        siz[x] += siz[y];
    }
    bool is_same(int x, int y) {
        return find(x) == find(y);
    }
    int size(int x) {
        x = find(x);
        return siz[x];
    }
};

int main(){
    int h, w;
    while(cin >> w >> h, h){
        int xs, ys, xg, yg;
        int n;
        cin >> xs >> ys >> xg >> yg >> n;
        xs--; ys--; xg--; yg--;
        vector<vector<int> > color(h, vector<int>(w,0));
        for(int i=0;i<n;i++){
            int c, d, x, y;
            cin >> c >> d >> x >> y;
            x--; y--;
            for(int dy=0;dy<(d?4:2);dy++){
                for(int dx=0;dx<(d?2:4);dx++){
                    color[y+dy][x+dx] = c;
                }
            }
        }
        UnionFindTree uf;
        uf.init(h*w);
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(color[i][j] == 0) continue;
                if(i+1<h && color[i][j] == color[i+1][j]){
                    uf.unite(i*w+j, (i+1)*w+j);
                }
                if(j+1<w && color[i][j] == color[i][j+1]){
                    uf.unite(i*w+j, i*w+j+1);
                }
            }
        }
        cout << (uf.is_same(ys*w+xs,yg*w+xg) ? "OK" : "NG") << endl;
    }

    return 0;
}