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 <cstdio>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
int dy[] = {1, 0, 0, -1};
int dx[] = {0, 1, -1, 0};
int b[110][110];
int w, h;
int xs, ys, xg, yg;
int color;
bool flag = false;
bool used[110][110];

void dfs(int y, int x, int py, int px){
	rep(i, 4){
		int ny = y + dy[i], nx = x + dx[i];
		if(used[ny][nx]) continue;//???????????¨???????????????????????????
		if(!(0 <= ny && ny < h && 0 <= nx && nx < w)) continue;
		if(b[ny][nx] != color) continue;
		if(ny == py && nx == px) continue;
		if(ny == yg && nx == xg){
			// used[ny][nx] = true;
			flag = true; return;//????????????
		}
		used[ny][nx] = true;
		dfs(ny, nx, y, x);
	}
	return;//????????¢?????¨???????????????
}

int main(void){
	while(1){
		rep(i, 110)rep(j, 110){
			b[i][j] = -1; used[i][j] = false;
		}
		cin >> w >> h;
		if(w == 0 && h == 0) return 0;
		cin >> xs >> ys >> xg >> yg;
		xs--; ys--; xg--; yg--;
		int n; cin >> n;
		rep(i, n){
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			x--; y--;
			if(d == 0){//?¨?
				b[y][x] = b[y][x + 1] = b[y][x + 2] = b[y][x + 3] = c;
				b[y + 1][x] = b[y + 1][x + 1] = b[y + 1][x + 2] = b[y + 1][x + 3] = c;
			}else{//???
				b[y][x] = b[y][x + 1] = c;
				b[y + 1][x] = b[y + 1][x + 1] = c;
				b[y + 2][x] = b[y + 2][x + 1] = c;
				b[y + 3][x] = b[y + 3][x + 1] = c;
			}
		}
		color = b[ys][xs];
		if(color == -1) {//?????????????????????????????????
			printf("NG\n"); continue;
		}
		used[ys][xs] = true; flag = false;
		dfs(ys, xs, -1, -1);
		if(flag) printf("OK\n");
		else printf("NG\n");
	}
}

C++: #include<cstdio>
#include<queue>
using namespace std;
struct Block
{
	int color;
	int angle;
	int x,y;
};
int main(void)
{
	
	
RE:
	queue<pair<int,int> > que;
	int **board;
	int board_mx,board_my;
	int start_x,start_y;
	int goal_x,goal_y;
	int block_num;
	Block *block;
	//
	scanf("%d %d",&board_mx,&board_my);
	if(board_mx==0&&board_my==0)
	{
		goto END;
	}
	board=new int*[board_my];
	for(int f1=0;f1<board_my;f1++)
	{
		board[f1]=new int[board_mx];
		for(int f2=0;f2<board_mx;f2++)
		{
			board[f1][f2]=0;
		}
	}
	scanf("%d %d",&start_x,&start_y);
	scanf("%d %d",&goal_x,&goal_y);
	scanf("%d",&block_num);
	block=new Block[block_num];
	for(int f1=0;f1<block_num;f1++)
	{
		scanf("%d %d %d %d",&block[f1].color,&block[f1].angle,&block[f1].x,&block[f1].y);
		block[f1].x--;
		block[f1].y--;
	}
	//
	for(int f1=0;f1<block_num;f1++)
	{
		//横
		if(!block[f1].angle)
		{
			for(int f2=0;f2<2;f2++)
			{
				for(int f3=0;f3<4;f3++)
				{
					board[block[f1].y+f2][block[f1].x+f3]=block[f1].color;
				}
			}
		}
		//縦
		else
		{
			for(int f2=0;f2<4;f2++)
			{
				for(int f3=0;f3<2;f3++)
				{
					board[block[f1].y+f2][block[f1].x+f3]=block[f1].color;
				}
			}
		}
	}
	/*
	for(int f1=0;f1<board_my;f1++)
	{
		for(int f2=0;f2<board_mx;f2++)
		{
			printf("%d",board[f1][f2]);
		}
		printf("\n");
	}
	*/
	int st_col=board[start_y-1][start_x-1];
	int goal_col=board[goal_y-1][goal_x-1];
	//
	if(st_col!=goal_col||st_col==0||goal_col==0)
	{
		printf("NG\n");
		goto RE;
	}
	//queue<pair<int,int> > que;
	que.push(make_pair(start_y,start_x));
	while(!que.empty())
	{
		pair<int,int> buf=que.front();que.pop();
		if(buf.first==goal_y-1&&buf.second==goal_x-1)
		{
			printf("OK\n");
			goto RE;
		}
		//printf("%d %d\n",buf.second,buf.first);
		board[buf.first][buf.second]=9;
		if(buf.first+1<board_my)
		{
			if(board[buf.first+1][buf.second]==st_col)
			{
				que.push(make_pair(buf.first+1,buf.second));
			}
		}
		if(buf.first-1>=0)
		{
			if(board[buf.first-1][buf.second]==st_col)
			{
				que.push(make_pair(buf.first-1,buf.second));
			}
		}
		if(buf.second+1<board_mx)
		{
			if(board[buf.first][buf.second+1]==st_col)
			{
				que.push(make_pair(buf.first,buf.second+1));
			}
		}
		if(buf.second-1>=0){
			if(board[buf.first][buf.second-1]==st_col)
			{
				que.push(make_pair(buf.first,buf.second-1));
			}
		}
	}
	/*
	for(int f1=0;f1<board_my;f1++)
	{
		for(int f2=0;f2<board_mx;f2++)
		{
			printf("%d",board[f1][f2]);
		}
		printf("\n");
	}
	*/
	printf("NG\n");
END:
	/*
	printf("end\n");
	fflush(stdin);
	getchar();
	*/
	return 0;
}

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

int F[102][102];

bool DFS(int Y, int X, int C, int yg, int xg)
{
  if(Y == yg && X == xg)
    return true;
  if(F[Y][X]!=C)
    return false;

  F[Y][X] = 0;

  bool tmp = 0;
  tmp += DFS(Y-1, X, C, yg, xg);
  tmp += DFS(Y+1, X, C, yg, xg);
  tmp += DFS(Y, X-1, C, yg, xg);
  tmp += DFS(Y, X+1, C, yg, xg);
  return tmp;
}

int main()
{
  int w, h;
  cin >> w >> h;

  if(w == 0 && h == 0)
    return 0;

  int xs, ys;
  cin >> xs >> ys;

  int xg, yg;
  cin >> xg >> yg;

  int n;
  cin >> n;

  int c, d, x, y;
  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++)
          F[y+j][x+k] = c;
    else
      for(int j=0; j<4; j++)
        for(int k=0; k<2; k++)
          F[y+j][x+k] = c;
  }

  bool res = DFS(ys, xs, F[ys][xs], yg, xg);
  if(res)
    cout << "OK" << endl;
  else
    cout << "NG" << endl;

  main();
}