CodeForces - 1063B - Labyrinth 最短路+缩点

题目链接

题意:一个迷宫,起点为(r,c)限制每次从起点出发最多只能向左走x步,向右走y步,问能到达的点的数量(包括起点)。

思路:由于一列里面中间没有墙壁隔开的点可以直接到达不用考虑距离,那么可以按列进行缩点,把在一列里面可以直接到达的点都放进一个集合,然后按集合跑最短路即可

ps:由于从起点走到某一个点,|向左走的步数-向右走的步数|=|该点的列数-起点的列数|,所以直接设距离=向左走的步数+向右走的步数,最短的即为最优解。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<windows.h>
#include<conio.h>
using namespace std;
#define ll long long
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define NUM 4000005
#define debug true
#define lowbit(x) ((-x)&x)
#define ffor(i,d,u) for(int i=(d);i<=(u);++i)
#define _ffor(i,u,d) for(int i=(u);i>=(d);--i)
#define mst(array,Num,Kind,Count) memset(array,Num,sizeof(Kind)*(Count))
const int p = 1e9+7;
int n,m,r,c,x,y,s,ednum=0,ans=0;
char g[2005][2005];
struct node1
{
    int l,r;
    int id;
    bool operator>(const node1 &a)const
    {
        return (l+r) > (a.l+a.r);
    }
}dist[NUM];
int snum[NUM]={},head[NUM]={},index[2005][2005];
struct node2
{
    int next;
    node1 w;
}e[NUM<<1];
template <typename T>
inline void read(T &x){
    char ch = getchar();x = 0;
    for (; ch < '0' || ch > '9'; ch = getchar());
    for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
}
template <typename T>
inline void write(T x)
{
    int len=0;char c[21];
    if(x<0)putchar('-'),x*=(-1);
    do{++len;c[len]=(x%10)+'0';}while(x/=10);
    _ffor(i,len,1)putchar(c[i]);
}
inline void light()//缩点
{
    s=0;
    ffor(i,1,m)
        ffor(j,1,n)
        {
            if(g[j][i] != '.')continue;
            if(g[j-1][i] != '.')++s,dist[s].id = s,dist[s].l = dist[s].r = p;
            ++snum[s];
            index[j][i] = s;
        }
}
inline void add_edge(const int &a,const int &b)
{
    int s1 = index[a][b-1],s2 = index[a][b];
    e[++ednum].next=s1[head],e[ednum].w.l=0,e[ednum].w.r=1,e[ednum].w.id=s2,head[s1]=ednum;
    e[++ednum].next=s2[head],e[ednum].w.l=1,e[ednum].w.r=0,e[ednum].w.id=s1,head[s2]=ednum;
}
inline void build_graph()//建图
{
    ffor(i,2,m)
        ffor(j,1,n)
        {
            if(g[j][i] != '.'||g[j][i-1] != '.')continue ;
            if(g[j-1][i] != '.'||g[j-1][i-1] != '.')add_edge(j , i);
        }
}
inline void dij()
{
    priority_queue < node1 , vector < node1 > , greater < node1 > > q;
    node1 a,b;
    a.id = index[r][c];
    a.l = a.r = 0;
    dist[a.id] = a;
    q.push(a);
    while(!q.empty())
    {
        a = q.top(),q.pop();
        for(int i = head[a.id] ; i != 0 ; i = e[i].next)
        {
            b.l = e[i].w.l+dist[a.id].l,b.r = e[i].w.r+dist[a.id].r;
            b.id = e[i].w.id;
            if(b.l <= x && b.r <= y && dist[b.id] > b)
            {
                dist[b.id] = b;
                q.push(b);
            }
        }
    }
}
inline void AC()
{
    read(n),read(m),read(r),read(c),read(x),read(y);
    g[0][0] = 0;
    ffor(i,1,n)g[i][0] = 0;
    ffor(i,1,m)g[0][i] = 0;
    ffor(i,1,n)
    {
        ffor(j,1,m)g[i][j]=getchar();
        getchar();
    }
    light();
    build_graph();
    dij();
    ffor(i,1,s)if(dist[i].l <= x && dist[i].r <= y)ans += snum[i];
    write(ans),putchar('\n');
}
int main()
{
    AC();
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值