Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 49690 Accepted Submission(s): 12481

**The Description Problem**

"Lianliankan" I believe many people have played. I never played it does not matter, here I tell you about the rules of the game: In a chessboard, put a lot of pieces. If a two identical pieces can be connected together (this line can not pass through the other pieces), and the number of times of no more than two turning line, then the two pieces can be eliminated by a line on the board. I am sorry, because I had not played Lianliankan, consulted students, the connection can not go around from the outside, but in fact this is wrong. Now lead to disaster, it is only the wrong, and can not bypass connections from the periphery.

Players click on the mouse has two pieces, trying to eliminate them, and then the game's background to determine the two squares can not be eliminated. Now your task is to write the daemon.

**Input**

input multiple sets of data. The first line of each data set has two positive integers n, m (0 <n < = 1000,0 <m <1000), respectively, the number of rows and columns of the board. In the next n lines, each line has a non-negative integer m chessboard checkered distribution described. 0 indicates that this position is not a pawn, a positive integer representing the type of piece. The next line is a positive integer q (0 <q <50) , represents the q-th interrogation below. Q in the next row, each row has four positive integers x1, y1, x2, y2, x1 represents interrogation of row and first column pieces y1 x2 y2 row piece can eliminate columns. n = 0, m = 0, the end of the input.

Note: Has no relationship between the inquiry, are directed at the current state!

**Output**

of each set of input data corresponding to one line of output. If we can eliminate the output "YES", then the output can not be "NO".

**Sample Input**

3 4

1 2 3 4

0 0 0 0

4 3 2 1

4

1 1 3 4

1 1 2 4

1 1 3 3

2 1 2 4

3 4

0 1 4 3

0 2 4 1

0 0 0 0

2

1 1 2 4

1 3 2 3

0 0

**Sample Output**

YES

NO

NO

NO

NO

YES

**Problem link** : HDU1175 lianliankan

**problems outlined** :( slightly)

**Analysis** :

Lianliankan calculation games, first given the size of the disk and the piece number, number 0 indicates a blank.

To solve with DFS, simple question, do not explain.

**Program description** :( omitted)

**reference links** :( slightly)

**Inscription** :( omitted)

AC C ++ language program is as follows:

```
/* HDU1175 连连看 */
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int DN = 4;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
const int N = 1000 + 1;
int maze[N][N], vis[N][N];
int n, m, q, sx, sy, ex, ey, flag;
// DFS，dir为方向（初始为-1，0-3），turns为目前转向次数
void dfs(int x,int y, int dir, int turns)
{
if(turns > 2) return; // 转折多于２次
if(turns == 2 && x != ex && y != ey) return; // 已经转折２次，跟目标不在一条线
for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx <1 || nx > n || ny < 1 || ny >m || vis[nx][ny]) continue; // 越界或已经走过
if(nx == ex && ny == ey) {
flag = 1;
return;
} else if(maze[nx][ny] == 0) {
vis[nx][ny] = 1;
if(dir == -1 || dir == i) // 初始走或同方向
dfs(nx, ny, i, turns);
else
dfs(nx, ny, i, turns + 1);
vis[nx][ny]=0;
}
}
return;
}
int main()
{
while(~scanf("%d%d", &n, &m) && (n || m)) {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d", &maze[i][j]);
scanf("%d", &q);
while(q--) {
memset(vis,0,sizeof(vis));
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
flag = 0;
if(maze[sx][sy] == maze[ex][ey] && maze[sx][sy])
dfs(sx, sy, -1, 0); // 初始方向-1，转弯次数0
printf(flag ? "YES\n" : "NO\n");
}
}
return 0;
}
```