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 <queue>
#include <map>

using namespace std;

typedef pair<int, int> P;

int B[100][100];

bool bfs(int w, int h, int xs, int ys, int xg, int yg);

int main(int argc, char** argv)
{
	int w, h;
	int xs, ys;
	int xg, yg;
	int n;
	int c, d, x, y;

	while( 1 )
	{
		cin >> w;
		cin >> h;
		if(w==0 && h==0) break;
		
		cin >> xs;
		cin >> ys;
		cin >> xg;
		cin >> yg;
		cin >> n;

		for(int i=0; i<n; i++)
		{
			cin >> c; cin >> d; cin >> x; cin >> y;
			if(d==0)
			{
				for(int dx=0; dx<4; dx++)
				{
					for(int dy=0; dy<2; dy++)
					{
						B[x+dx][y+dy] = c;
					}
				}
			}
			else
			{
				for(int dx=0; dx<2; dx++)
				{
					for(int dy=0; dy<4; dy++)
					{
						B[x+dx][y+dy] = c;
					}
				}
			}
		}

		//for(int j=0; j<h; j++)
		//{
		//	for(int i=0; i<w; i++)
		//	{
		//		printf("%d ", B[i][j]);
		//	}
		//	printf("\n");
		//}

		cout << (bfs(w, h, xs, ys, xg, yg) ? "OK" : "NG") << endl;
	}

	return 0;
}

bool bfs(int w, int h, int xs, int ys, int xg, int yg)
{
	int x, y, c, xx, yy;
	int offset[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
	queue<P> q;
	P p;

	c = B[xs][ys];
	B[xs][ys] = 0;
	p.first = xs, p.second = ys;
	
	q.push(p);
	while(!q.empty())
	{
		p = q.front();
		q.pop();
		x = p.first;
		y = p.second;
		if(x==xg && y==yg) return true;
		B[x][y] = 0;

		for(int i=0; i<4; i++)
		{
			xx = x + offset[i][0];
			yy = y + offset[i][1];
			if(x>=0 && y>=0 && x<w && y<h)
			{
				if(B[xx][yy] == c)
				{
					p.first = xx;
					p.second = yy;
					q.push(p);
				}
			}
		}
	}

	return false;
}

C++: #include <queue>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){

	int w,h;
	int xs,ys;
	int xg,yg;
	int n;
	while(cin>>w>>h&&!(w==0&&h==0)){
		int field[101][101];
		for(int i = 0; i < 101; i++)
			fill(field[i],field[i]+101,0);

		cin>>xs>>ys;
		cin>>xg>>yg;
		xs--;ys--;xg--;yg--;
		cin>>n;
		for(int i = 0; i < n; i++){
			int c,d,x,y;
			cin>>c>>d>>x>>y;
			x--;y--;
			if(d==0){
				for(int j = y; j < 2+y; j++){
					for(int k = x; k < x + 4; k++){
						field[j][k]=c;
					}
				}
			}
			else{
				for(int j = y; j < 4+y; j++){
					for(int k = x; k < x + 2; k++){
						field[j][k]=c;
					}
				}
			}
		}
		// bfs
		int c=field[ys][xs];

		if(c==0){
			cout<<"NG"<<endl;
			continue;
		}
		queue<pair<int,int> > *prv=new queue<pair<int,int> >();
		queue<pair<int,int> > *nxt=new queue<pair<int,int> >();

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

		bool passed[101][101];
		for(int i = 0; i < 101; i++)
			fill(passed[i],passed[i]+101,false);

		prv->push(make_pair(ys,xs));

		bool fin=false;
		while(prv->size()){
			while(prv->size()){
				pair<int,int> p = prv->front();
				prv->pop();
				if(p.first==yg&&p.second==xg){
					fin=true;
					break;
				}

				if(passed[p.first][p.second])
					continue;
				passed[p.first][p.second]=true;
				for(int i = 0; i < 4; i++){
					int ny=p.first+dy[i];
					int nx=p.second+dx[i];
					if(ny>=0&&nx>=0&&ny<h&&nx<w&&field[ny][nx]==c&&!passed[ny][nx]){
						nxt->push(make_pair(ny,nx));
					}
				}
			}
			if(fin)
				break;
			swap(prv,nxt);
		}
		if(fin){
			cout<<"OK"<<endl;
		}
		else
			cout<<"NG"<<endl;
		delete prv,nxt;
	}

	return 0;
}

C++: #include <iostream>

using namespace std;

int map[101][101];
int xg, yg;
int w, h;
int col;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
bool flag;

void dfs(int x, int y)
{
  map[y][x] = 0;
  
  if (x == xg && y == yg) {
    flag = true;
    return ;
  }
  
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    
    if (nx < 0 || ny < 0 || nx >= w || ny >= h)
      continue;
    else if (map[ny][nx] == col)
      dfs(nx, ny);
    }
}

void setblock(int c, int d, int bx, int by)
{
  int wlim = 4, hlim = 2;
  
  if (d == 1) {
    wlim = 2;
    hlim = 4;
  }
  
  for (int y = 0; y < hlim; y++)
    for (int x = 0; x < wlim; x++)
      map[by + y][bx + x] = c;
}

 
int main()
{
  while (cin >> w >> h, w || h) {
    int xs, ys;
    int n;
    
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++)
                map[y][x] = 0;
    
    cin >> xs >> ys >> xg >> yg >> n;
    
    xs--, ys--, xg--, yg--;
    for (int i = 0; i < n; i++) {
      int c, d, x, y;
             
      cin >> c >> d >> x >> y;
      x--, y--;            
      setblock(c, d, x, y);
    }
    col = map[ys][xs];
    flag = false;
    if (col)
      dfs(xs, ys);
 
    if (flag)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  
    return 0;
}

C++: #include <iostream>
using namespace std;
  
int F[105][105];
int xg,yg;
  
  
void DFS(int Y,int X,int c){
  if(F[Y][X] != c){
    return;
  }
  
  F[Y][X] = -2000;
  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,n;
  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0)
      break;

      
    cin >> xs >> ys >> xg >> yg >> n;
    int c[n],d[n],x[n],y[n],ca;
    for(int i=0;i<n;i++){
      cin >> c[i] >> d[i] >> x[i] >> y[i];
    }
    for(int i=0;i<n;i++){
      if(c[i] == 0)
        ca = 10
      else if(i == n-1)
        c = 0:
    }
    for(int i=0; i<102; i++){
          for(int j=0; j<102; j++){
            F[i][j]=ca;
          }
    }
    for(int i=0;i<n;i++){
      if(d[i] == 0){
        for(int j=0;j<4;j++){
          F[x[i]+j][y[i]] = c[i];
          F[x[i]+j][y[i]+1] = c[i];
        }
      } else {
        for(int j=0;j<4;j++){
          F[x[i]][y[i]+j] = c[i];
          F[x[i]+1][y[i]+j] = c[i];
        }
      }
    }
 
  DFS(xs,ys,F[xs][ys]);
  
  if(F[xg][yg] == -2000){
    cout << "OK" << endl;
  } else {
    cout << "NG" <<endl;
  }
  }
}