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<bits/stdc++.h>
using namespace std;
queue <pair<int,int> > blocks;
int w,h,xs,ys,n,xg,yg,c,d,x,y,area[105][105];
bool flag=false;
void solve(int x,int y,int c);

int main(){
    while(1){
        cin >> w >> h;
        if(w==0&&h==0)break;
        cin >> xs >> ys >> xg >> yg >> n;
        for(int i=0;i<n;i++){
            cin >> c >> d >> x >> y;
            if(d==0){
            for(int j=y;j<y+2;j++){
                for(int k=x;k<x+4;k++){
                    area[k][j]=c;
                }
            }
            }else{
            for(int j=y;j<y+4;j++){
                for(int k=x;k<x+2;k++){
                    area[k][j]=c;
                    }
                }
            }
        }
        if(area[xs][ys]!=area[xg][yg]);
        else {
        blocks.push(make_pair(xs,ys));
        solve(1,1,area[xs][ys]);
        }
        if(flag==true)cout << "OK" << endl;
        else cout << "NG" << endl;
        flag=false;
        fill(area[0],area[104],0);
    }

return 0;
}

void solve(int x,int y,int c){
    if(!blocks.empty()){
    blocks.pop();
    if(x==xg&&y==yg){
        flag=true;
        while(!blocks.empty())blocks.pop();
    }else{
        area[x][y]=0;
        if(area[x-1][y]==c)blocks.push(make_pair(x-1,y));
        if(area[x][y-1]==c)blocks.push(make_pair(x,y-1));
        if(area[x+1][y]==c)blocks.push(make_pair(x+1,y));
        if(area[x][y+1]==c)blocks.push(make_pair(x,y+1));
        solve(blocks.front().first,blocks.front().second,c);
        }
    }
}

C++: #include <iostream>
#include <string.h>
#include <queue>
using namespace std;
struct q{
    int x,y,color,cnt;
};
queue <q> pipe;
q Main,sub;
int sy,sx;
int gy,gx;
int w,h,n;
int board[101][101];
int visited[101][101];
int state[4];

void put(int mode,int color,int x,int y);

int main(void){
    cin>>w>>h;
    while(w!=0&&h!=0){
        cin>>sx>>sy;
        cin>>gx>>gy;
        cin>>n;
        memset(board,0,sizeof(board));
        memset(visited,0,sizeof(visited));
        for(int i=0;i<n;i++){
            cin>>state[0]>>state[1]>>state[2]>>state[3];
            put(state[1],state[0],state[2],state[3]);
        }
        
        Main.x=sx;
        Main.y=sy;
        Main.cnt=0;
        Main.color=board[sx][sy];
        visited[sx][sy]=1;
        pipe.push(Main);
        
        int mx,fx[]={0,1,0,-1};
        int my,fy[]={1,0,-1,0};
        
        while(!pipe.empty()){
            Main=pipe.front();
            pipe.pop();
            for(int i=0;i<4;i++){
                mx=Main.x+fx[i];
                my=Main.y+fy[i];
                if(mx>=1&&mx<=100&&my>=1&&my<=100&&Main.color==board[mx][my]&&visited[mx][my]==0){
                    sub=Main; visited[mx][my]=1;
                    sub.x=mx; sub.y=my; sub.cnt++;
                    pipe.push(sub);
                }
            }
        }
        if(visited[gx][gy]==1){cout<<"OK"<<endl;}
        else{cout<<"NG"<<endl;}
        cin>>w>>h;
    }
}

void put(int mode,int color,int x,int y){
    int X[2],Y[2];
    X[0]=4; Y[0]=2;
    X[1]=2; Y[1]=4;
    for(int i=x;i<x+X[mode];i++){
        for(int j=y;j<y+Y[mode];j++){
            board[i][j]=color;
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define INF 1010001000
#define loop(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
typedef pair<int, int> i_i;

bool dfs(vector<vector<int> > &g);

int sx, sy, gx, gy;
int n, m; 
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};

int main()
{

while(cin >> n >> m,(n|m)){
 m++; n++;
 vector<vector<int> > graph(m,vector<int>(n,0));
 
 cin>>sx>>sy>>gx>>gy;
 int bl;  cin>>bl;
 loop(i, bl){
  int a,b,c,t; cin>>a>>b>>c>>t;
  if(b){
   for(int j=t;j<t+2;j++){
    for(int k=c;k<c+4;k++){
     graph[k][j] = a;
    }
   } 
  } else {
   for(int j=c;j<c+2;j++){
    for(int k=t;k<t+4;k++){
     graph[k][j] = a;
    }
   }
  }
 }
cout << ((dfs(graph)) ? "OK" : "NG") << endl;
}
return 0;
}

bool dfs(vector<vector<int> > &g) {
int cl = g[sx][sy];
vector<vector<bool> > vsed(m,vector<bool>(n,false));
queue<i_i > q;
q.push(mp(sx,sy));
while(!q.empty()){
int x = q.front().first, y = q.front().second;
q.pop();
if(x == gx && y == gy){
return true;
}
if(vsed[x][y]){
continue;
} else {
vsed[x][y]=true;
}

loop(i, 4) {
int nx = x +dx[i], ny = y +dy[i];
if(0<nx && nx < n && 0<ny && ny < m){
if(g[nx][ny] == cl){
q.push(mp(nx,ny));
}
} 
}

}
return false;
}