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 <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <ctime>
#include <list>
#include <numeric>
#include <utility>
#include <ctime>
#define INF 100000000
#define LINF 9000000000000000000
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;

/*---------------------------------------------------*/

int maze[105][105];
int w,h,xs,ys,xg,yg;

bool bfs(){
  queue<int>que;
  que.push(xs);
  que.push(ys);
  while(que.size()){
    int x=que.front();que.pop();
    int y=que.front();que.pop();
    if(x==xg&&y==yg)return true;
    for(int i=0;i<4;i++){
      if(0<=x+dx[i]&&x+dx[i]<=w&&0<=y+dy[i]&&y+dy[i]<=h&&maze[y+dy[i]][x+dx[i]]==maze[ys][xs]){
	que.push(x+dx[i]);
	que.push(y+dy[i]);
	maze[y+dy[i]][x+dx[i]]=-1;
      }
    }
  }
  return false;
}

int main(){
  while(cin>>w>>h){
    if(w==0&&h==0)break;
    cin>>xs>>ys;
    cin>>xg>>yg;
    rep(i,h/2){
      rep(j,w/4){
	cin>>maze[i][j];
      }
    }
    if(bfs())cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
  }
  return 0;
}

C++: #include<iostream>

using namespace std ;

int w ,h ;
int gx ,gy ;
int sx ,sy ;
int way ;
int map[ 101 ][ 101 ] ;

int ans( int x ,int y )
{
	int mx[ 4 ] = { 1 ,0 ,0 ,-1 } ;
	int my[ 4 ] = { 0 ,1 ,-1 ,0 } ;
	map[ y ][ x ] = -1 ;
	if( x == gx && y == gy )
	{
		return 1 ;
	}
	for( int i = 0 ; i < 4 ; i++ )
	{
		if( map[ y + my[ i ] ][ x + mx[ i ] ] == way && x + mx[ i ] > 0 && y + my[ i ] > 0 )
		{
			if( ans( x + mx[ i ] ,y + my[ i ] ) == 1 )
			{
				return 1 ;
			}
		}
	}
	return 0 ;
}

int main()
{
	while( cin >> w >> h ,h )
	{
		int n ;
		int color ,muki ,x ,y ;
		cin >> sx >> sy ;
		cin >> gx >> gy ;
		cin >> n ;
		while( n )
		{
			n-- ;
			cin >> color >> muki >> x >> y ;
			if( muki )
			{
				for( int i = 0 ; i < 4 ; i++ )
				{
					for( int j = 0 ; j < 2 ; j++ )
					{
						map[ y + i ][ x + j ] = color ;
					}
				}
			}
			else
			{
				for( int i = 0 ; i < 2 ; i++ )
				{
					for( int j = 0 ; j < 4 ; j++ )
					{
						map[ y + i ][ x + j ] = color ;
					}
				}
			}
		}
		way = map[ sy ][ sx ] ;
		if( map[ gy ][ gx ] == way )
		{
			if( ans( sx ,sy ) )
			{
				cout << "OK" << endl ;
			}
			else
			{
				cout << "NA" << endl ;
			}
		}
		else
		{
			cout << "NA" << endl ;
		}
	}
	return 0 ;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen

using namespace std;
typedef pair<int, int> P;
int maze[100][100];
bool used[100][100];
int main()
{
	const int dir[][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
//	cut here before submit 
//	freopen ("testcase.block", "r", stdin );
	int w, h;

	while (cin >> w >> h && w && h ){
		memset (maze, 0, sizeof(maze ) );
		memset (used, false, sizeof (used ) );

		P start, goal;
		cin >> start.first >> start.second;
		start.first--; start.second--;
		cin >> goal.first >> goal.second;
		goal.first--; goal.second--;
		int n;
		cin >> n;
		for (int i = 0; i < n; ++i ){
			int color, dir;
			P block;
			cin >> color >> dir >> block.first >> block.second;
			block.first--; block.second--;
			if (dir == 0 ){
				for (int col = 0; col < 4; ++col ){
					maze[block.second][block.first+col] = color;
					maze[block.second+1][block.first+col] = color;
				} // end for
			}else{
				for (int row = 0; row < 4; ++row ){
					maze[block.second+row][block.first] = color;
					maze[block.second+row][block.first+1] = color;
				} // end for
			} // end if
		} // end for
		queue<P> que;
		que.push (P(start.first, start.second ) );
		int search_color = maze[start.second][start.first];
		bool found = false;

		while (!que.empty() ){
			P curr = que.front(); que.pop();
			int x = curr.first;
			int y = curr.second;
			if (x == goal.first && y == goal.second ){
				found = true;
				break;
			} // end if
			used[y][x] = true;
			for (int k = 0; k < 4; ++k ){
				int nx = x + dir[k][0];
				int ny = y + dir[k][1];
				if (nx < 0 || nx >= w || ny < 0 || ny >= h )
					continue;
				if ((maze[ny][nx] == search_color) && !used[ny][nx] ){
					P next; next.first = nx; next.second = ny;
					que.push (next );
				} // end if
			} // end for
		} // end while

		if (found )
			cout << "OK" << endl;
		else
			cout << "NG" << endl;
	} // end loop
		
	return 0;
}