【HDU 5335】Walk Out(BFS)

这道题卡时间卡的比较紧。

一开始直接BFS 毫无疑问的超时,之后想到根据BFS的常规优化思想,去选择起始点进行遍历。

这样我们一开始先BFS一次,这次的BFS是选择出这一点为1并且从起点到这一个点,中间路径的点全为0的点。

这样选择出这个点之后,这个点到终点的路径长度就可以断定了。

之后我们把所有到终点距离最小的点放在一个容器里进行BFS。

这道题没有做出来的原因很大一部分就是对BFS的理解不够深以及该有的贪心思路没有,导致了这道题没有AC。

嗯,有句话说的对:综合是第一生产力。

#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1005;
const int maxd = 1000005;
const int  INF = (1 << 30);
int n,m;
char mat[maxn][maxn];
const int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
bool vis[maxn][maxn];
int  min_dist;
vector<int>v0,v1,vv;
int path[maxd];
int cnt,ok;
void bfs_init(){
    queue<int>q,_q;
    q.push(0);
    vis[0][0] = true;
    while(!q.empty()){
        int pos = q.front(); q.pop();
        int x = pos / m, y = pos % m;
        if(mat[x][y] == '1'){
            _q.push(pos);
            min_dist = min(min_dist,n + m - x - y);
            continue;
        }
        if(x == n - 1 && y == m - 1){
            ok = 1;
            return;
        }
        for(int i = 0; i < 4; i++){
            int _x = x + dir[i][0];
            int _y = y + dir[i][1];
            if(_x >= 0 && _x < n && _y >= 0 && _y < m && !vis[_x][_y]){
                vis[_x][_y] = true;
                q.push(_x * m + _y);
            }
        }
    }
    while(!_q.empty()){
        int pos = _q.front(); _q.pop();
        int x = pos / m ,y = pos % m;
        if((n + m - x - y) == min_dist){
            vv.push_back(pos);
        }
    }
}
void bfs(){
    path[0] = 1;
    for(int i = 1; i < min_dist - 1; i++){
        int Size = vv.size();
        for(int j = 0; j < Size; j++){
            int x = vv[j] / m, y = vv[j] % m;
            for(int d = 0; d < 2; d ++){
                int _x = x + dir[d][0];
                int _y = y + dir[d][1];
                if(_x >= 0 && _x < n && _y >= 0 && _y < m && !vis[_x][_y]){
                    vis[_x][_y] = true;
                    if(mat[_x][_y] == '0')
                        v0.push_back(_x * m + _y);
                    else
                        v1.push_back(_x * m + _y);
                }
            }
        }
        if(v0.empty()){
            path[cnt++] = 1;
            vv = v1;
            v1.clear();
        }
        else{
            path[cnt++] = 0;
            vv = v0;
            v0.clear();
            v1.clear();
        }
    }
}
void init(){
    min_dist = INF;
    memset(vis,false,sizeof(vis));
    v0.clear();
    v1.clear();
    vv.clear();
    cnt = 1;
    ok  = 0;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        init();
        for(int i = 0; i < n; i++)
            scanf("%s",mat[i]);
        bfs_init();
        if(ok)
            printf("0\n");
        else{
            bfs();
            for(int i = 0; i < cnt; i++)
                printf("%d",path[i]);
            puts("");
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 02:07:51

【HDU 5335】Walk Out(BFS)的相关文章

【HDU 3400】Line belt(三分法)

题目链接 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3400 题意 有两条传送带AB和CD,移动速度分别为p,q. 除了传送带的其他区域移动速度为r,问A到D最短时间. 题目分析 在AB上找一点E,在CD上找一点F. 使得A->E->F->D时间最短. 数学思路 时间 time = |AE|/p + |EF|/r + |FD|/q. (|AE|为线段AE的长度.) 未知量有E的位置和F的位置,由于确定在AB和CD上,所以只需要两个未知量

【HDU 1533】 Going Home (KM)

Going Home Problem Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel f

【POJ - 3669】Meteor Shower(bfs)

-->Meteor Shower 直接上中文了 Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平面坐标系的原点放牧,打算在群星断其生路前转移至安全地点. 此次共有M (1 ≤ M ≤ 50,000)颗流星来袭,流星i将在时间点Ti (0 ≤ Ti  ≤ 1,000) 袭击点 (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300).每颗流星都将摧毁落点及其相邻四点的区

【HDU 3640】I, zombie (二分)

传送门 题目描述 The "endless" model in "I, zombie" of "Plants vs. Zombies" is my favourite.The aim of the game is to put down the zombies most reasonable in the right-most , to eat the brain protected by Plants in the left-most. To

【HDU 5839】Special Tetrahedron(计算几何)

空间的200个点,求出至少四边相等,且其余两边必须不相邻的四面体的个数. 用map记录距离点i为d的点有几个,这样来优化暴力的四重循环. 别人的做法是枚举两点的中垂面上的点,再把到中点距离相等的点找出来,n^3的样子. 还要注意四个点共面的情况. 共面判断就是用叉乘计算出ijk三点所在面的法向量,然后判断il向量是否和法向量垂直,是则共面. #include <cstdio> #include <cstring> #include <algorithm> #includ

【HDU 4763】Theme Section(KMP)

这题数据水的一B,直接暴力都可以过. 比赛的时候暴力过的,回头按照正法做了一发. 匹配的时候 失配函数 其实就是前缀 后缀的匹配长度,之后就是乱搞了. KMP的题可能不会很直接的出,但是KMP的思想经常渗透在很多题目里面,最近需要多练习一下. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 1000005; int _next[max

【HDU 5091】Beam Cannon(扫描线)

按照x轴建树,求线段树的区间最值. 模型转化就是: 矩阵最大交 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define lson (pos<<1) #define rson (pos<<1|1) const int ADD = 25555; const int maxn = 80005; int n,w,h,cnt; struct Se

【HDU 4445】Crazy Tank(暴力)

高中物理斜抛运动,简单分析一下角度固定下来则可以计算每个cannonball的降落坐标lnd. 因此暴力计算不同角度下的结果. #include <cstdio> #include "cmath" #include "algorithm" #define ll long long #define dd double #define N 205 #define g 9.8 #define eps 1e-6 const dd pi=acos(-1.0); l

【hdu 5918】Sequence I(KMP)

给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 #include<bits/stdc++.h> #define N 1000005 int t,n,m,p; int nex[N]; int a[N],b[N]; using namespace std; void getNext(){ int i=0,k=-1; nex[0]=k; while(b