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<stdio.h>
#include<string.h>
void haichi(void);
int Block(int,int);
int c,d,x,y,xg,yg,w,h,M,Flag;
int MAP[101][101],MF[101][101];
int main(void)
{
	int xs,ys,n;
	while(1){
		Flag=0;
		memset(MAP,0,sizeof(MAP));
		memset(MF,0,sizeof(MF));
		scanf("%d %d",&w,&h);
		if(w == 0 && h == 0) break;
		scanf("%d %d %d %d",&xs,&ys,&xg,&yg);
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			haichi();
		}
		/*for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				printf("%d",MAP[i][j]);
			}
			printf("\n");
		}*/
		M = MAP[ys][xs];//
		if(Block(xs,ys))
			printf("OK\n");
		else
			printf("NG\n");
	}
	return 0;
}

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

int Block(int X,int Y)
{
	
	//printf("%d",M);
	printf("%d %d\n",X,Y);
	if(X == xg && Y == yg)
		return 1;
	if(MAP[Y][X] != M || MF[Y][X] == 1 || X < 1 || Y < 1 || X > w || Y > h) return 0;
	MF[Y][X] = 1;
	return Block(X,Y-1) || Block(X,Y+1) || Block(X-1,Y) || Block(X+1,Y);
}

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

struct board{
  int mass[102][102];
};

bool goal_flag;
void DFS(int x,int y,int gx,int gy,board b,int c){
    if(b.mass[y][x]!=c || !b.mass[y][x])return;
    b.mass[y][x]=0;
    if(x==gx&&y==gy){goal_flag=true;return;}
    DFS(x+1,y,gx,gy,b,c);
    DFS(x-1,y,gx,gy,b,c);
    DFS(x,y+1,gx,gy,b,c);
    DFS(x,y-1,gx,gy,b,c);
}

int main(){
  int h,w;
  int start_x,start_y;
  int goal_x,goal_y;
  int n;
  int c,d,x,y;
  while(1){
    cin>>w>>h;
    if(w==0&&h==0)break;
    cin>>start_x>>start_y;
    cin>>goal_x>>goal_y;
    cin>>n;

    board b;
    for(int i=0;i<h+2;i++){
      for(int j=0;j<w+2;j++){
        b.mass[i][j]=0;
      }
    }

    for(int i=0;i<n;i++){
        cin>>c>>d>>x>>y;
        for(int j=0;j<2;j++){
          for(int k=0;k<4;k++){
            if(d==0){b.mass[y+j][x+k]=c;}
            else if(d==1){b.mass[y+k][x+j]=c;}
          }
        }
    }
    goal_flag=false;
    DFS(start_x,start_y,goal_x,goal_y,b,b.mass[start_y][start_x]);
    if(goal_flag)cout<<"OK";
    else cout<<"NG";
    cout<<endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <stack>
#include <list>

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,b,n) for(int i=b;i<n;i++)
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

const float PI = 3.1415926;
static const int INFTY = (1<<30);

using namespace std;

int B[102][102];
int M[102][102];
int w, h, xs, ys, xg, yg, n, res;
 
void bfs(int x, int y, int c) {
  if(x <= 0 || y <= 0 || x > w || y > h)
    return;
  if(B[x][y] != c)
    return;
  if(M[x][y] == 1) {
    return;
  } else {
    M[x][y] = 1;
  }
  if(x == xg && y == yg) {
    res = 1;
    return; 
  }
  bfs(x+1, y, c);
  bfs(x, y+1, c);
  bfs(x-1, y, c);
  bfs(x, y-1, c);
}

int main() {
  while(cin >> w >> h, w) {
    // ?????????
    rep(i, w) {
      rep(j, h) {
        B[i][j] = 0;
        M[i][j] = 0;
      }
    }
    res = 0;
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;
    // ??????????????????
    rep(i, n) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      
      B[x][y] = c;
      B[x+1][y] = c;
      B[x][y+1] = c;
      B[x+1][y+1] = c;
      if ( d == 0 ) {
        B[x+2][y] = c;
        B[x+3][y] = c;
        B[x+2][y+1] = c;
        B[x+3][y+1] = c;
      } else {
        B[x][y+2] = c;
        B[x][y+3] = c;
        B[x+1][y+2] = c;
        B[x+1][y+3] = c;
      }        
    }
    bfs(xs, ys, B[xs][ys]);
    if(res)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

bool dfs(int xs,int ys,int xg,int yg,vector<vector<bool> >& canGo){
canGo[xs][ys]=false;
if(xs==xg&&ys==yg)return true;
if(canGo[xs+1][ys]){if(dfs(xs+1,ys,xg,yg,canGo))return true;}
if(canGo[xs-1][ys]){if(dfs(xs-1,ys,xg,yg,canGo))return true;}
if(canGo[xs][ys+1]){if(dfs(xs,ys+1,xg,yg,canGo))return true;}
if(canGo[xs][ys-1]){if(dfs(xs,ys-1,xg,yg,canGo))return true;}
return false;
}
int main(void){
int w,h,xs,ys,xg,yg,n;
bool ans,endflag;
vector<vector<bool> > blockmap;
while(true){
cin>>w>>h;
if(w==0&&h==0)break;
cin>>xs>>ys>>xg>>yg>>n;
blockmap=vector<vector<bool> >(w+2,vector<bool>(h+2,false));
for(int i=0;i<n;i++){
    int c,d,x,y;
    cin>>c>>d>>x>>y;
    if(c==2){
        blockmap[x+1][y+1]=true;
        blockmap[x+2][y+1]=true;
        blockmap[x+1][y+2]=true;
        blockmap[x+2][y+2]=true;
        if(d==0){
            blockmap[x+3][y+1]=true;
            blockmap[x+3][y+2]=true;
            blockmap[x+4][y+1]=true;
            blockmap[x+4][y+2]=true;
        }
        else {
            blockmap[x+1][y+2]=true;
            blockmap[x+2][y+3]=true;
            blockmap[x+1][y+4]=true;
            blockmap[x+2][y+4]=true;
        }
    }
}
ans=dfs(xs+1,ys+1,xg+1,yg+1,blockmap);
if(ans)cout<<"OK"<<endl;
else cout<<"NG"<<endl;
}
return 0;
}

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