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 < 4; 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":"NG")<<endl;
	}
}

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

int xg , yg;
int w, h;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int sc;
bool b;

void dfs(int board[][102] ,int x , int y){
	if(x==xg&&y==yg) {b=true;return;}
	for(int i=0;i<4;++i){
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(tx<=0||ty<=0||tx>w||ty>h) continue;
		if(board[tx][ty]!=sc || board[tx][ty]==0) continue;
		if(b) continue;
		dfs( board , tx,ty);
	}
}

int main(){
	while(cin >> w >> h , w||h){
		int xs , ys;
		int n;
		b=false;
		int board[102][102];
		memset(board,0,sizeof(board));
		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){
				for(int k=0;k<2;++k){
					for(int j=0;j<4;++j){
						board[x+j][y+k]=c;
					}
				}
			} else {
				for(int k=0;k<4;++k){
					for(int j=0;j<2;++j){
						board[x+j][y+k]=c;
					}
				}
			}
		}
		
		sc=board[xs][ys];
		int gc=board[xg][yg];
		if(sc!=gc) {}
		else dfs(board ,xs,ys);
		cout << ( b ? "OK" : "NG") << endl;
	}
	return 0;
}

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

int w,h;
int xs,ys;
int xg,yg;
int n;
vector<int> c(200);
vector<int> d(200);
vector<int> x(200);
vector<int> y(200);
vector<vector<bool>> visited(200,vector<bool>(200,0));
vector<vector<int>> maze(200,vector<int>(200,0));

void dfs(int x,int y){

    if(visited[x][y]) return ;
    if(maze[xs][ys]!=maze[x][y]) return ;
 
    visited[x][y]=true;

    if(x<w) dfs(x+1,y);
    if(x>1) dfs(x-1,y);
    if(y<h)dfs(x,y+1);
    if(y>1)dfs(x,y-1);

}

int main(){
    while(true){
        cin>>w>>h;
        if(w==0&&h==0) break;
        cin>>xs>>ys;
        cin>>xg>>yg;
        cin>>n;
        
        c=vector<int>(200,0);
        d=vector<int>(200,0);
        x=vector<int>(200,0);
        y=vector<int>(200,0);
        visited=vector<vector<bool>>(200,vector<bool>(200,0));
       
        for(int i=0;i<n;i++){
            cin>>c[i]>>d[i]>>x[i]>>y[i];
            if(d[i]){
                for(int j=0;j<4;j++)
                for(int k=0;k<2;k++)
                maze[x[i]+k][y[i]+j]=c[i];
            }else{
                for(int j=0;j<2;j++)
                for(int k=0;k<4;k++)
                maze[x[i]+k][y[i]+j]=c[i];
            }
        }
        dfs(xs,ys);
        if(visited[xg][yg]&&maze[xs][ys]) cout<<"OK"<<endl;
        else cout<<"NG"<<endl;
        
    }

    return 0;
}