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<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<tuple>
#include<utility>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<iomanip>
#include<map>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int INF = 1e9 + 7;
int dx[8] = { 1,0,-1,0,1,1,-1,-1 };
int dy[8] = { 0,1,0,-1,1,-1,1,-1 };

int h, w;
int sy, sx, gy, gx;
int n;
int s[105][105] = { 0 };
int color;

void dfs(int y, int x) {

	s[y][x] = 10;

	for (int i = 0; i < 4; i++) {
		int ny = y + dy[i];
		int nx = x + dx[i];

		if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == color) {
			dfs(ny, nx);
		}
	}

	return;
}



int main() {

	while (cin >> w >> h) {
		if (w == 0)break;


		cin >> sx >> sy >> gx >> gy;
		sx--, sy--, gx--, gy--;

		cin >> n;

		int c[200], d[200], x[200], y[200];

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

		for (int i = 0; i < n; i++) {
			if (d[i] == 0) {
				for (int j = 0; j < 4; j++) {
					for (int k = 0; k < 2; k++) {
						s[y[i] + k][x[i] + j] = c[i];
					}
				}
			}
			else {
				for (int j = 0; j < 2; j++) {
					for (int k = 0; k < 4; k++) {
						s[y[i] + k][x[i] + j] = c[i];
					}
				}

			}
		}

		color = s[sy][sx];

		dfs(sy, sx);


		

		if (s[gy][gx] == 10) {
			cout << "OK" << endl;
		}

		else {
			cout << "NG" << endl;
		}

		for (int i = 0; i < 105; i++) {
			for (int j = 0; j < 105; j++) {
				s[i][j] = 0;
			}
		}
	}

	return 0;
}

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

int w,h;
int sx,sy,gx,gy;
int field[104][104];

class Info{
public:
	int x,y;
};

bool bfd(void){
	int search = field[sy-1][sx-1];
	int dx[4] = {1,-1,0,0};
	int dy[4] = {0,0,1,-1};

	queue<Info> q;
	Info tmp = {sx-1,sy-1};

	if(search == 0){return false;}

	q.push(tmp);
	while(!q.empty()){
		tmp = q.front(); q.pop();

		if(tmp.x + 1 == gx && tmp.y + 1 == gy){
			return true;
		}

		field[tmp.y][tmp.x] = 0;
		for(int k=0;k<4;k++){
			int nx = tmp.x + dx[k];
			int ny = tmp.y + dy[k];

			if(nx>=0 && ny>=0 && nx<w && ny<h && field[ny][nx]==search){
				Info tmp2 = {nx,ny};
				q.push(tmp2);
			}
		}
	}

	return false;
}

void put_block(int c,int d,int x,int y){
	int i,j;
	int to_i=2,to_j=4;

	if(d == 1){to_i=4,to_j=2;}

	for(i=0;i<to_i;i++){
		for(j=0;j<to_j;j++){
			field[y+i-1][x+j-1] = c;
		}
	}
}

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

	while(cin>>w>>h && w){
		cin>>sx>>sy>>gx>>gy>>n;

		memset(field,0,sizeof(field));
		while(n--){
			cin>>c>>d>>x>>y;
			put_block(c,d,x,y);
		}

		if(bfd()){
			cout<<"OK\n";
		}
		else{
			cout<<"NG\n";
		}
	}

	return 0;
}

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

int F[102][102];

bool DFS(int X, int Y, int Xg, int Yg, int c){
	if(F[Y][X] != c){
		return false;
	}else if(F[Y][X] == c && X == Xg && Y == Yg){
		return true;
	}
	F[Y][X] = -1;
	if(DFS(X, Y - 1, Xg, Yg, c)){
		return true;
	}else if(DFS(X + 1, Y, Xg, Yg, c)){
		return true;
	}else if(DFS(X, Y + 1, Xg, Yg, c)){
		return true;
	}else if(DFS(X - 1, Y, Xg, Yg, c)){
		return true;
	}
	
	return false;
}

int main(){

while(true){
	int w, h;
	cin >> w >> h;
	if(w == 0 && h == 0){
		break;
	}
	int xs, ys, xg, yg;
	cin >> xs >> ys >> xg >> yg;
	int n;
	cin >> n;
	
	for(int i = 0; i < n; i++){
		int c, d, x, y;
		cin >> c >> d >> x >> y;
		if(d == 0){
			for(int X = x; X < x + 4; X++){
				for(int Y = y; Y < y + 2; Y++){
					F[Y][X] = c;
				}
			}
		}else if(d == 1){
			for(int X = x; X < x + 2; X++){
				for(int Y = y; Y < y + 4; Y++){
					F[Y][X] = c;
				}
			}
		}
	}
	if(DFS(xs, ys, xg, yg, F[xg][yg])){
		cout << "OK" << "\n";
	}else{
		cout << "NG" << "\n";#include<iostream>
using namespace std;

int F[102][102];

bool DFS(int X, int Y, int Xg, int Yg, int c){
	if(F[Y][X] != c){
		return false;
	}else if(F[Y][X] == c && X == Xg && Y == Yg){
		return true;
	}
	F[Y][X] = -1;
	if(DFS(X, Y - 1, Xg, Yg, c)){
		return true;
	}else if(DFS(X + 1, Y, Xg, Yg, c)){
		return true;
	}else if(DFS(X, Y + 1, Xg, Yg, c)){
		return true;
	}else if(DFS(X - 1, Y, Xg, Yg, c)){
		return true;
	}
	
	return false;
}

int main(){
bool isfirst = true;
while(true){
	if(isfirst){
		isfirst = false;
	}else{
		cout << endl;
	}
	int w, h;
	cin >> w >> h;
	if(w == 0 && h == 0){
		break;
	}
	int xs, ys, xg, yg;
	cin >> xs >> ys >> xg >> yg;
	int n;
	cin >> n;
	
	for(int i = 0; i < n; i++){
		int c, d, x, y;
		cin >> c >> d >> x >> y;
		if(d == 0){
			for(int X = x; X < x + 4; X++){
				for(int Y = y; Y < y + 2; Y++){
					F[Y][X] = c;
				}
			}
		}else if(d == 1){
			for(int X = x; X < x + 2; X++){
				for(int Y = y; Y < y + 4; Y++){
					F[Y][X] = c;
				}
			}
		}
	}
	if(DFS(xs, ys, xg, yg, F[xg][yg])){
		cout << "OK";
	}else{
		cout << "NG";
	}
}
	return 0;
}
	}
}
	return 0;
}