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;

const int size = 110;
int board[size][size];
int visited[size][size];
int xblocks[2][8] = { {0, 1, 2, 3, 0, 1, 2, 3},
		      {0, 0, 0, 0, 1, 1, 1, 1} };
int yblocks[2][8] = { {0, 0, 0, 0, 1, 1, 1, 1},
		      {0, 1, 2, 3, 0, 1, 2, 3} };
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main() {
  int w,h,xs,ys,xg,yg,n,c,d,x,y;
  while(cin>>w>>h, w|h) {
    for(int i=0; i<size; ++i)
      for(int j=0; j<size; ++j) {
	board[i][j] = 0;
	visited[i][j] = 0;
      }

    cin>>xs>>ys;
    cin>>xg>>yg;
    cin>>n;
    for(int i=0; i<n; ++i) {
      cin>>c>>d>>x>>y;
      for(int j=0; j<8; ++j)
	board[x+xblocks[d][j]][y+yblocks[d][j]] = c;
    }

    /*
    for(int i=1; i<h; ++i) {
      for(int j=1; j<w; ++j) {
	cout<<board[i][j]<<" ";
      }
      cout<<endl;
    }
    */

    queue<pair<int,int> > Q;
    Q.push(pair<int,int>(ys,xs));
    while(!Q.empty()) {
      pair<int,int> pos = Q.front(); Q.pop();
      int nx = pos.first,ny = pos.second;
      if(visited[ny][nx]) continue;
      visited[ny][nx] = 1;
      if(nx == yg && ny == xg) {
	cout<<"OK"<<endl;
	goto NEXT;
      }

      for(int i=0; i<4; ++i) {
	if(nx+dx[i] > 0 && nx+dx[i] <= w &&
	   ny+dy[i] > 0 && ny+dy[i] <= h &&
	   board[ny][nx] == board[ny+dy[i]][nx+dx[i]])
	  Q.push(pair<int,int>(nx+dx[i],ny+dy[i]));
      }
    }
    cout<<"NG"<<endl;
  NEXT:
    ;
  }
}

C++: #include <iostream>
using namespace std;
   
int F[102][102];
int xg,yg,w,h;
   
   
void DFS(int Y,int X,int c){
  //if(Y < 1 || X < 1)
    //return;
  if(F[Y][X] != c){
    return;
  }
   
  F[Y][X] = -1;
  DFS(Y+1,X  ,c);
  DFS(Y-1  ,X,c);
  DFS(Y,X+1  ,c);
  DFS(Y  ,X-1,c);
}
   
   
int main(){
  int xs,ys,n;
  while(1){
    cin >> w >> h;
    if(w == 0 && h == 0)
      break;
for(int i=0; i<102; i++){
      for(int j=0; j<102; j++){
        F[i][j] = 0;
      }
    }
       
    cin >> xs >> ys >> xg >> yg >> n;
   
   
    for(int i=0;i<n;i++){
      int c,d,x,y;
      cin >> c >> d >> x >> y;
      if(c == 0)
         c = 1919810;
      if(d == 0){
        for(int j=0;j<4;j++){
          F[x+j][y] = c;
          F[x+j][y+1] = c;
        }
      } else {
        for(int j=0;j<4;j++){
          F[x][y+j] = c;
          F[x+1][y+j] = c;
        }
      }
    }
  DFS(xs,ys,F[xs][ys]);
   
  if(F[xg][yg] == -1){
    cout << "OK" << endl;
  } else {
    cout << "NG" <<endl;
  }
  }
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int w,h,sx,sy,gx,gy,d[100][100],dx[4]={1,0,-1,0},dy[4]={0,1,0,-1},C;
int field[101][101];
const int INF=9999999;
typedef pair<int,int> P;

void bfs(){
	queue<P> que;
	for(int i=0;i<h;i++)for(int j=0;j<w;j++)d[i][j]=INF;
	que.push(P(sy,sx));
	d[sy][sx]=0;
	while(!que.empty()){
		P p=que.front(); que.pop();
		if(p.first==gy&&p.second==gx)break;
		for(int i=0;i<4;i++){
			int ny=p.first+dx[i],nx=p.second+dy[i];
			if(0<=ny&&ny<h&&0<=nx&&nx<w&&field[ny][nx]==C&&d[ny][nx]==INF){
			que.push(P(ny,nx));
			d[ny][nx]=d[p.first][p.second]+1;
			}
		}
	}
	if(d[gy][gx]==INF)cout<<"NG"<<endl;
	else cout<<"OK"<<endl;
}

int main() {
	while(cin>>h>>w){
		if(!h)return 0;
		int n,c,di,x,y;
		cin>>sx>>sy>>gx>>gy>>n;
		for(int i=0;i<h;i++)for(int j=0;j<w;j++)field[i][j]=0;
		for(int i=0;i<n;i++){
			cin>>c>>di>>x>>y;
			for(int j=y;j<=y+2+di*2;j++)for(int k=x;k<=x+2+!di*2;k++)field[j][k]=c;
		}
		C=field[1][1];
		bfs();
	}
}

C++: #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int, int> P;

const int INF = (int)1e9;

int board[128][128];

int main()
{
    int w, h;

    while (scanf("%d %d", &w, &h), w){
        int xs, ys;
        int xg, yg;
        scanf("%d %d", &xs, &ys);
        scanf("%d %d", &xg, &yg);

        int n;
        scanf("%d", &n);
        memset(board, 0, 128 * 128);
        while (n-- > 0){
            int c, d, x, y;
            scanf("%d %d %d %d", &c, &d, &x, &y);

            board[y][x] = board[y][x + 1] = c;
            board[y + 1][x] = board[y + 1][x + 1] = c;
            if (d == 0){
                board[y][x + 2] = board[y][x + 3] = c;
                board[y + 1][x + 2] = board[y + 1][x + 3] = c;
            }
            else {
                board[y + 2][x] = board[y + 3][x] = c;
                board[y + 2][x + 1] = board[y + 3][x + 1] = c;
            }
        }

        queue<P> que;
        bool flag = false;
        int color = board[ys][xs];

        if (color == 0){
            puts("NA");
            continue;
        }
        for (que.push(P(xs, ys)); que.size(); que.pop()){
            P p = que.front();
            int x = p.first;
            int y = p.second;

            if (board[y][x] != color) continue;

            if (x == xg && y == yg){
                flag = true;
                break;
            }
            board[y][x] = 0;
            que.push(P(x + 1, y));
            que.push(P(x - 1, y));
            que.push(P(x, y + 1));
            que.push(P(x, y - 1));
        }

        puts(flag ? "OK" : "NG");
    }

    return 0;
}