bzoj 5138 [Usaco2017 Dec]Push a Box

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值