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>
int xg,yg,c,sc,ans,block[101][101],w,h;
int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
int idou(int x,int y)
{
    if(x == xg&&y == yg)
    {
        ans = 1;
        return 0;
    }
    if(sc == 0)return 0;
    block[x][y] = 0;
    for(int i = 0; i < 4; i++)
    {
        if(block[x + dx[i]][y + dy[i]] == sc)idou(x + dx[i],y + dy[i]);
    }
    return 0;
}
int main()
{
    while(1)
    {
        int xs,ys,n,d,x,y;
        ans = 0;
        for(int i = 0; i < w; i++)
        {
            for(int j = 0; j < h; j++)
            {
                block[i][j] = 0;
            }
        }
        scanf("%d%d",&w,&h);
        if(w == 0&&h == 0)break;
        scanf("%d%d",&ys,&xs);
        scanf("%d%d",&yg,&xg);
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d%d%d",&c,&d,&y,&x);
            if(d == 1)
            {
                for(int i = 0; i < 2; i++)
                {
                    for(int j = 0; j < 4; j++)
                    {
                        block[x + j][y + i] = c;
                    }
                }
            }
            if(d == 0)
            {
                for(int i = 0; i < 2; i++)
                {
                    for(int j = 0; j < 4; j++)
                    {
                        block[x + i][y + j] = c;
                    }
                }
            }
        }
        sc = block[xs][ys];
        //block[xg][yg] = 9;
        /*for(int i = 0; i < w; i++)
        {
            for(int j = 0; j < h; j++)
            {
                printf("%d",block[i][j]);
            }
            printf("\n");
        }*/
        idou(xs,ys);
        if(ans == 1)printf("OK\n");
        else printf("NG\n");
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <utility>
#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)
using namespace std;

typedef pair<int,int> pii;
int dx[]={0,0,1,-1}, dy[]={1,-1,0,0};
int sx,sy,gx,gy;
int w,h,n;
int g[1000][1000];
bool valid(int y, int x){ return 0<=x && x<w && 0<=y && y<h; }

int main(){
    while(cin>>w>>h && w){
        cin>>sx>>sy>>gx>>gy;
        sx--,sy--,gx--,gy--;
        rep(i,h)rep(j,w)g[i][j]=0;
        cin>>n;
        rep(i,n){
            int c,d,x,y;
            cin>>c>>d>>x>>y;
            x--,y--;
            for(int j=0;j<4;j++)g[y+j%2][x+j/2]=c;
            if(d==0)x+=2;else y+=2;
            for(int j=0;j<4;j++)g[y+j%2][x+j/2]=c;
        }
        int C=g[sy][sx];
        // cout<<C<<endl;
        // rep(i,h){
        //     rep(j,w){
        //         cout<<g[i][j];
        //     }
        //     cout<<endl;
        // }
        rep(i,h)rep(j,w)g[i][j]= (g[i][j]==C) ? C : 0;

        bool vis[1000][1000]={};
        vis[sy][sx]=true;
        if(C){
            stack<pii> s;
            vis[sy][sx]=true;
            s.emplace(sy,sx);
            while(s.size()){
                int y,x;
                tie(y,x) = s.top();s.pop();
                rep(d,4){
                    int nx=x+dx[d],ny=y+dy[d];
                    if(!valid(ny,nx))continue;
                    if(vis[ny][nx])continue;
                    if(!g[ny][nx])continue;
                    vis[ny][nx]=true;
                    s.emplace(ny,nx);
                }
            }
            // rep(i,h){
            //     rep(j,w){
            //         cout<<vis[i][j];
            //     }
            //     cout<<endl;
            // }
        }
        puts(C && vis[gy][gx] ? "OK" : "NG");
    }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <utility>
#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)
using namespace std;

typedef pair<int,int> pii;
int dx[]={0,0,1,-1}, dy[]={1,-1,0,0};
int sx,sy,gx,gy;
int w,h,n;
int g[1000][1000];
bool valid(int y, int x){ return 0<=x && x<w && 0<=y && y<h; }

int main(){
    while(cin>>w>>h && w){
        cin>>sx>>sy>>gx>>gy;
        sx--,sy--,gx--,gy--;
        cin>>n;
        rep(i,h)rep(j,w)g[i][j]=0;
        rep(i,n){
            int c,d,x,y;
            cin>>c>>d>>x>>y;
            x--,y--;
            for(int j=0;j<4;j++)g[y+j%2][x+j/2]=c;
            if(d==0)x+=2;else y+=2;
            for(int j=0;j<4;j++)g[y+j%2][x+j/2]=c;
            g[y][x]=c;
        }
        int C=g[sy][sx];
        rep(i,h)rep(j,w)g[i][j]= (g[i][j]==C) ? C : 0;
        bool vis[1000][1000]={};
        vis[sy][sx]=true;
        if(C){
            stack<pii> s;
            vis[sy][sx]=true;
            s.emplace(sy,sx);
            while(s.size()){
                int y,x;
                tie(y,x) = s.top();s.pop();
                rep(d,4){
                    int nx=x+dx[d],ny=y+dy[d];
                    if(!valid(nx,ny))continue;
                    if(vis[ny][nx])continue;
                    vis[ny][nx]=true;
                    s.emplace(ny,nx);
                }
            }
        }
        puts(vis[gy][gx] ? "OK" : "NG");
    }
}