【hdu1175】连连看——dfs(剪枝)

来源:互联网 发布:淘宝10000点券多少钱 编辑:程序博客网 时间:2024/06/05 02:24

题目:

连连看

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27462    Accepted Submission(s): 6809


Problem Description
“连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
 

Input
输入数据有多组。每组数据的第一行有两个正整数n,m(0<n<=1000,0<m<1000),分别表示棋盘的行数与列数。在接下来的n行中,每行有m个非负整数描述棋盘的方格分布。0表示这个位置没有棋子,正整数表示棋子的类型。接下来的一行是一个正整数q(0<q<50),表示下面有q次询问。在接下来的q行里,每行有四个正整数x1,y1,x2,y2,表示询问第x1行y1列的棋子与第x2行y2列的棋子能不能消去。n=0,m=0时,输入结束。
注意:询问之间无先后关系,都是针对当前状态的!
 

Output
每一组输入数据对应一行输出。如果能消去则输出"YES",不能则输出"NO"。
 

Sample Input
3 41 2 3 40 0 0 04 3 2 141 1 3 41 1 2 41 1 3 32 1 2 43 40 1 4 30 2 4 10 0 0 021 1 2 41 3 2 30 0
 

Sample Output
YESNONONONOYES
 

Author
lwg
 

Recommend
We have carefully selected several similar problems for you:  1180 1072 1016 1026 1240 

题解:这题坑点好多,一开始的时候给了10s,说明搜索的时候应该情况很多的,因为这个并不是找一个最优化的解,而是有转弯次数的限制,这个体现在dfs里面就不太好判断,而且剪枝也是这道题的一个很难的地方。首先第一层剪枝,是在get到查询之后的,如果两个地方的数字不一样,也就是连连看的图案不一样,那么不需要搜索直接输出NO,相应的情况还有该点的数值为0的情况,也是不需要搜索。其次是在dfs时的剪枝,当转弯次数超过两次时,直接判断当前方向上是否可达目标点,这个剪枝能把运行时间从8k降低到200ms。由于这次搜索的主要key是方向,那么相应的修改一下模板,没有用for循环来模拟方向,而是纯粹手打,而且对于每种情况优先考虑和它同方向的搜索,不过这个优化的时间不怎么明显,大概只有20ms左右。代码1是只做了第一层优化的代码,直接T,运气好c++可以过,蛤蛤。

代码:

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>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 <cstdio>#include <cstring>#include <algorithm>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,rightvoid dfs(int x, int y, int dir, int turn){if (ans)return;if (x <= 0 || x > n || y <= 0 || y > m)  //out of mapreturn;if (turn >= 3)                           //turn out of limitreturn;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)                      //barrierreturn;if (vis[x][y])                           //visitedreturn;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");elseputs("NO");}elseputs("NO");}}return 0;}


0 0
原创粉丝点击