题目:
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
YES NO NO NO NO YES
题解:这题坑点好多,一开始的时候给了10s,说明搜索的时候应该情况很多的,因为这个并不是找一个最优化的解,而是有转弯次数的限制,这个体现在dfs里面就不太好判断,而且剪枝也是这道题的一个很难的地方。首先第一层剪枝,是在get到查询之后的,如果两个地方的数字不一样,也就是连连看的图案不一样,那么不需要搜索直接输出NO,相应的情况还有该点的数值为0的情况,也是不需要搜索。其次是在dfs时的剪枝,当转弯次数超过两次时,直接判断当前方向上是否可达目标点,这个剪枝能把运行时间从8k降低到200ms。由于这次搜索的主要key是方向,那么相应的修改一下模板,没有用for循环来模拟方向,而是纯粹手打,而且对于每种情况优先考虑和它同方向的搜索,不过这个优化的时间不怎么明显,大概只有20ms左右。代码1是只做了第一层优化的代码,直接T,运气好c++可以过,蛤蛤。
代码:
#include
#include
#include
#include
using namespace std;
int map[1010][1010];
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
int n, m,ans,x2,y2;
void dfs(int x1, int y1, int dir, int times)
{
if (x1 == x2 && y1 == y2 && times <= 2)
{
ans = 1;
return;
}
if (times > 2)
return ;
map[x1][y1] = -1;
for (int i = 0; i < 4; i++)
{
int curx = x1 + dx[i];
int cury = y1 + dy[i];
if (curx > 0 && curx <= n && cury > 0 && cury <= m && map[curx][cury] == 0)
{
if (i == dir)
dfs(curx, cury, dir, times);
else
dfs(curx, cury, i, times + 1);
}
}
map[x1][y1] = 0;
return;
}
int main()
{
//freopen("input.txt", "r", stdin);
int q, x1, y1;
while (scanf("%d%d", &n, &m), !(m == 0 && n == 0))
{
//init();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%d", &map[i+1][j+1]);
}
}
scanf("%d", &q);
for (int i = 0; i < q; i++)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
if (map[x1][y1] == map[x2][y2] && map[x1][y1] !=0)
{
map[x2][y2] = 0;
int temp = map[x1][y1];
ans = 0;
dfs(x1, y1, -1, -1);
if (ans)
printf("YES\n");
else
printf("NO\n");
map[x1][y1] = map[x2][y2] = temp;
}
else
puts("NO");
}
}
return 0;
}
代码2:
#include
#include
#include
using namespace std;
const int maxn = 1005;
int map[maxn][maxn], vis[maxn][maxn];
int n, m, x2, y2;
int ans;
//1,2,3,4stands for up,down,left,right
void dfs(int x, int y, int dir, int turn)
{
if (ans)
return;
if (x <= 0 || x > n || y <= 0 || y > m) //out of map
return;
if (turn >= 3) //turn out of limit
return;
if (x == x2 && y == y2) //get the goal
{
ans = 1;
return;
}
if (turn == 2) //剪枝,转弯两次时,看目的点的坐标与现在的朝向是否在一个方向上,不在的话就返回
{
if (!((dir == 1 && y < y2 && x == x2) || (dir == 2 && y > y2 && x == x2) || (dir == 3 && x > x2 && y == y2) || (dir == 4 && x < x2 && y == y2)))
return;
}
if (map[x][y] != 0) //barrier
return;
if (vis[x][y]) //visited
return;
vis[x][y] = 1;
if (dir == 1)
{
dfs(x, y + 1, 1, turn);
dfs(x, y - 1, 2, turn + 1);
dfs(x - 1, y, 3, turn + 1);
dfs(x + 1, y, 4, turn + 1);
}
else if (dir == 2)
{
dfs(x, y - 1, 2, turn);
dfs(x, y + 1, 1, turn + 1);
dfs(x - 1, y, 3, turn + 1);
dfs(x + 1, y, 4, turn + 1);
}
else if (dir == 3)
{
dfs(x - 1, y, 3, turn);
dfs(x, y + 1, 1, turn + 1);
dfs(x, y - 1, 2, turn + 1);
dfs(x + 1, y, 4, turn + 1);
}
else if (dir == 4)
{
dfs(x + 1, y, 4, turn);
dfs(x, y + 1, 1, turn + 1);
dfs(x, y - 1, 2, turn + 1);
dfs(x - 1, y, 3, turn + 1);
}
vis[x][y] = 0;
return;
}
int main()
{
//freopen("input.txt", "r", stdin);
int q, x1, y1;
while (scanf("%d%d", &n, &m) != EOF, n || m)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
scanf("%d", &map[i][j]);
}
}
scanf("%d", &q);
while (q--)
{
memset(vis, 0, sizeof(vis));
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
ans = 0;
if (x1 == x2 && y1 == y2)
puts("NO");
else if (map[x1][y1] == map[x2][y2] && map[x1][y1])
{
dfs(x1, y1 + 1, 1, 0);
dfs(x1, y1 - 1, 2, 0);
dfs(x1 - 1, y1, 3, 0);
dfs(x1 + 1, y1, 4, 0);
if (ans)
puts("YES");
else
puts("NO");
}
else
puts("NO");
}
}
return 0;
}