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 <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

typedef struct{
	int x;
	int y;
}point;

int main(){
	while(1){
		int f[101][101];
		int w, h, n;
		point s, g;
		
		memset(f, 0, sizeof(f));
		
		//入力
		scanf(" %d %d", &w, &h);
		if(w==0) break;
		
		scanf(" %d %d %d %d", &s.x, &s.y, &g.x, &g.y);
		scanf(" %d", &n);
		for(int i=0; i<n; ++i){
			int c, d, bx, by;
			scanf(" %d %d %d %d", &c, &d, &bx, &by);
			
			if(d==0){
				for(int j=0; j<2; ++j){
					for(int k=0; k<4; ++k){
						f[by+j][bx+k]=c;		
					}
				}
			}
			else {
				for(int j=0; j<4; ++j){
					for(int k=0; k<2; ++k){
						f[by+j][bx+k]=c;		
					}
				}
			}
		}
		
		//bfs
		int dx[4]={1,0,-1,0}, dy[4]={0,1,0,-1};
		int color=f[s.y][s.x]; //スタート地点のブロックの色
		queue<point> que;
		f[s.y][s.x]=6; //訪問済み
		que.push(s);
		
		while( !que.empty() ){
			point v=que.front();
			que.pop();
			for(int i=0; i<4; ++i){
				int nx=v.x+dx[i], ny=v.y+dy[i];
				
				if(nx>0 && nx<=w && ny>0 && ny<=h && f[ny][nx]==color){
					f[ny][nx]=6;
					point tmp;
					tmp.x=nx; tmp.y=ny;
					que.push(tmp);
				}
			}	
		}

		//ゴールにたどり着いているか判定
		if(f[g.y][g.x]==6) printf("OK\n");
		else printf("NG\n");	
	}	
}

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

int weight, hight;
int g[110][110];
int sc;
int sx, sy, gx, gy;
bool flag = false;
const int vx[4] = {0, 0, -1, 1};
const int vy[4] = {1, -1, 0, 0};

void dfs(int x, int y){
    if(x == gx && y == gy){
        flag = true;
        return;
    }
    
    for(int vh = 0;vh < 4;vh++){
        for(int vw = 0;vw < 4;vw++){
            int nx = x + vx[vw];
            int ny = y + vy[vh];
            if(nx >= 0 && nx < weight && ny >= 0 && ny < hight){
                if(g[ny][nx] == sc){
                    g[y][x] = 8;
                    dfs(nx, ny);
                }
            }
        }
    }
    
    return ;
}

void disp(){
    for(int i = 0;i < hight;i++){
        for(int j = 0;j < weight;j++){
            cout << g[i][j];
        }cout << endl;
    }
}

int main(){
    int dn, c, d, x, y;
    
    while(cin >> weight >> hight){
        if(weight == 0 && hight == 0){
            break;
        }
        
        cin >> sx >> sy >> gx >> gy >> dn;
        
        for(int i = 0;i < dn;i++){
            cin >> c >> d >> x >> y;
            
            if(sx == x && sy == y){
                sc = c;
            }
            
            if(d == 0){
                for(int h = 0;h < 2;h++){
                    for(int w = 0;w < 4;w++){
                        g[y+h][x+w] = c;
                    }
                }
            }else{
                for(int h = 0;h < 4;h++){
                    for(int w = 0;w < 2;w++){
                        g[y+h][x+w] = c;
                    }
                }
            }
        }

        flag = false;
        dfs(sx, sy);
        
        if(flag){
            cout << "OK\n";
        }else{
            cout << "NG\n";
        }
    }
    
    return 0;
}

C++: #include<string>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;

int block[100][100];
bool maze = false;
int w, h;

void Lnitialization(int w,int h){
	for (int i = 1; i <= h; i++){
		for (int j = 1; j <= w; j++){
			block[i][j] = 0;
		}
	}
}
void Side(int c,int x,int y){
	for (int i = y; i < y + 2; i++){
		for (int j = x; j < x + 4; j++){
			block[i][j] = c;
		}
	}
}
void Longitudinal(int c,int x,int y){
	for (int i = y; i < y + 4; i++){
		for (int j = x; j < x + 2; j++){
			block[i][j] = c;
		}
	}
}
void solve(int xs, int ys, int xg, int yg,int c){

	if (xs<0 || xs>w || ys<0 || ys>h || block[ys][xs] != c) return;
	if (ys == yg && xs == xg) {
		maze = true;
		return;
	}
	block[ys][xs] = -1;

	solve(xs+1, ys, xg, yg, c);
	solve(xs-1, ys, xg, yg, c);
	solve(xs, ys+1, xg, yg, c);
	solve(xs, ys-1, xg, yg, c);

	return;
}

// block[xx][xx]???-1?????£?????´??? return;
// block[xx][xx]??????????????§????????´??? return;
// block[xx][xx]?????´???????????£?????´??????true AND re:start

int main(){
	int xs, ys;
	int xg, yg;
	int n;
	int c, d, x, y;
	while (cin >> w >> h, w, h){
		Lnitialization(w, h);
		cin >> xs >> ys;
		cin >> xg >> yg;
		cin >> n;
		for (int i = 0; i < n; i++){
			cin >> c >> d >> x >> y;
			if (d == 0) Side(c, x, y);
			else if (d == 1) Longitudinal(c, x, y);
		}
		c = block[ys][xs];
		solve(xs, ys, xg, yg, c);
		if (maze) cout << "OK" << endl;
		else cout << "NG" << endl;
		maze = false;
 	}
	return 0;
}