zoukankan      html  css  js  c++  java
  • Bloxorz I (poj3322) (BFS)

    【题目描述】

    	 It's a game about rolling a box to a specific position on a special plane. Precisely, the plane, which is composed of several unit cells, is a rectangle shaped area. And the box, consisting of two perfectly aligned unit cube, may either lies down and occupies two neighbouring cells or stands up and occupies one single cell. One may move the box by picking one of the four edges of the box on the ground and rolling the box 90 degrees around that edge, which is counted as one move. There are three kinds of cells, rigid cells, easily broken cells and empty cells. A rigid cell can support full weight of the box, so it can be either one of the two cells that the box lies on or the cell that the box fully stands on. A easily broken cells can only support half the weight of the box, so it cannot be the only cell that the box stands on. An empty cell cannot support anything, so there cannot be any part of the box on that cell. The target of the game is to roll the box standing onto the only target cell on the plane with minimum moves.
    

    【算法】

    	貌似没啥算法,就是BFS,但是写起来很烦,%lyd,大佬的代码为什么就这么清晰明了。。。。
    

    【题目链接】

    Bloxorz I

    【代码】

    #include <stdio.h>
    #include <queue>
    using namespace std;
    struct rec{ int x,y,state; }st,ed;
    char s[510][510];
    int m,n,d[510][510][3];
    queue<rec> q;
    const int dx[]={0,0,-1,1},dy[]={-1,1,0,0};
    bool valid(int x,int y) {
        return x>=1&&x<=m&&y>=1&&y<=n;
    }
    void parse_st_ed() {
        for(int i=1;i<=m;i++) {
            for(int j=1;j<=n;j++) {
                if(s[i][j]=='O') {
                    ed.x=i,ed.y=j,s[i][j]='.';
                }else if(s[i][j]=='X') {
                    for(int k=0;k<4;k++) {
                        int x=i+dx[k],y=j+dy[k];
                        if(valid(x,y)&&s[x][y]=='X') {
                            st.x=min(x,i),st.y=min(y,j);
                            st.state=x==i?1:2;
                            s[i][j]=s[x][y]='.';
                            break;
                        }
                    }
                    if(s[i][j]=='X') st.x=i,st.y=j,st.state=0;
                }
            }
        }
    }
    const int next_x[3][4]={ {0,0,-2,1},{0,0,-1,1},{0,0,-1,2} };
    const int next_y[3][4]={ {-2,1,0,0},{-1,2,0,0},{-1,1,0,0} };
    const int next_state[3][4]={ {1,1,2,2},{0,0,1,1},{2,2,0,0} };
    bool valid(rec k) {
        if(!valid(k.x,k.y)) return 0;
        if(s[k.x][k.y]=='#') return 0;
        if(k.state==0&&s[k.x][k.y]=='E') return 0;
        if(k.state==1&&s[k.x][k.y+1]=='#') return 0;
        if(k.state==2&&s[k.x+1][k.y]=='#') return 0;
        return 1;
    }
    int bfs() {
        while(q.size()) q.pop();
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++)
                d[i][j][0]=d[i][j][1]=d[i][j][2]=-1;
        d[st.x][st.y][st.state]=0;
        q.push(st);
        while(q.size()) {
            rec now=q.front(); q.pop();
            for(int i=0;i<4;i++) {
                rec next;
                next.x=now.x+next_x[now.state][i];
                next.y=now.y+next_y[now.state][i];
                next.state=next_state[now.state][i];
                if(!valid(next)) continue;
                if(d[next.x][next.y][next.state]==-1) {
                    d[next.x][next.y][next.state]
                        =d[now.x][now.y][now.state]+1;
                    if(next.x==ed.x&&next.y==ed.y&&!next.state) return d[next.x][next.y][0];
                    q.push(next);
                }
            }
        }
        return -1;
    }
    int main() {
        while(~scanf("%d%d",&m,&n)&&m) {
            for(int i=1;i<=m;i++) scanf("%s",s[i]+1);
            parse_st_ed();
            int ans=bfs();
            if(ans==-1) puts("Impossible");
            else printf("%d
    ",ans);
        }
        return 0;
    }
    
    
  • 相关阅读:
    预处理器宏指令(Macro)
    汇编语言中macro的用法
    USB设备的VID与PID
    前端工具 | JS编译器Monaco使用教程
    vue + ts中的shimsvue.d.ts文件的作用,在ts中引入vueecharts等vue文件 TypeScript 导入 JSON Module resolveJsonModule
    Jenkins自动打包并部署到远程服务器
    如何获取设备的VID,PID?
    TypeScript装饰器(decorators)
    MACRO指令
    IE6左右边框断线现象
  • 原文地址:https://www.cnblogs.com/Willendless/p/9530928.html
Copyright © 2011-2022 走看看