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 C[102][102];
int ans;
int xg,yg;

void meiro(int W,int H,int c){
  if(W==xg && H==yg){
    ans = 1;
    return;
  }

  if(C[W][H]!=c){
    return;
  }

  C[W][H] = '.';

  meiro(W-1,H  ,c);
  meiro(W  ,H+1,c);
  meiro(W+1,H  ,c);
  meiro(W  ,H-1,c);
}

int main(){
  while(1){
    int w,h;
    cin >> w >> h;

    for(int i=0;i<w;i++){
      for(int j=0;j<h;j++){
        C[i][j] = 0;
      }
    }

    if(w==0 && h==0){
      break;
    }

    int xs,ys;
    cin >> xs >> ys;

    cin >> xg >> yg;

    int n;
    cin >> n;
    int c[n],d[n],x[n],y[n];

    int cbase;

    for(int i=0;i<n;i++){
      cin >> c[i] >> d[i] >> x[i] >> y[i];

      if(x[i]==xs &&y[i]==ys){
        cbase = c[i];
      }

      if(d[i]==0 && c[i]==cbase){
        for(int j=x[i];j<x[i]+4;j++){
          for(int k=y[i];k<y[i]+2;k++){
            C[j][k]=c[i];
          }
        }
      }
      if(d[i]==1 && c[i]==cbase){
        for(int j=x[i];j<x[i]+2;j++){
          for(int k=y[i];k<y[i]+4;k++){
            C[j][k]=c[i];
          }
        }
      }
    }
    ans = 0;
    meiro(xs,ys,cbase);

    if(ans == 1){
      cout << "OK" << endl;
    }
    if(ans != 1 || c[xs][ys] == 0){
      cout << "NG" << endl;
    }
  }

  return 0;
}

C++: #include <iostream>
int m[101][101];
int w,h,r,gx,gy;
void dfs(int x,int y,int c){
	if(!(x-gx)&&!(y-gy))r=1;
	if(x>0&&m[x-1][y]==c)m[x-1][y]=0,dfs(x-1,y,c);
	if(x<w&&m[x+1][y]==c)m[x+1][y]=0,dfs(x+1,y,c);
	if(y>0&&m[x][y-1]==c)m[x][y-1]=0,dfs(x,y-1,c);
	if(y<h&&m[x][y+1]==c)m[x][y+1]=0,dfs(x,y+1,c);
	return ;
}

int main(){
	//whÍ{[hÌå«³AnÍubNÌÂ
	int n,c,d,sx,sy,bx,by;
	
	while(std::cin>>w>>h,w&&h){
		for(int y=0;y<101;y++){
			for(int x=0;x<101;x++){
				m[x][y] = 0;
			}
		}
		r=0;
		std::cin>>sx>>sy;
		std::cin>>gx>>gy;
		std::cin>>n;
		for(int i=0;i<n;i++){
			std::cin>>c>>d>>bx>>by;
			int bw,bh;
			d?bw=2,bh=4:bh=2,bw=4;
			for(int y=by;y<=by+bh;y++){
				for(int x=bx;x<=bx+bw;x++){
					m[x][y] = c;
				}
			}
		}
		if(m[sx][sy])dfs(sx,sy,m[sx][sy]),m[sx][sy] = 0;
		if(r)std::cout<<"OK"<<std::endl;
		else{std::cout<<"NG"<<std::endl;}
	}
	return 0;
}

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

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

int w, h, xs, ys, xg, yg, G[MAX+2][MAX+2];

bool dfs(int x, int y, int color){
	if(color == 0 || G[x][y] != color) return false;
	if(x==xg&&y==yg) return true;
	G[x][y] = 0;
	for(int r=0; r<4; r++){
		if(dfs(x+dx[r], y+dy[r], color)) return true;
	}
	return false;
}

int main(void)
{
	int n, c, d, bx, by;
	while(cin>>w>>h, w&&h){
		for(int x=0; x<w+2; x++){
			for(int y=0; y<h+2; y++){
				G[x][y] = 0;
			}
		}
		cin >> xs >> ys >> xg >> yg >> n;
		for(int i=0; i<n; i++){
			cin >> c >> d >> bx >> by;
			for(int i=0; i<4; i++){
				for(int j=0; j<2; j++){
					if(d==0) G[bx+i][by+j] = c; 
					else G[bx+j][by+i] = c;
				}
			}
		}
		if(dfs(xs, ys, G[xs][ys])) cout << "OK" << endl;
		else cout << "NG" << endl;
	}
	return 0;
}

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


#define rep(i,n) for(int i = 0; i < n; i++)
#define MAX 50

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

typedef pair<int,int> P;
P S,G;
int w,h;
int maze[MAX][MAX];
int color;

bool check()
{
	queue<P> que;
	que.push(S);
	color = maze[S.second][S.first];

	int nx,ny;
	while(que.size()){
		P p = que.front();	que.pop();

		if(p == G)
			return true;

		rep(i,4){
			nx = p.first + dx[i], ny = p.second + dy[i];
			
			if(nx > -1 && ny > -1 && nx < w && ny < h &&
				maze[ny][nx] == color){
					maze[ny][nx] = -1;
					que.push(P(nx,ny));
			}

		}
	}


	
	return false;
}

int main(void)
{
	int n,c,d,x,y;

	while(cin >> w >> h, w){
		cin >> S.first >> S.second;
		cin >> G.first >> G.second;
		S.first = S.first / 2;
		S.second= S.second/ 2;

		cin >> n;

		rep(i,n){
			cin >> c >> d >> x >> y;
			x /= 2;
			y /= 2;
			x++;
			y++;

			if(d == 0)
				maze[y][x] = maze[y][x+1] = c;
			else
				maze[y][x] = maze[y+1][x] = c;
		}
		
		if(check())
			printf("OK\n");
		else
			printf("NG\n");
/*
		rep(i,y){
			rep(j,x)
				printf("%2d", maze[i][j]);
			cout << endl;
		}
	}
	*/
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
pair<int,int> pii;

int main() {
	int w,h,n;
	int xs,ys;
	int xg,yg;
	int c,d,x,y;
	int v[101][101];
	int u[101][101];
	int flag=0;
	int xx[4]={-1,0,1,0};
	int yy[4]={0,1,0,-1};
	int xxx,yyy;
while(cin>>w>>h,w||h){
	cin>>xs>>ys;
	cin>>xg>>yg;
	cin>>n;
	fill(v[0],v[101],0);
	fill(u[0],u[101],0);
	for (int i = 0; i < n; ++i) {
			cin>>c>>d>>x>>y;
			if(d==0){
				v[x][y]=c;	v[x+1][y]=c;	v[x+2][y]=c;	v[x+3][y]=c;
				v[x][y+1]=c;	v[x+1][y+1]=c;	v[x+2][y+1]=c;	v[x+3][y+1]=c;
			}
			if(d==1){
				v[x][y]=c;	v[x][y+1]=c;	v[x][y+2]=c;	v[x][y+3]=c;
				v[x+1][y]=c;	v[x+1][y+1]=c;	v[x+1][y+2]=c;	v[x+1][y+3]=c;
			}
	}
	queue<pair<int,int> > q;
	u[xs][ys]=1;
	int color=v[xs][ys];
	q.push(make_pair(xs,ys));
	while(!q.empty()){
		pii=q.front();q.pop();
		if(pii.first==xg&&pii.second==yg){flag=1;break;}
		for (int i = 0; i < 4; ++i) {
			xxx=xx[i]+pii.first;
			yyy=yy[i]+pii.second;
			if(xxx>=1&&xxx<=w&&yyy>=1&&yyy<=h&&u[xxx][yyy]!=1&&color==v[xxx][yyy]){
				u[xxx][yyy]=1;
				q.push(make_pair(xxx,yyy));
			}
		}
	}
	if(flag){cout<<"OK"<<endl;}
	else{cout<<"NG"<<endl;}
	flag=0;
}
	return 0;
}