首页 > 其他分享 >ABC246E Bishop 2 题解

ABC246E Bishop 2 题解

时间:2022-10-24 08:23:20浏览次数:72  
标签:int 题解 void tp ABC246E dq Bishop dir define

ABC246E Bishop 2 Solution

目录

更好的阅读体验戳此进入

题面

给定有障碍的网格图,. 为空地,# 为障碍。给定起点终点,每次移动仅可以斜向走任意长度,问从起点到终点的最少移动次数,可能无解,无解输出 -1

Solution

BFS 较为显然,因为时限 6000ms,只要写的不太丑直接搜也能过。对于本题,使用 01BFS 较为显然。我们在宽搜每次搜一步且距离仅为 $ 1 $,并记录上一步的方向,如果同向则认为走了一个 $ 0 $ 边,异向则为 $ 1 $ 边,开个双端队列,同向插到前面,反之插到后面,按照正常宽搜每次取队头扩展即可。

需要注意对于 01BFS 时,我们判断是否走过和是否为答案的时候,需要在从队头取元素的时候判断,而不是在扩展的时候判断。因为如果在某一次由 $ 1 $ 扩展的时候如果直接把 $ vis $ 设为 $ \texttt{true} $,但是这可能会导致后面从 $ 0 $ 扩展的,本应能插在队列中比这个更前面的更优的无法转移,使答案更劣。同时我们也需要考虑到不同方向的时候扩展也是不同,所以 $ vis $ 中也要考虑到方向这一维。

Code

#define _USE_MATH_DEFINES
#include <bits/extc++.h>

#define PI M_PI
#define E M_E
#define npt nullptr
#define SON i->to
#define OPNEW void* operator new(size_t)
#define ROPNEW(arr) void* Edge::operator new(size_t){static Edge* P = arr; return P++;}

using namespace std;
using namespace __gnu_pbds;

mt19937 rnd(random_device{}());
int rndd(int l, int r){return rnd() % (r - l + 1) + l;}
bool rnddd(int x){return rndd(1, 100) <= x;}

typedef unsigned int uint;
typedef unsigned long long unll;
typedef long long ll;
typedef long double ld;

#define CHK(x, y) (x >= 1 && x <= N && y >= 1 && y <= N && !mp[x][y])

template<typename T = int>
inline T read(void);

int N;
int dx[10] = {0,  -1, -1, 1, 1};
int dy[10] = {0,  -1, 1, -1, 1};
int vis[1600][1600][5];
bool mp[1600][1600];

struct Status{
    int x, y;
    int dir;//direction 1, 2, 3, 4
    int dist;
}S, T;
void Init(void){
    char c = getchar();
    for(int i = 1; i <= N; ++i)for(int j = 1; j <= N; ++j){
        while(c != '.' && c != '#')c = getchar();
        mp[i][j] = c == '.' ? false : true;
        c = getchar();
    }
}
void bfs(void){
    deque < Status > dq;
    dq.push_back(S);
    while(!dq.empty()){
        auto tp = dq.front(); dq.pop_front();
        if(vis[tp.x][tp.y][tp.dir])continue;
        vis[tp.x][tp.y][tp.dir] = true;
        if(tp.x == T.x && tp.y == T.y)
            printf("%d\n", tp.dist), exit(0);
        // printf("Current pos (%d, %d): dis = %d, dir = %d\n", tp.x, tp.y, tp.dist, tp.dir);
        for(int i = 1; i <= 4; ++i){
            int tx = tp.x + dx[i], ty = tp.y + dy[i];
            if(!CHK(tx, ty))continue;
            if(i == tp.dir)dq.push_front(Status{tx, ty, i, tp.dist});
            else dq.push_back(Status{tx, ty, i, tp.dist + 1});
        }
    }printf("-1\n");
}

int main(){
    // freopen("test_11.txt", "r", stdin);
    N = read();
    int x = read(), y = read(); S = Status{x, y, 0, 0};
    x = read(), y = read(); T = Status{x, y, 0, 0};
    Init();
    bfs();
    fprintf(stderr, "Time: %.6lf\n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}

template<typename T>
inline T read(void){
    T ret(0);
    short flag(1);
    char c = getchar();
    while(c != '-' && !isdigit(c))c = getchar();
    if(c == '-')flag = -1, c = getchar();
    while(isdigit(c)){
        ret *= 10;
        ret += int(c - '0');
        c = getchar();
    }
    ret *= flag;
    return ret;
}

UPD

update-2022_10_21 初稿

标签:int,题解,void,tp,ABC246E,dq,Bishop,dir,define
From: https://www.cnblogs.com/tsawke/p/16820301.html

相关文章

  • LG-P5104 红包发红包 题解
    LG-P5104红包发红包Solution目录LG-P5104红包发红包Solution更好的阅读体验戳此进入UPD更好的阅读体验戳此进入(建议您从上方链接进入我的个人网站查看此Blog,在Luo......
  • ABC246C Coupon 题解
    ABC246CCouponSolution目录ABC246CCouponSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面$n$个物品第$i$个价格为$a_i$,......
  • The 18th Zhejiang Provincial Collegiate Programming Contest题解
    A.LeagueofLegends水题。点击查看代码#include<bits/stdc++.h>usingnamespacestd;#defineintlonglonginta[6];intb[6];signedmain(){for(......
  • ABC274 题解
    A题目:给定\(A,B\)输出\({B}\over{A}\)保留\(3\)位小数。简答题,和A+Bproblem一样,除一除,保留一下小数。B题目:给定一个\(n\)行\(m\)列由'.'和'#'的方阵,求每列......
  • 【题解】The 2021 ICPC Asia Macau Regional Contest - E Pass The Ball
    问题描述解释相当于给定一个置换群,求\(\sum\limits_{i=1}^{n}i*b_{i}\)题目分析本题这种传球的关系显然是存在循环节的,先考虑一个大小为\(m\)的环,显然我们可以用......
  • 0025:2011年NOIp普及组真题——瑞士轮题解
    题目链接:https://www.luogu.com.cn/problem/P1309如果是新手可能马上会想到sort排序,每比一次就排一次,但是这样的时间复杂度有点高,只有60分;这是因为每次比完赛会产生两个......
  • ABC274D题解
    这是一道较为简单的可行性DP。首先看到题目,很容易想到将横纵坐标一起进行处理,但显然时间会炸飞。所以我们将横纵坐标拆开分别处理,那么就有如下状态:\(dpa_{i,j}\)表示在......
  • Ubuntu20.04运行wiki.js服务器出错问题解决方法
    错误代码:root@xxx:/home/xxxxx/wiki#nodeserverLoadingconfigurationfrom/home/xxxxx/wiki/config.yml...OK2022-10-23T05:00:25.563Z[MASTER]info:============......
  • ABC274G题解
    这是一个比较经典的网络流的建模。首先我们可以横着和竖着给原图编两遍号,能够一次照到的编号相同。以样例一为例:....#....先横着编号:1112#3444再......
  • ABC274C题解
    直接扫一遍,统计即可。点击查看代码#include<bits/stdc++.h>usingnamespacestd;constintMAXN=2e5+5;//charbuf[1<<21],*p1=buf,*p2=buf;//#definegetchar(......