码迷,mamicode.com
首页 > 其他好文 > 详细

BFS POJ3322 Bloxorz I

时间:2017-08-19 21:14:12      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:xor   first   using   blog   contains   ecif   sub   download   tin   

Bloxorz I
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6108   Accepted: 2007

Description

Little Tom loves playing games. One day he downloads a little computer game called ‘Bloxorz‘ which makes him excited. 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.

技术分享
The box stands on a single cell
技术分享
The box lies on two neighbouring cells, horizontally
技术分享
The box lies on two neighbouring cells, vertically

After Little Tom passes several stages of the game, he finds it much harder than he expected. So he turns to your help.

Input

Input contains multiple test cases. Each test case is one single stage of the game. It starts with two integers R and C(3 ≤ R, C ≤ 500) which stands for number of rows and columns of the plane. That follows the plane, which contains R lines and C characters for each line, with ‘O‘ (Oh) for target cell, ‘X‘ for initial position of the box, ‘.‘ for a rigid cell, ‘#‘ for a empty cell and ‘E‘ for a easily broken cell. A test cases starts with two zeros ends the input.

It guarantees that

  • There‘s only one ‘O‘ in a plane.
  • There‘s either one ‘X‘ or neighbouring two ‘X‘s in a plane.
  • The first(and last) row(and column) must be ‘#‘(empty cell).
  • Cells covered by ‘O‘ and ‘X‘ are all rigid cells.

Output

For each test cases output one line with the minimum number of moves or "Impossible" (without quote) when there‘s no way to achieve the target cell.  

Sample Input

7 7
#######
#..X###
#..##O#
#....E#
#....E#
#.....#
#######
0 0

Sample Output

10

好久没有写blog了,今天spli写这个题,他竟然自嘲自己代码丑,真是扯,我的才丑呢!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 int n,m,endx,endy;
 8 char ch[510][510];
 9 bool check[510][510][3];
10 int dx0[4]={1,-2,0,0},dy0[4]={0,0,1,-2},ds0[4]={1,1,2,2},dx1[4]={2,-1,0,0},dy1[4]={0,0,1,-1},ds1[4]={0,0,1,1},dx2[4]={1,-1,0,0},dy2[4]={0,0,-1,2},ds2[4]={2,2,0,0};
11 struct data{
12     int x,y,cnt,state;
13 };
14 queue<data>q;
15 void prework(){
16     endx=0,endy=0;
17     memset(check,0,sizeof(check));
18     while(!q.empty()) q.pop();
19     for(int i=1;i<=n;i++) scanf("%s",&ch[i]+1);
20     for(int i=1;i<=n;i++)
21         for(int j=1;j<=m;j++){
22             if(ch[i][j]==O) endx=i,endy=j;
23             if(ch[i][j]==#){
24                 check[i][j][0]=1,check[i][j][1]=1,check[i][j][2]=1;
25                 if(i>=2) check[i-1][j][1]=1;
26                 if(j>=2) check[i][j-1][2]=1;
27             }
28             if(ch[i][j]==X){
29                 bool flag=0;
30                 if(ch[i+1][j]==X) q.push((data){i,j,0,1}),check[i][j][1]=1,flag=1;
31                 if(ch[i][j+1]==X) q.push((data){i,j,0,2}),check[i][j][2]=1,flag=1;
32                 if(!flag) q.push((data){i,j,0,0}),check[i][j][0]=1;
33             }
34         }
35 }
36 void bfs(){
37     while(!q.empty()){
38         data p=q.front();
39         q.pop();
40         if((p.x<1||p.x>n||p.y<1||p.y>m)||(!p.state&&ch[p.x][p.y]==E)||(p.state==1&&p.x+1>n)||(p.state==2&&p.y+1>m)) continue;
41         if(p.x==endx&&p.y==endy&&!p.state){
42             printf("%d\n",p.cnt);
43             return;
44         }
45         if(!p.state)
46             for(int i=0;i<=3;i++)
47                 if(!check[p.x+dx0[i]][p.y+dy0[i]][ds0[i]]&&p.x+dx0[i]>=1&&p.x+dx0[i]<=n&&p.y+dy0[i]>=1&&p.y+dy0[i]<=m) q.push((data){p.x+dx0[i],p.y+dy0[i],p.cnt+1,ds0[i]}),check[p.x+dx0[i]][p.y+dy0[i]][ds0[i]]=1;
48         if(p.state==1){
49             for(int i=0;i<=1;i++) 
50                 if(!check[p.x+dx1[i]][p.y+dy1[i]][ds1[i]]&&p.x+dx1[i]>=1&&p.x+dx1[i]<=n&&p.y+dy1[i]>=1&&p.y+dy1[i]<=m&&ch[p.x+dx1[i]][p.y+dy1[i]]!=E) q.push((data){p.x+dx1[i],p.y+dy1[i],p.cnt+1,ds1[i]}),check[p.x+dx1[i]][p.y+dy1[i]][ds1[i]]=1;
51             for(int i=2;i<=3;i++) 
52                 if(!check[p.x+dx1[i]][p.y+dy1[i]][ds1[i]]&&p.x+dx1[i]>=1&&p.x+dx1[i]<=n&&p.y+dy1[i]>=1&&p.y+dy1[i]<=m) q.push((data){p.x+dx1[i],p.y+dy1[i],p.cnt+1,ds1[i]}),check[p.x+dx1[i]][p.y+dy1[i]][ds1[i]]=1;
53         }
54         if(p.state==2){
55             for(int i=0;i<=1;i++) 
56                 if(!check[p.x+dx2[i]][p.y+dy2[i]][ds2[i]]&&p.x+dx2[i]>=1&&p.x+dx2[i]<=n&&p.y+dy2[i]>=1&&p.y+dy2[i]<=m) q.push((data){p.x+dx2[i],p.y+dy2[i],p.cnt+1,ds2[i]}),check[p.x+dx2[i]][p.y+dy2[i]][ds2[i]]=1;
57             for(int i=2;i<=3;i++) 
58                 if(!check[p.x+dx2[i]][p.y+dy2[i]][ds2[i]]&&p.x+dx2[i]>=1&&p.x+dx2[i]<=n&&p.y+dy2[i]>=1&&p.y+dy2[i]<=m&&ch[p.x+dx2[i]][p.y+dy2[i]]!=E) q.push((data){p.x+dx2[i],p.y+dy2[i],p.cnt+1,ds2[i]}),check[p.x+dx2[i]][p.y+dy2[i]][ds2[i]]=1;
59         }
60     }
61     printf("Impossible\n");
62     return;
63 }
64 int main(){
65     while(scanf("%d%d",&n,&m)&&n&&m){
66         prework();
67         bfs();
68     }
69     return 0;
70 }

 



BFS POJ3322 Bloxorz I

标签:xor   first   using   blog   contains   ecif   sub   download   tin   

原文地址:http://www.cnblogs.com/sdfzxh/p/7397722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!