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 <bits/stdc++.h>
using namespace std;
typedef long long ll;

int b[100][100];
int vis[100][100];

int w, h;
bool in(int x, int y) {
	return 0 <= x && x < w && 0 <= y && y < h;
}


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(cin >> w >> h, w + h) {
		int xs, ys, xg, yg, n;
		cin >> xs >> ys >> xg >> yg >> n;
		xs--, ys--, xg--, yg--;

		memset(b, 0, sizeof b);
		for(int i = 0; i < n; i++) {
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			x--, y--;
			int H = 2, W = 4;
			if(d == 1) swap(H, W);
			for(int dx = 0; dx < W; dx++) {
				for(int dy = 0; dy < H; dy++) {
					b[y + dy][x + dx] = c;
				}
			}
		}

		if(b[ys][xs] != b[yg][xg]) {
			cout << "NG" << endl;
			return 0;
		}

		typedef pair<int, int> P;
		queue<P> q;
		q.push({ xs, ys });
		memset(vis, 0, sizeof vis);
		vis[ys][xs] = 1;
		while(q.size()) {
			int x = q.front().first, y = q.front().second;
			q.pop();
			int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
			for(int k = 0; k < 4; k++) {
				int nx = x + dx[k], ny = y + dy[k];
				if(in(nx, ny) && vis[ny][nx] == 0) {
					vis[ny][nx] = 1;
					q.push({ nx, ny });
				}
			}
		}
		if(vis[yg][xg]) {
			cout << "OK" << endl;
		}
		else {
			cout << "NG" << endl;
		}
	}
}

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

int map[MAX+2][MAX+2];
bool check[MAX+2][MAX+2];
int dx[4] = { -1,1,0,0};
int dy[4] = { 0,0,-1,1};

int main () {

  while ( true ) {

    queue<int> next;

    while( !next.empty() ) {
      next.pop();
    }
    
    int w,h;
    cin >> w >> h ;

    if ( w == 0 )
      break;

    int xs,ys;
    cin >> xs >> ys;
    
    int xg, yg;
    cin >> xg >> yg;

    int n;
    cin >> n;

    int c,d,x,y;

    for ( int i=0; i<h+2; i++ ) {
      for ( int j=0; j<w+2; j++ ) {
	map[i][j] = 0;
	check[i][j] = true;
      }
    }

    for ( int i=0; i<n; i++ ) {
      cin >> c >> d >> x >> y;
      
      //int count = 0;
      for ( int j=0; j<2; j++ ) {
	for ( int k=0; k<4; k++ ) {
	  //cout << count;
	  if ( d == 0 ) {
	    map[y+j][x+k] = c;
	  } else {
	    map[y+k][x+j] = c;
	  }
	}
      }
      //cout << endl;
    }
    next.push(y*1000 + x );

    int point;
    bool goal = false;

    int sc = map[ys][xs];

    /*
    for ( int i=0; i<=h; i++ ) {
      for ( int j=0; j<=w; j++ ) {
	cout << map[i+1][j+1];
      }
      cout << endl;
    }
    cout << endl;
    */
    if ( map[yg][xg] == sc ) {
	
      while ( !next.empty() ) {
	point = next.front();
	next.pop();

	check[point/1000][point%1000] = false;
	if (map[point/1000][point%1000] == 0 )
	  continue;

	if (point == yg*1000+xg) {
	  if ( sc == map[yg][xg] )
	    goal = true;
	  break;
	}
	for ( int i=0; i<4; i++ ) {
	  if ( check[point/1000+dy[i]][point%1000+dx[i]] ) {
	    check[point/1000+dy[i]][point%1000+dx[i]] = false;
	    if ( map[point/1000+dy[i]][point%1000+dx[i]] == sc ) {
	      next.push((point/1000+dy[i])*1000+point%1000+dx[i]);
	    }
	  }
	}
      }
    }

    if ( goal )
      cout << "OK\n";
    else
      cout << "NG\n";


  }


}

C++: #include <iostream>
using namespace std;
  
int F[102][102];
  
void DFS(int Y,int X,int c){
  if(F[Y][X] != c){
    return;
  }
  
  F[Y][X] = -1;
  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(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(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;
        }
      }
    }

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

}
}

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

bool dfs(int x,int y,int color);
int w,h;
int xs,ys;
int xg,yg;
int board[111][111];
int cheak[111][111];
int xd[]={0,-1,0,1};
int yd[]={-1,0,1,0};
int main(){
  int n;
  int c,d,x,y;
  int a[]={1,0,1,0,1,0,1};
  int b[]={0,1,1,2,2,3,3};
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;
    memset(board,0,sizeof(board));
    memset(cheak,0,sizeof(cheak));
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;
    xs--,ys--,xg--,yg--;
    for(int i=0;i<n;i++){
      cin >> c >> d >> x >> y;
      x--,y--;
      board[y][x]=c;
      for(int j=0;j<7;j++){
	if(d==0) board[y+a[j]][x+b[j]]=c;
	else board[y+b[j]][x+a[j]]=c;
      }
    }
    if(board[ys][xs]!=0){
      if(dfs(xs,ys,board[ys][xs])==true) cout << "OK" << endl;
      else cout << "NG" << endl;
    }
    else cout << "NG" << endl;
  }
}

bool dfs(int x,int y,int color){
  board[y][x]=0;
  if(x==xg && y==yg) return true;
  for(int i=0;i<4;i++){
    int xn=x+xd[i];
    int yn=y+yd[i];
    if(xn>=0 && xn<w && yn>=0 && yn<h && board[yn][xn]==color && board[yn][xn]!=0){
      if(dfs(xn,yn,board[yn][xn])==true) return true;
    }
  }
  return false;
}