HDU - 1175 连连看(BFS 扩展顺序不同导致WA ???)

22 篇文章 0 订阅
21 篇文章 0 订阅

HDU - 1175 连连看

题目

给一个迷宫,起点和终点,问是否能从起点走到终点,且转向次数不超过 2 次。

分析

题目并不难,很容易想到 bfs,不过要有很多细节要注意。

定义 bfs 的每次的状态包括当前位置,当前已转向次数,来时的方向。

可以先将终点位置值设为 0 ,即通路,便于写代码。但是 bfs之后要改回去。

后面判断转向就用要去的方向与来时的方向一样不一样。

注意细节

2019.7.25 更

最近发现了这题的 bug,用原来代码提交能 A C AC AC,但是改了一下 B F S BFS BFS 的扩展方向,例如改成先下后上之类的,就会 W A WA WA。思考了一下。

原来的代码错的因为题目的数据太水,导致用错误代码用特定的扩展方向也能过

错误原因:

先上数据:

3 4
1 1 0 5
0 0 0 0
5 0 1 0

3 1 1 4

就是上面的图,从左下的 5 走到右上的 5。
在这里插入图片描述
关键看黄色节点,如果扩展顺序导致红色路径先选,那么黄色节点就会被红色路径先占据,按原来的代码就会被标记,绿色的路径扩展到黄色节点时因为已经被标记,导致绿色路径被迫中断,然而成功的路径恰恰是绿色路径。如图:
在这里插入图片描述

正确做法

对于一个点,要考虑所有经过他的路径,其唯一目的就是让从起点到终点的路径转弯的次数最小。

做法就是如果这个点之前被标记过也没关系,只要当前的路径到达这个点的转向次数不超过上次就行。

tturn <= vis[tx][ty]

这时候标记数组不单单存是否访问过,而是标记最小转向次数

错误代码

#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
#define d(x) cout<<(x)<<endl;
typedef long long ll;
const int N = 1e3 + 10;
const int base = 60;

int n, m, q, s1, s2, e1, e2;
int a[N][N];                //存迷宫
int vis[N][N];              //标记数组
int dir[][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};

struct node{                
    int x, y, turn, di;     // 表示 位置,转向次数,来时的方向
    node(int x, int y, int turn, int di):x(x),y(y),turn(turn),di(di){}
};

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

int bfs(){
    if(!a[s1][s2] || !a[e1][e2] || a[s1][s2] != a[e1][e2])
        return 0;
    a[e1][e2] = 0;                  // 这里改变了值,bfs完了要改回去
    queue<node> q;
    q.push(node(s1, s2, -1, -1));
    while(!q.empty()){
        node cnt = q.front();
        q.pop();
        if(cnt.x == e1 && cnt.y == e2 && cnt.turn <= 2){
            return 1;
        }
        if(cnt.turn == 3)
            continue;
        for(int i = 0; i < 4; i++){
            int tx = cnt.x + dir[i][0];
            int ty = cnt.y + dir[i][1];
            if(!vis[tx][ty] && f(tx, ty) && a[tx][ty] == 0){
                vis[tx][ty] = 1;
                q.push(node(tx, ty, (i == cnt.di ? cnt.turn : cnt.turn + 1), i));
            }
        }
    }
    return 0;
}

int main() {
    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", &a[i][j]);
            }
        }
        scanf("%d", &q);
        while(q--){
            memset(vis, 0, sizeof(vis));    // 每次bfs之前要清空
            scanf("%d%d%d%d", &s1, &s2, &e1, &e2);
            int temp = a[e1][e2];
            printf(bfs() ? "YES\n" : "NO\n");
            a[e1][e2] = temp;               // 改回去
        }
    }
    return 0;
}

正确代码

#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
#define d(x) cout<<(x)<<endl;
typedef long long ll;
const int N = 1e3 + 10;
const int base = 60;

int n, m, q, s1, s2, e1, e2;
int a[N][N];                //存迷宫
int vis[N][N];              //标记数组
int dir[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

struct node{                
    int x, y, turn, di;     // 表示 位置,转向次数,来时的方向
    node(int x, int y, int turn, int di):x(x),y(y),turn(turn),di(di){}
};

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

int bfs(){
    if(!a[s1][s2] || !a[e1][e2] || a[s1][s2] != a[e1][e2])
        return 0;
    a[e1][e2] = 0;                  // 这里改变了值,bfs完了要改回去
    queue<node> q;
    q.push(node(s1, s2, -1, -1));
    vis[s1][s2] = 1;
    while(!q.empty()){
        node cnt = q.front();
        q.pop();
        if(cnt.x == e1 && cnt.y == e2 && cnt.turn <= 2){
            return 1;
        }
        if(cnt.turn == 2 && cnt.x != e1 && cnt.y != e2)
            continue;
        for(int i = 0; i < 4; i++){
            int tx = cnt.x + dir[i][0];
            int ty = cnt.y + dir[i][1];				 
            int tturn = (i == cnt.di ? cnt.turn : cnt.turn + 1);
            if(f(tx, ty) && (!vis[tx][ty] || tturn <= vis[tx][ty]) && a[tx][ty] == 0 && tturn < 3){
            	if(!(tx == e1 && ty == e2))
                	vis[tx][ty] = tturn + 1;		// + 1 为了标记没转向时 
                q.push(node(tx, ty, tturn, i));
            }
        }
    }
    return 0;
}

int main() {
    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", &a[i][j]);
            }
        }
        scanf("%d", &q);
        while(q--){
            memset(vis, 0, sizeof(vis));    // 每次bfs之前要清空
            scanf("%d%d%d%d", &s1, &s2, &e1, &e2);
            int temp = a[e1][e2];
            printf(bfs() ? "YES\n" : "NO\n");
            a[e1][e2] = temp;               // 改回去
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值