题意:
就是连连看 嗯 跟平时玩的游戏不同的就是 不能从外边连。。。 0代表空
思路:
DFS 深度优先搜索。。从第一个开始 向四个方向搜索。如果最后一个不等于第一个 或者说最后一个和第一个都是0 就直接输出NO了 也不用搜索了。能直着走就直着走到头 如果不能直着走再考虑转向问题。。虽然做了很长时间,,调试很麻烦 但是 题目思路还是比较清晰地 比较简单 就是麻烦 小半天就搭在写代码上了。。。。。
#include "stdafx.h"
#include<fstream>
#include<iostream>
#include<queue>
using namespace std;
int map[1010][1010];
int cnt_map[1010][1010];
int dir[4][2] = { { 1,0 },{ 0,1 },{ -1,0 },{ 0,-1 } };
int s_x, s_y;
int e_x, e_y;
int flag, n, m;
struct node
{
int cnt;
int x, y;
int pre_dir;
}s_pos;
bool cheak(int x, int y)
{
if (x >= 1 && x <= n&&y >= 1 && y <= m && (map[x][y] == 0 || x == e_x&&y == e_y))
return true;
return false;
}
void dfs(int x, int y, int cnt, int pre_dir)
{
if (flag || cnt>3) return;
{
flag = 1;
return;
}
for (int i = 0;i<4;i++)
{
int n_x, n_y;
n_x = x + dir[i][0];
n_y = y + dir[i][1];
if (cheak(n_x, n_y) && cnt + 1 <= cnt_map[n_x][n_y])
{
if (pre_dir == i&&cnt <= 3 && cnt<cnt_map[n_x][n_y]) {
cnt_map[n_x][n_y] = cnt;
dfs(n_x, n_y, cnt, pre_dir);
}
if ((pre_dir != i || pre_dir == -1) && cnt <= 3)
{
cnt_map[n_x][n_y] = cnt + 1;
dfs(n_x, n_y, cnt + 1, i);
}
}
}
}
int main()
{
int i, j, q;
fstream cin("E:/C++/IN/aaa.txt");
while (cin >> n >> m, n + m) {
for (i = 1;i <= n;i++) for (j = 1;j <= m;j++)
cin>>map[i][j];
cin >> q;
while (q--)
{
for (i = 1;i <= n;i++)
for (j = 1;j <= m;j++)
cnt_map[i][j] = 100000;
cin >> s_x >> s_y >> e_x >> e_y;
if (map[s_x][s_y] != map[e_x][e_y] || map[s_x][s_y] == 0 || map[e_x][e_y] == 0)
{
cout<<"NO"<<endl;
}
else
{
flag = 0;
dfs(s_x, s_y, 0, -1);
if (flag) cout<<"YES"<<endl;
else cout<<"NO";
}
}
}
return 0;
}