zoj 2411 link link Look bfs

11084: link link Look

时间限制: 1 Sec  内存限制: 128 MB
提交: 150  解决: 25
[提交] [状态] [讨论版] [命题人:admin]

题目描述

A single player game is played on a grid chessboard with some colored beads on. In each round, the player can click on two grids of the chessboard. If the two grids both have beads of the same color, and a line with no more than two turns could be drawn to connect the two beads, the two beads will both disappear from the chessboard. Note that the line must not pass through any other beads.

Figure 1: Valid clicks that will erase the two beads

Given the information of a chessboard and the player's clicks in each round, you are required to calculate how many beads will disappear.

 

输入

The input consists of several test cases. The first line of each case contains two numbers N and M (1 <= N, M <= 100), indicating the dimensions of the chessboard. Then N lines follow, each represents a row of the chessboard. The positive numbers represent different colors of beads, and zero represents an empty grid. Then there is a positive number T followed by T lines of input which describe the clicks of all T rounds. Each line contains four numbers x1, y1, x2, y2 which are the positions (x1, y1) and (x2, y2) of the two clicks on the chessboard (the north-west corner is at (1, 1)).

 

输出

For each case, print in one line the number of beads disappeared.

 

样例输入

复制样例数据

3 4
1 1 2 2
3 3 4 4
2 2 1 1
6
1 1 1 2
1 3 1 4
2 1 2 2
2 3 2 4
3 1 3 2
3 3 3 4

3 4
1 2 1 2
3 4 3 4
2 1 2 1
6
1 1 1 2
1 3 1 4
2 1 2 2
2 3 2 4
3 1 3 2
3 3 3 4

3 4
1 2 1 2
1 1 2 2
2 2 1 1
3
2 3 2 4
2 2 3 3
3 2 1 4

2 3
0 0 0
0 0 0
2
1 1 1 1
1 1 1 2

0 0

样例输出

12
0
6
0

每次从起点开始bfs,不好判断的就是拐弯次数

每次在一个方向走到不能走为止,每次可以的点拐弯次数加一同时加入队列

代码:

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
const int maxn = 200;
int s[maxn][maxn];
int n, m;
int dx[10] = {0, 0, -1, 1};
int dy[10] = {-1, 1, 0, 0};
struct node {
    int x, y, turn;
};
queue<node> q;
int vis[maxn][maxn];

bool judge(int x, int y) {
    if (x >= 0 && x <= n + 1 && y >= 0 && y <= m + 1) return 1;
    return 0;
}

bool bfs(int x1, int y1, int x2, int y2) {
    if (s[x1][y1] == 0 || s[x2][y2] == 0 || s[x1][y1] != s[x2][y2]) return false;
    while (!q.empty()) q.pop();
    node v = {x1, y1, -1};
    q.push(v);
    vis[x1][y1] = 1;
    while (!q.empty()) {
        node vv = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
            for (int j = 1;; j++) {
                int xx = vv.x + j * dx[i], yy = vv.y + j * dy[i], tu = vv.turn + 1;
                if (!judge(xx, yy)) break;
                if (s[xx][yy]) {
                    if (xx == x2 && yy == y2) return true;
                    break;
                }
                if (vis[xx][yy]) continue;
                vis[xx][yy] = 1;
                if (tu < 2) {
                    node tmp = {xx, yy, tu};
                    q.push(tmp);
                }
            }
        }
    }
    return false;
}

int main() {
    //freopen("out.txt", "r", stdin);
    while (scanf("%d%d", &n, &m)) {
        if (n == 0 && m == 0) break;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++)
                scanf("%d", &s[i][j]);
        }
        int t;
        scanf("%d", &t);
        int x1, x2, y1, y2, ans = 0;
        while (t--) {
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            if (s[x1][y1] != s[x2][y2]) continue;
            if (bfs(x1, y1, x2, y2)) {
                ans += 2;
                s[x1][y1] = 0;
                s[x2][y2] = 0;
            }
            memset(vis, 0, sizeof(vis));
        }
        memset(vis, 0, sizeof(vis));
        printf("%d\n", ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值