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 main(){
	int w, h, xs, ys, xg, yg, n, c, d, x, y, color;
	vector< vector< int > > field;
	queue< complex< int > > que;
	bool exist;
	
	while(cin >> w >> h, w){
		cin >> xs >> ys >> xg >> yg >> n;
		
		field.clear();
		field.resize(w);
		for(int i = 0; i < w; ++i) field[i].resize(h);
		
		for(int i = 0; i < n; ++i){
			cin >> c >> d >> x >> y;
			
			if(d == 0){
				for(int j = 0; j < 4; ++j){
					for(int k = 0; k < 2; ++k){
						field[x + j - 1][y + k - 1] = c;
					}
				}
			}else{
				for(int j = 0; j < 2; ++j){
					for(int k = 0; k < 4; ++k){
						field[x + j - 1][y + k - 1] = c;
					}
				}
			}
		}
		
		exist = false;
		if(field[xs - 1][ys - 1] != 0 && field[xg - 1][yg - 1] != 0 && field[xs - 1][ys - 1] == field[xg - 1][yg - 1]){
			que.push(complex< int >(xs - 1, ys - 1));
			color = field[xs - 1][ys - 1];
			
			while(!que.empty()){
				complex< int > pos = que.front();
				que.pop();
				
				if(pos.real() == xg - 1 && pos.imag() == yg - 1){
					exist = true;
					break;
				}
				
				if(pos.real() > 0 && field[pos.real() - 1][pos.imag()] == color) que.push(pos + complex< int >(-1, 0));
				if(pos.real() < w - 1 && field[pos.real() + 1][pos.imag()] == color) que.push(pos + complex< int >(1, 0));
				if(pos.imag() > 0 && field[pos.real()][pos.imag() - 1] == color) que.push(pos + complex< int >(0, -1));
				if(pos.imag() < h - 1 && field[pos.real()][pos.imag() + 1] == color) que.push(pos + complex< int >(0, 1));
				
				field[pos.real()][pos.imag()] = 0;
			}
		}
		
		cout << (exist ? "OK" : "NG") << endl;
		
	}
	
	return 0;
}

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

int a[1000][1000];
int xg,yg,w,h,ans;

void solve(int xs,int ys){

  if(xs == xg && ys == yg){
    ans = 1;
    return;
  }

  if(a[ys][xs] != a[yg][xg]) return;
  a[ys][xs] = 100;

  if(xs+1 <= w) solve(xs+1,ys);
  if(ys+1 <= h) solve(xs,ys+1);
  if(xs-1 > 0) solve(xs-1,ys);
  if(ys-1 > 0) solve(xs,ys-1);

}

int main(){

  int xs,ys;
  int n;

  while(1){
    cin >> w >> h;
    if(!w && !h) break;
    cin >> xs >> ys >> xg >> yg >> n;

    memset(a,0,sizeof(a));
    ans = 0;

    int c,d,x,y;

    for(int i=0;i<n;i++){
      cin >> c >> d >> x >> y;

      if(d == 0){
	for(int j=0;j<4;j++){
	  a[y][x+j] = c;
	  a[y+1][x+j] = c;
	}
      }

      else if(d == 1){
	for(int j=0;j<4;j++){
	  a[y+j][x] = c;
	  a[y+j][x+1] =c ;
	}
      }
    }

    solve(xs,ys);

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

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int sx,sy;
int gx,gy;
vvi tmp;
int q;
int dx[]={0,0,1,-1};
int dy[]={-1,1,0,0};
void solve(int x,int y){
	tmp[x][y]=-1;
	rep(i,4){
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(tmp[nx][ny]==q){solve(nx,ny);}
	}
}
int main(){
	int n,m;
	while(cin>>n>>m,n+m){
		vvi in(n+2,vi(m+2));
		cin>>sx>>sy>>gx>>gy;
		cin>>n;
		while(n--){
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			if(!b)rep(i,4)rep(j,2)in[c+i][d+j]=a;
			else rep(i,2)rep(j,4)in[c+i][d+j]=a;
		}
		tmp=in;
		//rep(i,in.size()){rep(j,in[0].size())cout<<in[i][j]<<" ";cout<<endl;}
		if(in[sx][sy]){
			q=in[sx][sy];
			solve(sx,sy);
		}
		//cout<<endl;
		//rep(i,tmp.size()){rep(j,tmp[0].size())cout<<tmp[i][j]<<" ";cout<<endl;}
		cout<<((tmp[gx][gy]==-1)?"OK":"NG")<<endl;
		//rep(i,22){rep(j,22)cout<<tmp[i][j]<<" ";cout<<endl;}
	}
}