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<cstdio>
using namespace std;
int w,h,xs,ys,xg,yg,n;
int c,d,x,y,ans=0,f=0;
int map[101][101]={0},v[101][101]={0};
int root(int x,int y,int vmap[][101]){
	int mx[]={0,1,0,-1},fx;
	int my[]={1,0,-1,0},fy;
	if(x==xg&&y==yg){
		f=1;
		return f;
	}
	for(int i=0;i<4;i++){
		fx=x+mx[i];
		fy=y+my[i];
		if(map[fy][fx]==map[xs][ys]&&fy>0&&fy<=h&&fx>0&&fx<=w&&vmap[fy][fx]==0){
			vmap[y][x]=1;
			return root(fx,fy,vmap);
		}
	}
	return f;
}
int main(){
	scanf("%d %d",&w ,&h);
	while(w!=0&&h!=0){
		scanf("%d %d %d %d %d",&xs ,&ys ,&xg ,&yg ,&n);
		for(int i=0;i<n;i++){
			scanf("%d %d %d %d",&c ,&d ,&x ,&y);
			if(d==0){
				for(int i=x;i<x+4;i++){
					for(int e=y;e<y+2;e++){
						map[e][i]=c;
					}
				}
			}
			if(d==1){
				for(int i=x;i<x+2;i++){
					for(int e=y;e<y+4;e++){
						map[e][i]=c;
					}
				}
			}
		}
		/*for(int i=0;i<w;i++){
			for(int i1=0;i1<h;i1++){
				printf("%d ",map[i][i1]);
			}printf("\n");
		}*/
		x=xs;
		y=ys;
		ans=root(x,y,v);
		if(ans==1){
			printf("OK\n");
		}
		else{
			printf("NG\n");
		}
		for(int i=0;i<101;i++){
			for(int i1=0;i1<101;i1++){
				map[i][i1]=0;
				v[i][i1]=0;
			}
		}
		f=0;
		ans=0;
		scanf("%d %d",&w ,&h);
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int w,h,xs,ys,xg,yg,n,ban[111][111]={{0},{0}};
bool visited [101][101];
const int START=4545191;
const int GOAL=454545;

	bool dfs(int x,int y){
		//cout<<x<<" "<<y<<endl;
		if(xg==x&&yg==y)return true;
		if(ban[xs][ys]==0)return false;
		if(ban[x+1][y]==ban[xs][ys]&&visited[x][y]==false){
			visited[x][y]=true;
			if(dfs(x+1,y))return true;
		}
		if(ban[x][y+1]==ban[xs][ys]&&visited[x][y]==false){
			visited[x][y]=true;
			if(dfs(x,y+1))return true;
		}
		if(x>=2&&ban[x-1][y]==ban[xs][ys]&&visited[x][y]==false){
			visited[x][y]=true;
			if(dfs(x-1,y))return true;
		}
		if(y>=2&&ban[x][y-1]==ban[xs][ys]&&visited[x][y]==false){
			visited[x][y]=true;
			if(dfs(x,y-1))return true;
		}
		return false;
	}


int main(){

	while(cin>>w>>h&&w&&h){
	cin>>xs>>ys>>xg>>yg>>n;
	for(int i=0;i<101;i++)
	for(int j=0;j<101;j++)ban[i][j]=0,visited[i][j]=false;
	
	for(int i=0;i<n;i++){
		int c,d,x,y;
		cin>>c>>d>>x>>y;
		if(!d){
			for(int i=x;i<x+4;i++){
				for(int j=y;j<y+2;j++){
					ban[i][j]=c;
				}
			}
		}
		else{
		for(int i=x;i<x+2;i++){
				for(int j=y;j<y+4;j++){
					ban[i][j]=c;
				}
			}
		}
	}
	
	if(dfs(xs,ys))cout<<"OK"<<endl;
	else cout<<"NG"<<endl;
	}
	return 0;
	}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define MAX_W 100
#define MAX_H 100

int board[MAX_H + 1][MAX_W + 1];
bool ans = false;

void swapAB(int a[], int b[]);
void resetBoard();
void setBoard(int c, int d, int x, int y);
void loop(int xn, int yn, int xg, int yg, int c);
void disp(int w, int h);

int main(){
    int w, h;
    while(cin >>w >>h){
        if(w == 0 && h == 0) break;
        int xs, ys, xg, yg, n;
        resetBoard();
        ans = false;
        cin >>xs >>ys >>xg >>yg >>n;
        for(int i = 0; i < n; i++){
            int c, d, x, y;
            cin >>c >>d >>x >>y;
            setBoard(c, d, x, y);
        }
//        disp(w, h);
        if(board[ys][xs] == 0){
            cout <<"NG" <<endl;
        } else{
            loop(xs, ys, xg, yg, board[ys][xs]);
            if(ans) cout <<"OK" <<endl;
            else cout <<"NG" <<endl;
        }
    }
    return 0;
}   

void swapAB(int a[], int b[]){
    for(int i = 0; i < 8; i++){
        int tmp = a[i];
        a[i] = b[i];
        b[i] = tmp;
    }
}

void resetBoard(){
    for(int i = 0; i <= MAX_H; i++){
        for(int j = 0; j <= MAX_W; j++){
            board[i][j] = 0;
        }
    }
}

void setBoard(int c, int d, int x, int y){
    int xm[] = {0, 1, 2, 3, 0, 1, 2, 3}, ym[] = {0, 0, 0, 0, 1, 1, 1, 1};
    if(d == 1) swapAB(xm, ym);
    for(int i = 0; i < 8; i++){
        int tmpx = x + xm[i], tmpy = y + ym[i];
        if(tmpx > 0 || tmpy > 0 || tmpx <= MAX_W || tmpy <= MAX_H){
            board[tmpy][tmpx] = c;
        }
    }
}
void loop(int xn, int yn, int xg, int yg, int c){
    if(xn == xg && yn == yg){
        ans = true;
    } else{
        board[yn][xn] = 0;
        int xm[] = {0, 1, 0, -1}, ym[] = {-1, 0, 1, 0};
        for(int i = 0; i < 4; i++){
            int tmpx = xn + xm[i], tmpy = yn + ym[i];
            //            cout <<"tmpx = " <<tmpx <<", tmpy = " <<tmpy <<endl;
            if(tmpx > 0 && tmpy > 0 && tmpx <= MAX_W && tmpy <= MAX_H && board[tmpy][tmpx] == c){
                loop(tmpx, tmpy, xg, yg, c);
            }
        }
    }
}

void disp(int w, int h){
    for(int i = 0; i <= h; i++){
        for(int j = 0; j <= w; j++){
            cout <<board[i][j];
        }
        cout <<endl;
    }
}

//0, 01:15:08

C++: #include <iostream>

using namespace std;

int w, h;
int xg, yg;
int data[102][102];
int dx[]={0, 1, 0, -1};
int dy[]={-1, 0, 1, 0};
bool dfs(int, int, int);

main(){
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;
    int xs, ys, n;
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;
    for(int i=0;i<=h+1;i++){
      for(int j=0;j<=w+1;j++){
	data[i][j]=0;
      }
    }
    for(int i=0;i<n;i++){
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if(d==0){
	for(int i=0;i<2;i++){
	  for(int j=0;j<4;j++){
	    data[i+y][j+x]=c;
	  }
	}
      }else{
	for(int i=0;i<4;i++){
	  for(int j=0;j<4;j++){
	    data[i+y][j+x]=c;
	  }
	}
      }
    }
    if(dfs(ys, xs, data[ys][xs])){
      cout << "OK" << endl;
    }else{
      cout << "NG" << endl;
    }
  }
  return 0;
}

bool dfs(int y, int x, int c){
  if(c==0 || data[y][x]!=c) return false;
  if(y==yg && x==xg) return true;
  data[y][x]=0;
  for(int i=0;i<4;i++){
    if(dfs(y+dy[i], x+dx[i], c)) return true;
  }
  return false;
}