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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <unordered_set>
//#include <unordered_map>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
int h,w;
vvint vv;
pii s,g;
vvint used;
int dd[]={1,0,-1,0,1};
bool foo(pii a,int c){
	if(used[a.F][a.S]) return false;
	if(vv[a.F][a.S]!=c) return false;
	used[a.F][a.S]=1;
	if(a==g) return true;
	rep(i,4){
		pii next=a;
		next.F+=dd[i];
		next.S+=dd[i+1];
		if(0<=next.F&&next.F<h&&0<=next.S&&next.S<w){
			if(foo(next,c)) return true;
		}
	}
	return false;
}
void mainmain(){
	while(cin>>w>>h,w||h){
		initvv(vv,h,w,0);
		used=vv;
		cin>>s.S>>s.F>>g.S>>g.F;
		s.F--;s.S--;
		g.F--;g.S--;
		int n;
		cin>>n;
		rep(i,n){
			int a,b,c,d;
			cin>>a>>b>>d>>c;
			c--;
			d--;
			if(b==0){
				vv[c][d]=vv[c][d+1]=vv[c][d+2]=vv[c][d+3]=a;
				vv[c+1][d]=vv[c+1][d+1]=vv[c+1][d+2]=vv[c+1][d+3]=a;
			}
			else{
				vv[c][d]=vv[c+1][d]=vv[c+2][d]=vv[c+3][d]=a;
				vv[c][d+1]=vv[c+1][d+1]=vv[c+2][d+1]=vv[c+3][d+1]=a;
			}
		}
		rep(i,h){
			rep(j,w){

			}
		}
		if(vv[s.F][s.S]==0){
			cout<<"NG"<<endl;
		}
		else if(foo(s,vv[s.F][s.S])){
			cout<<"OK"<<endl;
		}
		else{
			cout<<"NG"<<endl;
		}
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

C++: #include <iostream>
using namespace std;
  
int F[102][102];
int xg,yg,w,h;
  
  
void DFS(int Y,int X,int c){
  if(Y < 1 || X < 1)
    //return;
  if(F[Y][X] != c || Y > w || X > h){
    return;
  }
  
  F[Y][X] = -1;
  DFS(Y+1,X  ,c);
  DFS(Y-1  ,X,c);
  DFS(Y,X+1  ,c);
  DFS(Y  ,X-1,c);
}
  
  
int main(){
  int xs,ys,n;
  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0)
      break;
for(int i=0; i<102; i++){
      for(int j=0; j<102; j++){
        F[i][j] = 10;
      }
    }
      
    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 == 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(F[xg][yg] == -1){
    cout << "OK" << endl;
  } else {
    cout << "NG" <<endl;
  }
  }
}

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

class Point{
public:
	Point() : x(0), y(0){}
	Point(int ax, int ay) : x(ax), y(ay){}
	int x, y;
};

int field[100][100] = {0};
bool checked[100][100] = {0};
int w, h, n;
Point s, g;

const Point dire4[4] = {Point(-1, 0), Point(1, 0), Point(0, -1), Point(0, 1)};

bool isEnabled(int x, int y){
	return (0 <= x && x < w && 0 <= y && y < h);
}

int main(){
	while(1){
		cin >> w >> h;
		if(w == 0 && h == 0) break;
		
		for(int y = 0;y < h;y++){
			for(int x = 0;x < w;x++){
				field[x][y] = 0;
				checked[x][y] = false;
			}
		}
		
		cin >> s.x >> s.y >> g.x >> g.y >> n;
		s.x--; s.y--; g.x--; g.y--;
		for(int i = 0;i < n;i++){
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			x--; y--;
			int tw = ((d == 0)? 4 : 2), th = ((d == 0)? 2 : 4);
			for(int iy = 0;iy < th;iy++){
				for(int ix = 0;ix < tw;ix++){
					field[x + ix][y + iy] = c;
				}
			}
		}
		
		bool res = false;
		
		queue<Point> que;
		que.push(s);
		while(!que.empty()){
			Point p = que.front();
			que.pop();
			if(p.x == g.x && p.y == g.y){
				res = true;
				break;
			}
			for(int i = 0;i < 4;i++){
				int x = p.x + dire4[i].x, y = p.y + dire4[i].y;
				if(isEnabled(x, y) && field[p.x][p.y] == field[x][y] && !checked[x][y]){
					que.push(Point(x, y));
					checked[x][y] = true;
				}
			}
		}
		
		if(res) cout << "OK" << endl;
		else cout << "NG" << endl;
	}
	
	return 0;
}