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.h>
#include <stdio.h>
using namespace std;
int gx,gy,sx,sy;
int map[102][102];
bool flag[102][102];
int xx[]={3,1};
int yy[]={1,3};
int xv[]={0,1,0,-1};
int yv[]={1,0,-1,0};
void cal(int X,int Y){
	flag[X][Y]=1;
	for(int i=0;i<4;i++){
		if(map[X][Y]==map[X+xv[i]][Y+yv[i]]&&!flag[X+xv[i]][Y+yv[i]]){
			cal(X+xv[i],Y+yv[i]);
		}
	}
}

int main(){
	for(int w,h,n;cin>>w>>h,w;){
		memset(map,0,sizeof(map));
		memset(flag,0,sizeof(flag));
		cin>>sx>>sy;
		cin>>gx>>gy;
		for(cin>>n;n>0;n--){
			int c,d,x,y;
			cin>>c>>d>>x>>y;
			for(int X=x;X<=x+xx[d];X++){
				for(int Y=y;Y<=y+yy[d];Y++){
					map[X][Y]=c;
				}
			}
		}
		if(map[sx][sy])cal(sx,sy);
		if(flag[gx][gy])cout<<"OK"<<endl;
		else cout<<"NG"<<endl;
	}
	
	return 0;
}

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

int F[102][102];

void
DFS (int Y, int X, int A)
{
  if (F[Y][X] != A)
    return;

  F[Y][X] = 7;

  DFS (Y - 1, X, A);
  DFS (Y, X + 1, A);
  DFS (Y + 1, X, A);
  DFS (Y, X - 1, A);
}

int
main ()
{
  int w, h;
  int xs, ys;
  int xg, yg;
  int n;
  int c, d, x, y;

  while (true)
    {
      cin >> w >> h;
      if (w == 0 && h == 0)
	break;
      cin >> xs >> ys;
      cin >> xg >> yg;
      cin >> n;

      for (int i = 0; i < n; i++)
	{
	  cin >> c >> d >> x >> y;
	  if (d == 0)
	    {
	      for (int a = 0; a < 4; a++)
		{
		  for (int b = 0; b < 2; b++)
		    {
		      F[y + b][x + a] = c;
		    }
		}

	    }
	  else
	    {
	      for (int a = 0; a < 2; a++)
		{
		  for (int b = 0; b < 4; b++)
		    {
		      F[y + b][x + a] = c;
		    }
		}
	    }

	}


      if (F[ys][xs] == 0 || F[ys][xs] == 1 || F[ys][xs] == 2 || F[ys][xs] == 3
	  || F[ys][xs] == 4 || F[ys][xs] == 5)
	{
	  DFS (ys, xs, F[ys][xs]);
	}

      if (F[yg][xg] == F[ys][xs])
	{
	  cout << "OK" << endl;
	}
      else
	{
	  cout << "NG" << endl;
	}
   
    /*for (int y = 0; y < h; y++)
	{
	  for (int x = 0; x < w; x++)
	    {
	      cout << F[y][x];
	    }
         cout << endl;
	}*/
   
    for (int y = 0; y < h; y++)
	{
	  for (int x = 0; x < w; x++)
	    {
	      F[y][x] = 0;
	    }
	}

    
   
    }

  return 0;
}

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)return;
    if(!b.mass[y][x])return;
    b.mass[y][x]=-1;
    if(x==gx&&y==gy)goal_flag=true;
    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(){
  while(1){
    int h,w;
    int start_x,start_y;
    int goal_x,goal_y;
    int n;

    cin>>w>>h;
    if(w==0&&h==0)return 0;
    cin>>start_x>>start_y;
    cin>>goal_x>>goal_y;
    cin>>n;

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

    int c,d,x,y;
    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 <vector>
using namespace std;

int needleX[4] = {-1, 0, 1, 0};
int needleY[4] = {0, 1, 0, -1};

bool dfs(int x, int y, int goalx, int goaly,vector<vector<int> >& canGo){

    //??´??????????????????????????????true
    if(x == goalx && y == goaly){ return true; }

    //???????????°????????¢?´¢???????????????
    canGo[x][y] = false;

    //???????????????????????¢?´¢
    for(int i = 0; i < 4; i++){
        int nextx = x + needleX[i];
        int nexty = y + needleY[i];

        //?????????????????????
        if(canGo[nextx][nexty]){
            bool flag = dfs(nextx, nexty, goalx, goaly, canGo);
            if(flag){
                return true;
            }
        }
    }

    return false;
}


int block(int w, int h){

    //0 0??§??????
    if(w == 0 && h == 0){return 0;}

    //??????????????±??\???
    int xs, ys, xg, yg;
    int n;
    int c, d, inputX, inputY;
    vector<vector<int> > canGo(h + 2, vector<int>(w + 2, 0));
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;

    //??????????????????
    for(int i = 1; i <= n; i++){
        cin >> c >> d >> inputX >> inputY;
        if(d == 0){
            for(int j = 0; j < 4; j++){
                for(int k = 0; k < 2; k++){
                    canGo[inputY + k][inputX + j] = c;
                }
            }
        }else{
            for(int j = 0; j < 2; j++){
                for(int k = 0; k < 4; k++){
                    canGo[inputY + k][inputX + j] = c;
                }
            }
        }   
    }
    /*??????????????¨???
    cout << canGo[ys][xs] << endl;
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            cout << canGo[i][j];
        }
        cout << endl;
    }
    cout << endl;
    */

    //????????????bool????????¨?????????
    int load = canGo[ys][xs];
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            if(canGo[i][j] == load){
                canGo[i][j] = 1;
            }else{
                canGo[i][j] = 0;
            }
        }
    }
    
    // ??±???????????¢?´¢????????????????????????
    bool result = dfs(xs,ys,xg,yg,canGo);
    if(result){
        return 1;
    }else{
        return 2;
    }

    
}

int main(void){
    int i = 0;
    int array[30];
    int w, h;
    while(1){
        cin >> w >> h;
        array[i] = block(w, h);
        if(array[i] == 0){break;}
        i++;
    }
    for(int j = 0; j < i; j++){
        if(array[j] == 1){cout << "OK" << endl;}
        else {cout << "NG" << endl;}
    }

    return 0;
}