bzoj 5138 [Usaco2017 Dec]Push a Box

本文介绍了一种利用广度优先搜索(BFS)和深度优先搜索(DFS)解决推箱子游戏问题的方法,通过建立圆方树来确定玩家能否将箱子推到指定位置,同时提供了完整的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.elijahqi.win/archives/3580
Description
Bessie and her friends have invented a new game. The game is named accurately, but not particularly
creatively. They call it the “Push A Box Around The Barn To Get It In The Right Spot And Don’t Move
The Hay” game (if you think that’s excessive, you should see some of the variable names the cows use
when they write code…)The barn can be modeled as an N×M rectangular grid. Some of the grid cells
have hay in them. Bessie occupies one cell in this grid, and a large wooden box occupies another ce
ll. Bessie and the box are not able to fit in the same cell at the same time, and neither can fit in
to a cell containing hay.Bessie can move in the 4 orthogonal directions (north, east, south, west) a
s long as she does not walk into hay. If she attempts to walk to the space with the box, then the bo
x will be pushed one space in that direction, as long as there is an empty cell on the other side. I
f there is no empty cell, then Bessie will not be able to make that move.A certain grid cell is desi
gnated as the goal. Bessie’s goal is to get the box into that location.Given the layout of the barn,
including the starting positions of the box and the cow, and the target position of the box, determ
ine if it possible to win the game.
Input
The first line has three numbers, N, M, and Q,
where N is the number of rows in the grid and M is the number of columns.
1≤N,M≤1500
1≤Q≤50,000
On the next N lines is a representation of the grid,
where characters represent empty cells (.), hay (#),
Bessie’s starting position (A), and the box’s initial location (B).
This is followed by Q lines, each with a pair of integers (R,C).
For each pair, you should determine if it is possible to get the box to that cell at row R, column C,
starting from the initial state of the barn. The top row is row 1, and the left column is column 1.
Output
Q lines, each with either the string “YES” or “NO”.
Sample Input
5 5 4

.

.

A.B..

.

.

3 2

3 5

1 3

5 3
Sample Output
NO

YES

NO

NO

To push the box to the position (3, 5), the cow just needs to move 3 spaces to the right.

None of the other three positions are attainable.
HINT
Source
Platinum

设visit[x][y][i]表示箱子在x,y这个位置 人在箱子的i这个方向上 那么考虑如何不经过箱子到达箱子的另外一边 我们考虑 这样的点一定是在点双里 那么我们直接建出圆方树 然后判断的时候就看是否是 父亲的父亲 或者拥有 相同的父亲即可

mmp蒟蒻我bfs的时候 需要先标记vis表示是否在队列里 我基础这么菜?..

第一遍bfs确定我能到箱子的哪边

第二遍就是求答案 最后o(1)询问即可

#include<queue>
#include<cstdio>
#include<cctype>
#include<algorithm>
#define fi first
#define se second
#define pa pair<int,int>
#define mp(x,y) make_pair(x,y)
using namespace std;
const int N=1550;
const int NN=N*N;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x*f;
}
char s[N][N];int num,dfn[NN],low[NN];bool visit[N][N][4],vis[N][N];
int n,m,Q,cnt,id[N][N],tot,fa[NN<<1],q[NN],top,stx,sty,edx,edy;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
inline void dfs(int x,int y,int frx,int fry){
    dfn[id[x][y]]=low[id[x][y]]=++num;q[++top]=id[x][y];
    for (int i=0;i<=3;++i){
        int x1=x+dx[i],y1=y+dy[i];
        if (x1<1||x1>n||y1<1||y1>m) continue;
        if (x1==frx&&y1==fry) continue;
        if (s[x1][y1]!='.') continue;
        if (!dfn[id[x1][y1]]){
            dfs(x1,y1,x,y);low[id[x][y]]=min(low[id[x][y]],low[id[x1][y1]]);
            if (low[id[x1][y1]]>=dfn[id[x][y]]){
                ++tot;fa[tot]=id[x][y];int v;
                do{
                    v=q[top--];fa[v]=tot;
                }while(v!=id[x1][y1]);

            }
        }else low[id[x][y]]=min(low[id[x][y]],dfn[id[x1][y1]]);
    }
}
inline bool check(int x,int y){
    if (!fa[x]&&!fa[y]) return 0;
    return fa[x]==fa[y]||fa[fa[x]]==y||fa[fa[y]]==x;
}
inline void init(int sx,int sy){
    queue<pa>q;q.push(mp(sx,sy));vis[sx][sy]=1;vis[edx][edy]=1;
    while(!q.empty()){
        int x=q.front().fi,y=q.front().se;q.pop();//printf("%d\n",vis[x][y]);vis[x][y]=1;printf("%d %d\n",x,y);printf("%lld\n",q.size());
        for (int i=0;i<4;++i){
            int x1=x+dx[i],y1=y+dy[i];if (s[x1][y1]!='.') continue;
            if (x1<1||x1>n||y1<1||y1>m) continue;
            if (vis[x1][y1]) continue;vis[x1][y1]=1;q.push(mp(x1,y1));
        }
    }
}
struct node{int x,y,s;};
inline void gao(){
    queue<node> q;
    for (int i=0;i<=3;++i){
        int x=edx+dx[i],y=edy+dy[i];
        if(x<1||x>n||y<1||y>m) continue;
        if (s[x][y]!='.'||!vis[x][y]) continue;
        visit[edx][edy][i]=1;q.push((node){edx,edy,i});
    }
    while(!q.empty()){
        static node v;v=q.front();q.pop();
        int x=v.x,y=v.y,S=v.s;
        int x1=x+dx[S^1],y1=y+dy[S^1];
        if(!(x1<1||x1>n||y1<1||y1>m)&&s[x1][y1]=='.'&&!visit[x1][y1][S]){
            visit[x1][y1][S]=1;q.push((node){x1,y1,S});
        } int nowx=x+dx[S],nowy=y+dy[S];
        for (int i=0;i<=3;++i){
            int x1=x+dx[i],y1=y+dy[i];
            if (x1<1||x1>n||y1<1||y1>m) continue;
            if (!check(id[nowx][nowy],id[x1][y1])) continue;
            if (s[x1][y1]!='.') continue;if (visit[x][y][i]) continue;
            visit[x][y][i]=1;q.push((node){x,y,i});
        }
    }
}
int main(){
    freopen("bzoj5138.in","r",stdin);
    n=read();m=read();Q=read();tot=n*m;
    for (int i=1;i<=n;++i) scanf("%s",s[i]+1);
    for (int i=1;i<=n;++i) 
        for (int j=1;j<=m;++j) id[i][j]=++cnt;
    for (int i=1;i<=n;++i){
        for (int j=1;j<=m;++j){
            if(s[i][j]=='A') stx=i,sty=j,s[i][j]='.';
            if(s[i][j]=='B') edx=i,edy=j,s[i][j]='.';
        }
    }dfs(stx,sty,0,0);
    init(stx,sty);
    gao();
    while(Q--){
        int x=read(),y=read();bool flag=0;flag|=(x==edx)&&(y==edy);
        for (int i=0;i<=3;++i) flag|=visit[x][y][i];
        flag?puts("YES"):puts("NO");
    }
    return 0;
}
题目描述 牛牛和她的朋友们正在玩一个有趣的游戏,他们需要构建一个有 $n$ 个节点的无向图,每个节点都有一个唯一的编号并且编号从 $1$ 到 $n$。他们需要从节点 $1$ 到节点 $n$ 找到一条最短路径,其中路径长度是经过的边权的和。为了让游戏更有趣,他们决定在图上添加一些额外的边,这些边的权值都是 $x$。他们想知道,如果他们添加的边数尽可能少,最短路径的长度最多会增加多少。 输入格式 第一行包含两个正整数 $n$ 和 $m$,表示节点数和边数。 接下来 $m$ 行,每行包含三个整数 $u_i,v_i,w_i$,表示一条无向边 $(u_i,v_i)$,权值为 $w_i$。 输出格式 输出一个整数,表示最短路径的长度最多会增加多少。 数据范围 $2 \leq n \leq 200$ $1 \leq m \leq n(n-1)/2$ $1 \leq w_i \leq 10^6$ 输入样例 #1: 4 4 1 2 2 2 3 3 3 4 4 4 1 5 输出样例 #1: 5 输入样例 #2: 4 3 1 2 1 2 3 2 3 4 3 输出样例 #2: 2 算法 (BFS+最短路) $O(n^3)$ 我们把问题转化一下,假设原图中没有添加边,所求的就是点 $1$ 到点 $n$ 的最短路,并且我们已经求出了这个最短路的长度 $dis$。 接下来我们从小到大枚举边权 $x$,每次将 $x$ 加入图中,然后再次求解点 $1$ 到点 $n$ 的最短路 $dis'$,那么增加的最短路长度就是 $dis'-dis$。 我们发现,每次加入一个边都需要重新求解最短路。如果我们使用 Dijkstra 算法的话,每次加入一条边需要 $O(m\log m)$ 的时间复杂度,总的时间复杂度就是 $O(m^2\log m)$,无法通过本题。因此我们需要使用更优秀的算法。 观察到 $n$ 的范围比较小,我们可以考虑使用 BFS 求解最短路。如果边权均为 $1$,那么 BFS 可以在 $O(m)$ 的时间复杂度内求解最短路。那么如果我们只是加入了一条边的话,我们可以将边权为 $x$ 的边看做 $x$ 条边的组合,每次加入该边时,我们就在原始图上添加 $x$ 条边,边权均为 $1$。这样,我们就可以使用一次 BFS 求解最短路了。 但是,我们不得不考虑加入多条边的情况。如果我们还是将边权为 $x$ 的边看做 $x$ 条边的组合,那么我们就需要加入 $x$ 条边,而不是一条边。这样,我们就不能使用 BFS 了。 但是,我们可以使用 Floyd 算法。事实上,我们每次加入边时,只有边权等于 $x$ 的边会发生变化。因此,如果我们枚举边权 $x$ 时,每次只需要将边权等于 $x$ 的边加入图中,然后使用 Floyd 算法重新计算最短路即可。由于 Floyd 算法的时间复杂度为 $O(n^3)$,因此总的时间复杂度为 $O(n^4)$。 时间复杂度 $O(n^4)$ 空间复杂度 $O(n^2)$ C++ 代码 注意点:Floyd算法计算任意两点之间的最短路径,只需要在之前的路径基础上加入新的边构成的新路径进行更新即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值