hdoj1175 连连看

本文介绍了一种使用优先队列优化的广度优先搜索(BFS)算法来解决连连看游戏中两个图案之间的路径寻找问题。该算法确保了路径的拐弯次数不超过两次,详细解释了算法实现细节,并附带完整代码。

题意:

  连连看的玩法:给定一个棋盘,上面有各种数字,大于0的数字表示图案,0表示该位置没有图案。问当给定两个图案的坐标时,能不能按照连连看的规则把这两个图案消去。注意,这里所谓连连看的规则,是找出一条路径让这两个图案连接起来、并且该路径的“拐弯”的次数最多为2。
  思路其实已经很清晰了,在起点和终点间找出一条路径并且“拐弯数”(direction change)尽可能少,很容易想到BFS。但是BFS通常是对“步数”(step)最少的一种求解方式,而且对应的树结构的每一层,都对应着相同的step,随着层数增加,step也递增。而这里是change尽可能少,小于3(可以当作求最少change),所以要对常用的BFS框架进行一些改动的地方(当然这只是我的思路,还有别的方法):
  (1)queue换成priority_queue。
  因为刚才也说了,求step最少的时候,随着BFS树结构层数增加,step也增加,换句话说,队列从头往后遍历的时候,首先遍历的节点的step肯定是更小的。而这道题目中,从某一点出发往4个方向遍历的时候,queue不能保证首先遍历的点的change是更小的。
  (2)标记数组(即标记某个坐标位置是否已经被遍历过的数组)增加一维以记录:在某个方向上,某个点被遍历过。因为在这道题里,一个位置不仅仅只能被访问一次,否则很可能出现某个位置被标记之后,不能再让change更少的方向来进行访问了。

举个例子,假设有这样一个棋盘,绿色格子是起点,黄色是终点:
这里写图片描述
因为0代表路径,数字代表不可以走,所以可以抽象成下面这个图:
这里写图片描述
下面我们要用数字填充这个表格的空缺位置,填上的数字表示在某条路上change的次数,比如一开始出发有两条路,一个向上(红色),一个向右(蓝色),因为都还没有拐弯,所以都是0:
这里写图片描述
继续进行这个过程:
(1)红色的路,因为还是向上走,所以没有拐弯,所以再走出一个0;
(2)蓝色的路,因为本来是向右的,现在要向下走了,拐了一次弯,所以要加1。如图:
这里写图片描述
然后一直走到如下图:
这里写图片描述
这时候很明显了,如果走蓝色的路,走到终点的时候,change为3,不符合条件,而如果是红色的路,最终change是2,符合条件,就找到了一条路了:
这里写图片描述

代码:

// 967ms, 22600K
#include <iostream>
#include <cstring>
#include <queue>        // bfs
#include <algorithm>    // min
using namespace std;

struct Location {
    int x, y;
    int dir;
    int sum;

    Location(int x, int y, int dir, int sum) : x(x), y(y), dir(dir), sum(sum) {
    }
};

bool operator<(const Location &a, const Location &b) {
    return a.sum > b.sum;
}

const int MAXN(1001), INF(0x3f3f3f3f);
const int DIR[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};   // up down left right
bool isVisited[MAXN][MAXN][4];  // 3-dimen
int board[MAXN][MAXN];  // data
int row, col;

void init() {
    memset(isVisited, false, sizeof isVisited);
}

bool isValid(int x, int y) {
    return x >= 0 && x < row && y >= 0 && y < col;
}

// might be a long time to execute this BFS
bool bfs(int start_x, int start_y, int end_x, int end_y) {
    priority_queue<Location> locations;
    for (int i = 0; i < 4; ++i) isVisited[start_x][start_y][i] = true;
    locations.push( Location(start_x, start_y, -1, 0) );
    while (!locations.empty()) {
        Location cur = locations.top();
        locations.pop();
        // explore 4 directions
        for (int i = 0; i < 4; ++i) {   // i is a certain direction
            int xx = cur.x + DIR[i][0];
            int yy = cur.y + DIR[i][1];
            if (isValid(xx, yy) && !isVisited[xx][yy][i] && (board[xx][yy] == 0 || xx == end_x && yy == end_y)) {

                // If the direction is the same as previous, then change remains the same
                // but if direction changes, then change += 1
                int this_sum = (i == cur.dir ? cur.sum : cur.sum + 1);

                // no push if >3
                if (this_sum > 3) {
                    continue;
                }
                // end
                if (xx == end_x && yy == end_y) {
                    return true;
                }
                isVisited[xx][yy][i] = true;
                locations.push( Location(xx, yy, i, this_sum) );
            }
        }
    }
    return false;
}


int main() {
//  freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
    while (cin >> row >> col) {
        if (row == 0 && col == 0) {
            break;
        }

        //init();
        for (int i(0); i < row; ++i) {
            for (int j(0); j < col; ++j) {
                cin >> board[i][j];
            }
        }

        int query;
        cin >> query;
        int x1, y1, x2, y2;
        while (query--) {
            cin >> x1 >> y1 >> x2 >> y2;
            --x1;
            --y1;
            --x2;
            --y2;

            // pre-condition
            int num1 = board[x1][y1];
            int num2 = board[x2][y2];
            if (num1 != num2 || num1 == 0) {
                cout << "NO" << endl;
                continue;
            }

            // bfs
            init();
            if (bfs(x1, y1, x2, y2)) {
                cout << "YES"; 
            } else {
                cout << "NO";
            }
            cout << endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值