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<cctype>
#include<utility>
#include<string>
#include<cmath>
#include <numeric>
#include<map>
#include<cstdio>
 
 
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1e9
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
 
typedef long long int ll;
#define MOD 1000000007
#define MAX_H 110
#define MAX_W 110

int h,w;
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,1,-1};
/*左、右、上、下*/
int xs,ys,xg,yg,color;
int mp[MAX_H][MAX_W];
bool ans = false;


void dfs(int y,int x){

    if(y == yg && x == xg){
        /*ゴールについた*/
        ans = true;
        return;
    }
    //今いるところを置き換える
    mp[y][x] = -1;
    //移動方向をループ
    for(int i = 0;i < 4;i++){
        //x方向にdx,ｙ方向にdy移動した場所を(ny,nx)とする
        int nx = x + dx[i], ny = y + dy[i];
        if(1 <= nx && nx <= w && 1 <= ny && ny <= h && mp[ny][nx] == color){/*同じ色なら進める*/
            dfs(ny,nx);
        }
    }
    return;/*全て調べ終わり、1つ深さを戻す*/
}




void solve(){

    for(int i = 1;i <= h;i++){
        for(int j = 1;j <= w;j++){
            if(mp[i][j] == color){
                dfs(i,j);
                if(ans){
                    /*dfsによってゴールに着いた*/
                    return;
                }
            }
        }
    }
}



int main(){

    while(cin >> w >> h,w){
        cin >> xs >> ys >> xg >> yg;

        int c,d,x,y,n;
        cin >> n;

        REP(i,n){

            cin >> c >> d >> x >> y;
            if(d == 0){
                /*横長*/
                for(int j = y;j < y+2;j++){
                    for(int k = x;k < x+4;k++){
                        mp[j][k] = c;
                    }
                }
            }else{
                /*縦長*/
                for(int j = y;j < y+4;j++){
                    for(int k = x;k < x+2;k++){
                        mp[j][k] = c;
                    }
                }
            }
        }

        color = mp[ys][xs];
    
        if(mp[ys][xs] == 0 || mp[yg][xg] == 0){
            ans = false;
        }else{
            /*横方向がx、縦方向がy*/
            dfs(ys,xs);
        }

        if(ans){
            cout << "OK" << endl;
        }else{
            cout << "NG" << endl;
        }
       
        /*初期化*/
        ans = false;
        for(int i = 1;i <= MAX_H;i++){
            for(int j = 1;j <= MAX_W;j++){
                mp[i][j] = 0;
            }
        }
    }
    return 0;
}

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

int f[102][102];

bool dfs(int ys,int xs,int yg,int xg,int c){
  if(f[ys][xs]!=c)return 0;

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

  f[ys][xs]=0;

  bool flag=0;
  flag += dfs(ys+1,xs,yg,xg,c);
  flag += dfs(ys-1,xs,yg,xg,c);
  flag += dfs(ys,xs+1,yg,xg,c);
  flag += dfs(ys,xs-1,yg,xg,c);
  return flag;
}

void set(int c,bool d,int x,int y){
  if(d==0){
    for(int i=0; i<4; i++){
      f[y][x+i]=c;
      f[y+1][x+i]=c;
    }
  }else if(d==1){
    for(int i=0; i<4; i++){
      f[y+i][x]=c;
      f[y+i][x+1]=c;
    }
  }
}

int main(){
  while(1){

  int w,h;
  cin >> w >> h;
  if(w==0 && h==0)break;

  int xs,ys;
  cin >> xs >> ys;
  int xg,yg;
  cin >> xg >> yg;
  int n;
  cin >> n;

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

  if(dfs(ys,xs,yg,xg,f[ys][xs])){
    cout << "OK\n";
  }else{
    cout << "NG\n";
  }
  }

  return 0;
}

C++: #include <iostream>
using namespace std;
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional> //sort(A,A.end(),greater<int>());???????????????
#include <queue>
#include <stack>
#include <vector>

#define INF 10 << 10
#define MP make_pair

int map[124][124];
bool map_flg[124][124];
int x_move[] = { 0,0,1,-1 };
int y_move[] = { -1,1,0,0 };

int main() {
	int w, h;

	while (cin >> w >> h && w != 0 && h != 0) {
		memset(map, 0, sizeof(map));
		memset(map_flg, true, sizeof(map_flg));
		int xs, ys;
		int xg, yg;
		int n;

		cin >> xs >> ys >> xg >> yg >> n;

		for (int i = 1; i <= n; ++i) {
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			int x_, y_;
			if (d == 0) {
				x_ = 4;
				y_ = 2;
			}
			else {
				x_ = 2;
				y_ = 4;
			}
			for (int j = y; j < y + y_; ++j) {
				for (int j2 = x; j2 < x + x_; ++j2) {
					map[j][j2] = c;
				}
			}
		}

		if (map[ys][xs] != map[yg][xg]) {
			cout << "NG" << endl;
			continue;
		}

		stack<pair<int, int> > st;
		st.push(MP(xs, ys));
		int m_c = map[ys][xs];
		bool flg = false;
		while (!st.empty()) {
			int x = st.top().first, y = st.top().second;
			st.pop();
			map_flg[y][x] = false;
			for (int i = 0; i < 4; ++i) {
				int x_ = x + x_move[i], y_ = y + y_move[i];
				if (map[y_][x_] == m_c && map_flg[y_][x_]) {
					if (y_ == yg && x_ == xg) {
						flg = true;
						break;
					}
					st.push(MP(x_, y_));
				}
			}
			if (flg) { break; }
		}

		if (flg) { cout << "OK" << endl; }
		else { cout << "NG" << endl; }
	}

	return 0;
}