poj 3322 Bloxorz I

    技术2023-03-19  45

    题意:就是一个简单的游戏。

    思路:这个题用bfs,砖块有三种状态,即:一个点,水平,竖直,分别用0,1,2来表示

    这里要注意的是需要用到一个坐标和状态的关系的数组,这个数组要慢慢写,很容易就错的,当时没注意就写错了一次

     

    int d[3][4][3]={

        {{0,-2,1},{0,1,1},{-2,0,2},{1,0,2}},

        {{0,-1,-1},{0,2,-1},{-1,0,0},{1,0,0}},

        {{0,-1,0},{0,1,0},{-1,0,-2},{2,0,-2}}

                    };                                                                      分别表示x,y和状态

    只要这些搞定了,就是一个简单的bfs

    /* * File: main.cpp * Author: mi * * Created on 2011年1月18日, 下午1:02 */ #include <cstdlib> #include <stdio.h> #include <queue> #include <iostream> #include <string.h> #define N 501 using namespace std; /* * */ struct node { int x,y,step,state; }p[700000],start,end,temp[2]; int d[3][4][3]={ {​{0,-2,1},{0,1,1},{-2,0,2},{1,0,2}}, {​{0,-1,-1},{0,2,-1},{-1,0,0},{1,0,0}}, {​{0,-1,0},{0,1,0},{-1,0,-2},{2,0,-2}} }; int n,m; char map[N][N]; int use[N][N][3]; bool ok(int x,int y,int state) { if(state==0) return x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#'&&map[x][y]!='E'; else if(state==1) return x>=0&&x<n&&y>=0&&y+1<m&&map[x][y]!='#'&&map[x][y+1]!='#'; else return x>=0&&x+1<n&&y>=0&&y<m&&map[x][y]!='#'&&map[x+1][y]!='#'; } int bfs() { int i; queue<node> q; q.push(start); while(!q.empty()) { node cur; cur=q.front(); // printf("x==%d y==%d step==%d state==%d/n",cur.x,cur.y,cur.step,cur.state); if(cur.x==end.x&&cur.y==end.y&&cur.state==0) return cur.step; q.pop(); for(i=0;i<4;i++) { int tx,ty,tstate; tx=cur.x+d[cur.state][i][0]; ty=cur.y+d[cur.state][i][1]; tstate=cur.state+d[cur.state][i][2]; if(ok(tx,ty,tstate)&&!use[tx][ty][tstate]) { node next; next.x=tx,next.y=ty,next.state=tstate,next.step=cur.step+1; use[next.x][next.y][next.state]=1; q.push(next); } } } return -1; } int main(int argc, char** argv) { int i,j,cnt,ans; char s[N]; while(scanf("%d%d",&n,&m)&&(n||m)) { cnt=0; ans=0; memset(use,0,sizeof(use)); for(i=0;i<n;i++) { scanf("%s",map[i]); for(j=0;j<m;j++) { if(map[i][j]=='X') temp[cnt].x=i,temp[cnt++].y=j; if(map[i][j]=='O') end.x=i,end.y=j; } } start.x=temp[0].x,start.y=temp[0].y,start.step=0; if(cnt==1) start.state=0; else { if(temp[0].y==temp[1].y) start.state=2; else start.state=1; } use[start.x][start.y][start.step]=1; ans=bfs(); if(ans==-1) puts("Impossible"); else printf("%d/n",ans); } return 0; }  

     

    最新回复(0)