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];

bool DFS(int X, int Y, int Xg, int Yg, int c){
	if(F[Y][X] != c){
		return false;
	}else if(F[Y][X] == c && X == Xg && Y == Yg){
		return true;
	}
	F[Y][X] = -1;
	if(DFS(X, Y - 1, Xg, Yg, c)){
		return true;
	}else if(DFS(X + 1, Y, Xg, Yg, c)){
		return true;
	}else if(DFS(X, Y + 1, Xg, Yg, c)){
		return true;
	}else if(DFS(X - 1, Y, Xg, Yg, c)){
		return true;
	}
	
	return false;
}

int main(){
while(true){
	int w, h;
	cin >> w >> h;
	if(w == 0 && h == 0){
		break;
	}
	int xs, ys, xg, yg;
	cin >> xs >> ys >> xg >> yg;
	int n;
	cin >> n;
	for(int y = 0; y < 102; y++){
		for(int x = 0; x < 102; x++){
			F[y][x] = -1;
		}
	}
	for(int i = 0; i < n; i++){
		int c, d, x, y;
		cin >> c >> d >> x >> y;
		if(d == 0){
			for(int X = x; X < x + 4; X++){
				for(int Y = y; Y < y + 2; Y++){
					F[Y][X] = c;
				}
			}
		}else if(d == 1){
			for(int X = x; X < x + 2; X++){
				for(int Y = y; Y < y + 4; Y++){
					F[Y][X] = c;
				}
			}
		}
	}
	if(DFS(xs, ys, xg, yg, F[xg][yg])){
		cout << "OK" << endl;
	}else{
		cout << "NG" << endl;
	}
}
	return 0;
}

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

using namespace std;

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

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

	while(true){
		cin >> w >> h;

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

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

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

		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;

	}

	return 0;
}

bool bfs(pair<int,int> sg[2],int board[102][102]){
	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>
using namespace std;

int F[102][102];
int xg,yg,ok;

void Maze(int X,int Y,int C){
	//cout << X<<Y<<F[X][Y]<<endl;
	if(F[X][Y]!=C)
		return;

	

	if(X==xg&&Y==yg){
		cout << "OK"<<endl;
		ok++;
	}

	F[X][Y]='.';

	Maze(X-1,Y  ,C);
	Maze(X  ,Y+1,C);
	Maze(X+1,Y  ,C);
	Maze(X  ,Y-1,C);
	
}



int main(){	
	while(1){
		ok=0;

		int w,h,xs,ys,n,c,d,x,y;

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

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


		for(int i=1; i<=w; i++)
			for(int j=1; j<=h; j++)
				F[j][i]=0;


		//for(int i=0; i<=101;i++){
			//F[0][i]=6;
			//F[i][0]=6;
		//}

		for(int i=1; i<=w;i++){
				F[w+1][i]=6;
				F[0][i]=6;
		}
		for(int i=1; i<=h;i++){
				F[i][h+1]=6;
				F[i][0]=6;
		}

		int N=0;
		while(N<n){
//cout <<N;
			cin >>c>>d>>x>>y;
//cout<<c<<d<<x<<y<<endl;
			if(d){
				for(int i=x; i<x+2; i++)
					for(int j=y; j<y+4; j++){
						F[i][j]=c;
//cout <<F[i][j];
				}
//cout <<endl;
			}else{
				for(int i=x; i<x+4; i++)
					for(int j=y; j<y+2; j++){
						F[i][j]=c;
//cout <<F[i][j];
					}
//cout <<endl;
			}
			N++;
		}

//for(int i=0; i<=101; i++){
	//for(int j=0; j<=101; j++){
		//cout <<F[j][i];
		//}
	//cout << endl;
//}
		
		Maze(xs,ys,F[xs][ys]);

		if(ok==0)
			cout <<"NG"<<endl;

	}

}

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



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

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