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<stdio.h>
#include<string.h>
void haichi(void);
void Block(int,int);
int c,d,x,y,xg,yg,M,Flag;
int MAP[102][102],MF[102][102];
int main(void)
{
	int w,h,xs,ys,n;
	while(1){
		Flag=0;
		memset(MAP,0,sizeof(MAP));
		memset(MF,0,sizeof(MF));
		scanf("%d %d",&w,&h);
		if(w == 0 && h == 0) break;
		scanf("%d %d %d %d",&xs,&ys,&xg,&yg);
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			haichi();
		}
		/*for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				printf("%d",MAP[i][j]);
			}
			printf("\n");
		}*/
		M = MAP[ys][xs];//
		if(MAP[ys][xs] == MAP[yg][xg])
			Block(xs,ys);
		if(Flag==1)
			printf("OK\n");
		else
			printf("NG\n");
	}
	return 0;
}

void haichi(void)
{
	if(d==0){
		MAP[y][x]=c;MAP[y][x+1]=c;MAP[y][x+2]=c;MAP[y][x+3]=c;
		MAP[y+1][x]=c;MAP[y+1][x+1]=c;MAP[y+1][x+2]=c;MAP[y+1][x+3]=c;
	}
	else if(d==1){
		MAP[y][x]=c;MAP[y][x+1]=c;
		MAP[y+1][x]=c;MAP[y+1][x+1]=c;
		MAP[y+2][x]=c;MAP[y+2][x+1]=c;
		MAP[y+3][x]=c;MAP[y+3][x+1]=c;
	}
}

void Block(int X,int Y)
{
	MF[Y][X] = 1;
	//printf("%d",M);
	if(X == xg && Y == yg)
		Flag = 1;
	if(Y>0 && X>0){
		if(MAP[Y-1][X] == M && MF[Y-1][X] == 0){
			Block(X,Y-1); //???
			//MF[Y][X-1]=0;
		}
		if(MAP[Y+1][X] == M && MF[Y+1][X] == 0){
			Block(X,Y+1); //???
			//MF[Y+1][X]=0;
		}
		if(MAP[Y][X-1] == M && MF[Y][X-1] == 0){
			Block(X-1,Y); //???
			//MF[Y][X-1]=0;
		}
		if(MAP[Y][X+1] == M && MF[Y][X+1] == 0){
			Block(X+1,Y); //???
			//MF[Y][X-1]=0;
		}
	}
}

C++: #include <iostream>
#include <queue>

using namespace std;

bool bfs(pair<int,int>[],int **);

int main(void){
	int w,h,xs,ys,xg,yg,n,**board,dr[2][2]={4,2,2,4};
	pair<int,int> sg[2];

	do{
		cin >> w >> h >> xs >> ys >> xg >> yg >> n;

		sg[0] = make_pair(xs,ys);
		sg[1] = make_pair(xg,yg);

		board = new int*[w+2];
		for(int i=0;i<w+2;i++)
			board[i] = new int[h+2];

		for(int i=0;i<h+2;i++)
			for(int j=0;j<w+2;j++)
				board[j][i]=0;

		for(int i=0;i<n;i++){
			int c,d,x,y;
			cin >> c >> d >> x >> y;
			for(int j=0;j<dr[d][0];j++)
				for(int k=0;k<dr[d][1];k++)
					board[x+j][y+k]=c;
		}

		cout << (bfs(sg,board)?"OK":"NG");
		cout << endl;

		for(int i=0;i<w+2;i++)
			delete [] board[i];
		delete [] board;	
	}while(w!=0||h!=0);

	return 0;
}

bool bfs(pair<int,int> sg[],int **board){
	queue<pair<int,int>> Q;
	pair<int,int> cur;
	int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1},c;

	Q.push(sg[0]);
	cur=Q.front();
	c=board[cur.first][cur.second];

	while(!Q.empty()&&c!=0){
		cur=Q.front();
		Q.pop();
		board[cur.first][cur.second]=0;

		if(cur==sg[1])
			return true;

		for(int i=0;i<4;i++)
			for(int j=0;j<4;j++)
				if(board[cur.first+dx[i]][cur.second+dy[j]]==c)
					Q.push(make_pair(cur.first+dx[i],cur.second+dy[j]));

	}
	return false;
}

C++: #include<iostream>
#include<algorithm>

using namespace std;

int main()
{
	static int board[101][101],wf[10300][10300];
	int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

	for(int w,h;cin>>w>>h,w;){
		for(int i=1;i<=h;i++)for(int j=1;j<=w;j++)board[i][j]=-1;
		int xs,ys,xg,yg,n;	cin>>xs>>ys>>xg>>yg>>n;
		while(n--){
			int c,d,x,y;	cin>>c>>d>>x>>y;
			for(int i=0;i<(d?4:2);i++)for(int j=0;j<(d?2:4);j++)board[y+i][x+j]=c;
		}

		if(board[ys][xs]!=board[yg][xg])	cout<<"NG"<<endl;
		else{
			int clr=board[ys][xs];
			for(int i=1;i<=w*h;i++)for(int j=1;j<=w*h;j++)wf[i][j]=1000000;
			for(int i=1;i<=h;i++){
				for(int j=1;j<=w;j++){
					if(board[i][j]==clr){
						for(int k=0;k<4;k++){
							int xx=j+dx[k],yy=i+dy[k];
							if(0<xx&&xx<=w&&0<yy&&yy<=h&&board[yy][xx]==clr)
								wf[w*i+j][w*yy+xx]=wf[w*yy+xx][w*i+j]=0;
						}
					}
				}
			}

			for(int k=1;k<=w*h;k++)for(int i=1;i<=w*h;i++)for(int j=1;j<=w*h;j++)
				wf[i][j]=min(wf[i][j],wf[i][k]+wf[k][j]);

			cout<<(wf[w*ys+xs][w*yg+xg]==0?"OK":"NG")<<endl;
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int board[105][105];
int xg , yg;
int w, h;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int sc;
bool b;

void dfs(int x , int y){
	if(x==xg&&y==yg) {b=true;return;}
	for(int i=0;i<4;++i){
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(tx<0||ty<0||tx>=w||ty>=h) continue;
		if(board[tx][ty]!=sc || board[tx][ty]==0) continue;
		if(b) continue;
		dfs(tx,ty);
	}
}

int main(){
	while(cin >> w >> h , w||h){
		int xs , ys;
		int n;
		b=false;
		//memset(board,0,sizeof(board));
		for(int i=0;i<101;++i){
			for(int j=0;j<101;++j){
				board[j][i]=0;
			}
		}
		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 k=0;k<2;++k){
					for(int j=0;j<4;++j){
						board[x+j][y+k]=c;
					}
				}
			} else if(d==1){
				for(int k=0;k<4;++k){
					for(int j=0;j<2;++j){
						board[x+j][y+k]=c;
					}
				}
			}
		}
		sc=board[xs][ys];
		int gc=board[xg][yg];
		if(sc!=gc) {}
		else dfs(xs,ys);
		cout << ( b ? "OK" : "NG") << endl;
	}
	return 0;
}