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>
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){
    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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

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

main(){
  int w,h;
  while(cin>>w>>h,w|h){
    int xs,ys,xg,yg,n;
    cin>>xs>>ys>>xg>>yg>>n;
    --xs,--ys,--xg,--yg;
    int maze[w][h];
    memset(maze,0,sizeof(maze));
    rep(i,n){
      int c,d,x,y;
      cin>>c>>d>>x>>y;
      --x,--y;
      rep(i,4-d*2)rep(j,2+d*2)maze[x+i][y+j]=c;
    }
    queue<PI> Q;
    if(maze[xs][ys]== 0 || maze[xs][ys]!=maze[xg][yg] || maze[xg][yg]==0){
      puts("NG");
      continue;
    }
    bool goal=false;
    Q.push(mp(xs,ys));
    int dp[w][h];
    memset(dp,0,sizeof(dp));
    dp[xs][ys]=1;
    while(!Q.empty()){
      int cx=Q.front().f,cy=Q.front().s;Q.pop();
      if(cx==xg && cy==yg){
        goal=true;
        break;
      }
      rep(i,4){
        int nx=cx+dx[i],ny=cy+dy[i];
        if(nx<0 || nx>=w || ny<0 || ny>=h || dp[nx][ny])continue;
        if(maze[nx][ny]!=maze[xs][ys])continue;
        dp[nx][ny]=1;
        Q.push(mp(nx,ny));
      }
    }
    cout<<(goal?"OK":"NG")<<endl;
  }
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<utility>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
 
using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+9
#define EPS 1e-9
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = j; i < (k); i++)
typedef long long ll;
int stage[128][128];
bool mem[128][128];
int xg,yg;
int w,h;
void dfs(int x,int y,int col){
    if(x < 1 || y < 1 || x > w || y > h)return ;
    if(mem[y][x])return ;
    if(stage[y][x] != col)return ;
    mem[y][x] = true;
    rep(i,4){
        if(col == stage[y+dy[i]][x+dx[i]]){
            dfs(x + dx[i], y + dy[i], col);
        }
    }
    return ;
}
int main(){
    while(scanf("%d%d",&w,&h),w|h){
        memset(stage,0,sizeof(stage));
        rep(i,128)rep(j,128)mem[i][j] = false;
        int xs,ys;
        scanf("%d%d",&xs,&ys);
        scanf("%d%d",&xg,&yg);
         
        int n;
        scanf("%d",&n);
 
        rep(q,n){
            int c,d,x,y;
            scanf("%d%d%d%d",&c,&d,&x,&y);
            if(d == 0){
                reps(i,x,x+4){
                    reps(j,y,y+2){
                        stage[j][i] = c;
                    }
                }
            }
            else{
                reps(i,x,x+2){
                    reps(j,y,y+4){
                        stage[j][i] = c;
                    }
                }
            }
        }
        dfs(xs,ys,stage[ys][xs]);
        bool f = mem[yg][xg];
        (f) ? puts("OK") : puts("NG");
    }
    return 0;
}

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

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

int main()
{
  int w, h;
  while (cin >> w >> h) {
    if ((w|h) == 0)
      break;

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

    int c;
    cin >> c;
    vector<vector<int> > block(h, vector<int>(w, -1));
    for (int i = 0; i < c; ++i) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      --x;
      --y;

      if (d == 0) {
	for (int j = 0; j < 4; ++j)
	  block[y][x+j] = block[y+1][x+j] = c;
      } else {
	for (int j = 0; j < 4; ++j)
	  block[y+j][x] = block[y+j][x+1] = c;
      }
    }

    string ans = "NG";
    queue<pair<int, int> > que;
    que.push(make_pair(xs, ys));
    vector<vector<int> > visited(h, vector<int>(w, 0));
    while (!que.empty()) {
      const pair<int, int> p = que.front();
      que.pop();

      if (p.second == yg && p.first == xg) {
	ans = "OK";
	break;
      }

      if (visited[p.second][p.first])
	continue;
      visited[p.second][p.first] = 1;

      for (int d = 0; d < 4; ++d) {
	int x = p.first + dx[d];
	int y = p.second + dy[d];
	if (x < 0 || w <= x || y < 0 || h <= y ||
	    block[p.second][p.first] != block[y][x] || visited[y][x])
	  continue;
	que.push(make_pair(x, y));
      }
    }

    cout << ans << endl;
  }
  return 0;
}