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>
#include<cstdio>
#include<cstring>
#include<stack>

using namespace std ;

int w ,h ,n ;
int sx ,sy ,gx ,gy ;

struct P
{
	int x ;
	int y ;
} ;

bool came[ 110 ][ 110 ] ;

int map[ 110 ][ 110 ] ;

void high( int x ,int y ,int c )
{
	for( int i = 0 ; i < 4 ; i++ )
	{
		for( int j = 0 ; j < 2 ; j++ )
			map[ y + i ][ x + j ] = c ;
	}

	return ;
}

void wide( int x ,int y ,int c )
{
	for( int i = 0 ; i < 2 ; i++ )
	{
		for( int j = 0 ; j < 4 ; j++ )
			map[ y + i ][ x + j ] = c ;
	}

	return ;
}

int main()
{
	while( cin >> w >> h )
	{
		if( w == h && !w )
			return 0 ;
		cin >> sx >> sy >> gx >> gy >> n ;
	memset( came ,false ,sizeof came ) ;
	memset( map ,0 ,sizeof map ) ;

	stack< P > st ;

	int c ,d ,x ,y ;
	int d1 ,d2 ;
	for( int k = 0 ; k < n ; k++ )
	{
		cin >> c >> d >> x >> y ;
		d ? high( x ,y ,c ) : wide( x ,y ,c ) ;
		/*if( d )
		{
			d1 = 2 ;
			d2 = 4 ;
		}
		else
		{
			d1 = 4 ;
			d2 = 2 ;
		}
		for( int i = 0 ; i < d1 ; i++ )
		{
			for( int j = 0 ; j < d2 ; j++ )
				map[ y + i ][ x + j ] = c ;
		}*/
	}

for( int i = 0 ; i <= h + 1 ; i++ )
{
	for( int j = 0 ; j <= w + 1 ; j++ )
		cout << map[ i ][ j ] ;
	cout << endl ;
}

	if( map[ sy ][ sx ] != map[ gy ][ gx ] )
	{
		printf( "NG\n" ) ;
		continue ;
	}

	c = map[ gy ][ gx ] ;

	for( int i = 1 ; i <= h ; i++ )
	{
		for( int j = 1 ; j <= w ; j++ )
		{
			if( c == map[ i ][ j ] )
				came[ i ][ j ] = true ;
		}
	}

	d = 1 ;
	P l ;
	l.x = sx ;
	l.y = sy ;
	st.push( l ) ;

	came[ sy ][ sx ] = false ;

	while( !st.empty() )
	{

		P p = st.top() ;
		st.pop() ;

		if( p.x == gx && p.y == gy )
		{
			d = 0 ;
			break ;
		}
		for( int i = 0 ; i < 4 ; i++ )
		{
			int mx[] = { 1 ,0 ,0 ,-1 } ;
			int my[] = { 0 ,1 ,-1 ,0 } ;
			int xx = p.x + mx[ i ] ;
			int yy = p.y + my[ i ] ;
			if( xx > 0 && x <= w && yy > 0 && yy <= h && came[ yy ][ xx ] )
			{
				came[ yy ][ xx ] = false ;
				l.x = xx ;
				l.y = yy ;
				st.push( l ) ;
			}
		}
	}
	d ? printf( "NG\n" ) : printf( "OK\n" ) ;

	}
}

C++: #include <bits/stdc++.h>

using namespace std;

#define MAX 101

int H, W, sx, sy, gx, gy;
int color, dir, field[MAX][MAX];

struct P{
  int x, y;
  P(int x, int y) : x(x), y(y) {}
};

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};

bool check(int y, int x){
  if(0 <= y && y < H && 0 <= x && x < W){
    return true;
  }
  return false;
}

void paint(int y, int x){
  if(!dir){
    for(int i = 0 ; i < 2 ; i++){
      for(int j = 0 ; j < 4 ; j++){
        field[y+i][x+j] = color;
      }
    }
  }else{
    for(int i = 0 ; i < 4 ; i++){
      for(int j = 0 ; j < 2 ; j++){
        field[y+i][x+j] = color;
      }
    }

  }
}

bool bfs(){
  bool used[MAX][MAX];
  memset(used, false, sizeof(used));
  used[sy][sx] = true;

  queue<P> Q;
  Q.push(P(sx, sy));

  while(!Q.empty()){
    P p = Q.front(); Q.pop();

    if(p.x == gx && p.y == gy){
      return true;
    }

    if(field[p.y][p.x] == -1) continue;

    for(int i = 0 ; i < 4 ; i++){
      int nx = p.x + dx[i];
      int ny = p.y + dy[i];

      if(!check(ny, nx) || used[ny][nx] || 
         field[p.y][p.x] != field[ny][nx]) continue;

      used[ny][nx] = true;
      Q.push(P(nx, ny));     
    }
  }

  return false;
}

int main(){
  int n, x, y;
  while(cin >> W >> H, (W | H)){
    memset(field, -1, sizeof(field));

    cin >> sx >> sy >> gx >> gy >> n;
    sy--, sx--, gy--, gx--;

    for(int i = 0 ; i < n ; i++){
      cin >> color >> dir >> x >> y;
      y--, x--;
      paint(y, x);
    }

    cout << (bfs() ? "OK" : "NG") << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int,int> pr;
int main() {
	// your code goes here
	int w,h;
	while(cin >> w >> h && w!=0){
	int xs,ys,xg,yg,n,m,s,sum,c,d,x,y;
	int dx[]={0,1,-1,0},dy[]={1,0,0,-1};
	cin >> xs>>ys>>xg>>yg >> n;
	bool map[102][102]={};	
	int color[102][102]={};
	queue<pr> q;
		color[xs][ys]=10;
		for(int i=0;i<n;i++){
			cin >> c >> d >>x >> y;
			for(int j=0;j<2+d*2;j++)
				for(int k=0;k<4-d*2;k++)
					color[x+k][y+j]=c;
		}
		map[xs][ys]=true;
		q.push(pr(xs,ys));
		while(!q.empty()){
			int f=q.front().first,s=q.front().second;
			q.pop();
			for(int i=0;i<4;i++){
					if(color[f][s]==color[f+dx[i]][s+dy[i]] && !map[f+dx[i]][s+dy[i]]){
						q.push(pr(f+dx[i],s+dy[i]));
						map[f+dx[i]][s+dy[i]]=true;
					}
			}
		}
		if(map[xg][yg])
		cout << "OK" << endl;
		else
		cout << "NG" << endl;
	}
	return 0;
}

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;
		for(int i=1; i<=100; i++)
			for(int j=1; j<=100; j++)
				F[j][i]=0;


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


		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=0; i<=w;i++)
				F[w+1][i]=6;

		for(int i=0; i<=h;i++)
				F[i][h+1]=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;

	}

}