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 <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define INF 1010001000
#define loop(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
typedef pair<int, int> i_i;

bool dfs(vector<vector<int> > &g);

int sx, sy, gx, gy;
int w, h;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};

int main()
{

    while (cin >> w >> h, (w|h) ) {
        w++;
        h++;
        vector<vector<int> > graph(w, vector<int>(h, 0));
        cin >> sx >> sy >> gx >> gy;
        int blocks;
        cin >> blocks; 
        loop(i, blocks){
            int c, d, x, y;
            cin >> c >> d >> x >> y;
            if (d) {
                for (int j = x; j < x+2; j++) {
                    for (int k = y; k < y+4; k++) {
                        graph[j][k] = c;
                    }
                }
            } else {
                for (int j = x; j < x+4; j++) {
                    for (int k = y; k < y+2; k++) {
                        graph[j][k] = c;
                    }
                }
            }
        }
        cout << ((dfs(graph))?"OK":"NG") << endl;
    }
    return 0;
}

bool dfs(vector<vector<int> > &g)
{
    int cl = g[sx][sy];
    vector<vector<bool> > check(w, vector<bool>(h, false));
    queue<i_i > q;
    q.push(mp(sx,sy));
    while(!q.empty()){
        int x = q.front().first, y = q.front().second;
        q.pop();
        if(x == gx && y == gy){
            return true;
        }
        if(check[x][y]){
            continue;
        } else {
            check[x][y] = true;
        }

        loop(i, 4) {
            int nx = x + dx[i], ny = y + dy[i];
            if (0 < nx && nx < w && 0 < ny && ny < h){
                if (g[nx][ny] == cl){
                    q.push(mp(nx,ny));
                }
            }
        }

    }
    return false;
}

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.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;
					}
				}
			}
		}
		
		if(field[xs - 1][ys - 1] != 0){
			que.push(complex< int >(xs - 1, ys - 1));
			color = field[xs - 1][ys - 1];
			exist = false;
			
			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<functional>
#include<algorithm>
#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
#define all(v) (v).begin(),(v).end()
#define pf push_front
#define pb push_back
#define int long long
using namespace std;
const int INF=1000000007;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int mp[101][101];
bool ans;
int w,h,tmp;
int xs,ys,xg,yg;

void dfs(int y,int x,int c){
  if(y>0&&y<=h&&x>0&&x<=w&&mp[y][x]==c){
    mp[y][x]=0;
    rep(i,4){
      dfs(y+dy[i],x+dx[i],c);
    }
  }else return;
  if(y==yg&&x==xg){
    ans=true;
    return;
  }
}

signed main(){
  while(cin>>w>>h,w){
    rep(i,w)rep(j,h)mp[i][j]==0;
    ans=false;
    cin>>xs>>ys>>xg>>yg;
    int n;
    cin>>n;
    rep(i,n){
      int c,d,x,y;
      cin>>c>>d>>x>>y;
      if(d==0)rep(i,2)rep(j,4)mp[y+i][x+j]=c;
      else rep(i,2)rep(j,4)mp[y+j][x+i]=c;
    }
    tmp=mp[ys][xs];
    if(mp[ys][xs]==tmp&&mp[yg][xg]==tmp)dfs(ys,xs,tmp);
    cout<<(ans?"OK":"NG")<<endl;
  }
return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
int w,h,sx,sy,gx,gy,n,c,d,x,y,sc;
int fld[101][101];
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
void dfs(int fx,int fy){
    if(fld[fy][fx]!=sc||fx<1||fy<1||fx>w||fy>h)return;
    fld[fy][fx]=0;
    for(int i=0;i<4;i++){
        dfs(fx+dx[i],fy+dy[i]);
    }
}
signed main(){
    for(;;){
        cin>>w>>h;
        if(w==0&&h==0)break;
        cin>>sx>>sy>>gx>>gy>>n;
        fill(fld[0],fld[0]+101*101,-1);
        for(int i=0;i<n;i++){
            cin>>c>>d>>x>>y;
            if(d==0){
                for(int j=0;j<2;j++){
                    for(int k=0;k<4;k++){
                        fld[y+j][x+k]=c;
                    }
                }
            }
            if(d==1){
                for(int j=0;j<4;j++){
                    for(int k=0;k<2;k++){
                        fld[y+j][x+k]=c;
                    }
                }
            }
        }
        sc=fld[sy][sx];
        dfs(sx,sy);
        if(fld[gy][gx]==0)cout<<"OK"<<endl;
        else cout<<"NG"<<endl;
    }
    return 0;
}