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 w,h,xs,ys,xg,yg,n;
int map[101][101];
string ans;
char start;
void fun(int y,int x){
    if(map[y][x]==start){
    	cout << 1 << endl;
        map[y][x]=7;
    }
    else
        return;
    if(x<1||w<x||y<1||h<y)
        return;
    if(map[y][x]==map[yg][xg]){
        ans="OK";
        return;
    }
    fun(y,x+1);
    fun(y,x-1);
    fun(y+1,x);
    fun(y-1,x);
}
void mc(){
    int c,d,x,y;
    for(int i=0;i<n;i++){
        cin >> c >> d >> x >> y;
        if(d==0){
            for(int j=0;j<2;j++){
                for(int k=0;k<4;k++){
                    map[y+j][x+k]=c;
                }
            }
        }
        else{
            for(int j=0;j<4;j++){
                for(int k=0;k<2;k++){
                    map[y+j][x+k]=c;
                }
            }
        }
    }
}
int main(){
    while(cin >> w >> h,w){
    	ans="NG";
        cin >> xs >> ys >> xg >> yg >> n;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                map[i][j]=0;
            }
        }
        mc();
        start=map[ys][xs];
        /*if(map[ys][xs]==0||map[yg][xg]==0);
        else*/
        	fun(ys,xs);
        for(int i=1;i<w;i++){
        	for(int j=1;j<h;j++){
        		cout << map[i][j];
        	}
        	cout << endl;
        }
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen

using namespace std;
typedef pair<int, int> P;
int maze[20][20];
bool used[20][20];
int main()
{
	const int dir[][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
//	cut here before submit 
//	freopen ("testcase.block", "r", stdin );
	int w, h;

	while (cin >> w >> h && w && h ){
		memset (maze, 0, sizeof(maze ) );
		memset (used, false, sizeof (used ) );

		P start, goal;
		cin >> start.first >> start.second;
		start.first--; start.second--;
		cin >> goal.first >> goal.second;
		goal.first--; goal.second--;
		int n;
		cin >> n;
		for (int i = 0; i < n; ++i ){
			int color, dir;
			P block;
			cin >> color >> dir >> block.first >> block.second;
			block.first--; block.second--;
			if (dir == 0 ){
				for (int col = 0; col < 4; ++col ){
					maze[block.second][block.first+col] = color;
					maze[block.second+1][block.first+col] = color;
				} // end for
			}else{
				for (int row = 0; row < 4; ++row ){
					maze[block.second+row][block.first] = color;
					maze[block.second+row][block.first+1] = color;
				} // end for
			} // end if
		} // end for
		queue<P> que;
		que.push (P(start.first, start.second ) );
		int search_color = maze[start.second][start.first];
		bool found = false;

		while (!que.empty() ){
			P curr = que.front(); que.pop();
			int x = curr.first;
			int y = curr.second;
			if (x == goal.first && y == goal.second ){
				found = true;
				break;
			} // end if
			used[y][x] = true;
			for (int k = 0; k < 4; ++k ){
				int nx = x + dir[k][0];
				int ny = y + dir[k][1];
				if (nx < 0 || nx >= w || ny < 0 || ny >= h )
					continue;
				if ((maze[ny][nx] == search_color) && !used[ny][nx] ){
					P next; next.first = nx; next.second = ny;
					que.push (next );
				} // end if
			} // end for
		} // end while

		if (found )
			cout << "OK" << endl;
		else
			cout << "NG" << endl;
	} // end loop
		
	return 0;
}

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

int F[102][102];

void setblock(int c, int d, int x, int y){
  if(d==0){
    for(int i=0;i<4;i++){
      F[y][x+i]=c;
      F[y+1][x+i]=c;
    }
  }else{
    for(int i=0;i<4;i++){
      F[y+i][x]=c;
      F[y+i][x+1]=c;
    }
  }

}

void DFS(int Y, int X, int c){
  if(F[Y][X]!=c)
    return;

  F[Y][X] = 6;

  DFS(Y-1, X  ,c);
  DFS(Y  , X+1,c);
  DFS(Y+1, X  ,c);
  DFS(Y  , X-1,c);
}

int main(){
  int w,h,xs,ys,xg,yg,n;
  while(true){
  cin >> w >> h;
  for(int i=0;i<h;i++){
   for(int k=0;k<w;k++){
     F[i][k]=-1;
   }
  }
  if(w==0&&h==0){
    break;
  }
  cin >> xs >> ys;
  cin >> xg >> yg;
  cin >> n;
  for(int i=0; i<n; i++){
    int c,d,x,y;
    cin >> c >> d >> x >> y;
    setblock(c,d,x,y);
  }

  DFS(ys,xs,F[ys][xs]);
  if(F[yg][xg]==6){
    cout << "OK" << endl;
  }else{
    cout << "NG" << endl;
  }
}
  return 0;
}