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>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

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

int w, h, sx, sy, gx, gy, n;
int c, d, x, y, nx, ny;

int dat[110][110];
bool used[110][110];

bool outOfRange(int x, int y){
	if(x > w || x < 1 || y > h || y < 1) return true;
	return false;
}


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

		queue<pair<int, pii> > q;
		pair<int, pii> p;
		q.push(mp(0, mp(sx, sy)));
		used[sx][sy] = true;
		bool ans = false;

		while(!q.empty()){
			p = q.front(); q.pop();
			c = p.fi;
			x = p.se.fi;
			y = p.se.se;
			if(x == gx && y == gy){
				ans = true;
				break;
			}
			for(int i = 0; i < n; i++){
				nx = x + dx[i];
				ny = y + dy[i];
				if(outOfRange(nx, ny) || used[nx][ny] == true) continue;
				if(dat[x][y]==dat[nx][ny]){
					q.push(mp(c+1, mp(nx, ny)));
					used[nx][ny] = true;
				}
			}
		}
		cout<<(ans?"OK":"NO")<<endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int fie[101][101],ans,gol[101][101],st[101][101],col,muki,bx,by;
int bex[4]={0,1,0,-1},bey[4]={1,0,-1,0},cou,a,b;
    int sx,sy,gx,gy,n;
int func(int y,int x){
    if(fie[sy][sx]==-1)return 0;
    if(y<1||x<1||y>b||x>a)return 0;
    if(fie[y][x]==ans){
        fie[y][x]=0;
        for(int i=0;i<4;i++){
            func(y+bey[i],x+bex[i]);
        }
    }
    return 0;
}
int main(){
    for(;;){
        cin>>a>>b;
        if(a==0&&b==0)break;
        fill(fie[0],fie[0]+101*101,-1);
        fill(vis[0],vis[0]+101*101,true);
        cin>>sx>>sy>>gx>>gy;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>col>>muki>>bx>>by;
            if(muki==0){
                for(int i=0;i<=1;i++){
                    for(int j=0;j<=3;j++){
                        fie[by+i][bx+j]=col;
                    }
                }
            }
            if(muki==1){
                for(int i=0;i<=3;i++){
                    for(int j=0;j<=1;j++){
                        fie[by+i][bx+j]=col;
                    }
                }
            }
        }
        ans=fie[sy][sx];
        if(fie[sy][sx]!=0){
            func(sy,sx);
        }
        /*for(int i=1;i<=a;i++){
            for(int j=1;j<=b;j++){
                cout<<fie[i][j];
            }
            cout<<endl;
        }*/
        if(!fie[gy][gx])cout<<"OK"<<endl;
        else cout<<"NG"<<endl;
        cou=0;
        /*for(int i=1;i<=a;i++){
            for(int j=1;j<=b;j++){
                cout<<fie[i][j];
            }
            cout<<endl;
        }*/
    }
return 0;
}
/*6 5
1 1
5 5
3
2 0 1 1
2 0 2 3
2 0 2 5
*/

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

int F[102][102];
int xg,yg;
int flag = 0;

void DFS(int Y,int X,int c){
  if(F[Y][X]!=c){
    return;
  }
  if(Y == xg && X == yg){
    flag = 1;
  }

  F[Y][X] = 0;
  DFS(Y-1,X  ,c);
  //DFS(Y-1,X+1,c);
  DFS(Y  ,X+1,c);
  //DFS(Y+1,X+1,c);
  DFS(Y+1,X  ,c);
  //DFS(Y+1,X-1,c);
  DFS(Y  ,X-1,c);
  //DFS(Y-1,X-1,c);
}


int main(){
  int w,h,xs,ys,n;
  cin >> w >> h >> 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;
      }
    }
  }

  DFS(xs,ys,F[xs][ys]);
  if(flag == 1){
    cout << "OK" << endl;
  } else {
    cout << "NG" <<endl;
  }

}