Poj3322 Bloxorz I

Source: Internet
Author: User

Poj3322 Bloxorz I

Classic box games

The minimum number of steps for a 1*2*1 brick reaches a specified hole.

Obviously bfs, the State indicates that a P value 0, 1, and 2 respectively indicate that the bricks are standing, lying horizontally and lying vertically, when weighting, use a three-dimensional array to obtain vis [p state] [row position] [column position].

Then, you can directly move from one state to another, and change the coordinates at the same time.


#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include using namespace std;//LOOP#define FE(i, a, b) for(int i = (a); i <= (b); ++i)#define REP(i, N) for(int i = 0; i < (N); ++i)#define CLR(A,value) memset(A,value,sizeof(A))//OTHER#define PB push_back//INPUT#define RI(n) scanf("%d", &n)#define RII(n, m) scanf("%d%d", &n, &m)#define RS(s) scanf("%s", s)typedef long long LL;typedef unsigned long long ULL;typedef vector 
         
           VI;const double eps = 1e-9;const int MOD = 1000000007;const double PI = acos(-1.0);const int INF = 0x3f3f3f3f;const int maxn = 110;int n, m;char s[510][510];int d[510][510][3];bool vis[510][510][3];int dir[][4][3] = {{{-2, 0, 2}, {0, 1, 1}, {1, 0, 2}, {0, -2, 1}}, {{-1, 0, 0}, {0, 2, -1}, {1, 0, 0}, {0, -1, -1}}, {{-1, 0, -2}, {0, 1, 0}, {2, 0, -2}, {0, -1, 0}} };struct Node{ int x, y; int p; Node(){} Node(int x, int y, int p): x(x), y(y), p(p){} bool operator == (const Node& a) const { return x == a.x && y == a.y && p == a.p; }}st, ed, now, nxt;inline void getorder(int &x1, int &y1, int &x2, int &y2, int &p){ if (x1 > x2 || (x1 == x2 && y1 > y2)) { swap(x1, x2); swap(y1, y2); } if (x1 == x2) p = 1; else p = 2; if (x1 == x2 && y1 == y2) p = 0;}vector
          
            > stv;bool ok(int x, int y, int p){ if (x < 0 || x >= n || y >= m || y < 0) return 0; if (p == 0) { if (s[x][y] == '#' || s[x][y] == 'E') return 0; return 1; } if (s[x][y] == '#') return 0; int x2, y2; if (p == 1) x2 = x, y2 = y + 1; if (p == 2) x2 = x + 1, y2 = y; if (x2 < 0 || x2 >= n || y2 >= m || y2 < 0) return 0; if (s[x2][y2] == '#') return 0; return 1;}int bfs(){ if (st == ed) return 0; queue
           
            q; int x, y, p; CLR(vis, 0); d[st.x][st.y][st.p] = 0; vis[st.x][st.y][st.p] = 1; q.push(st); while (!q.empty()) { now = q.front(); q.pop(); REP(i, 4) { x = now.x + dir[now.p][i][0]; y = now.y + dir[now.p][i][1]; p = now.p + dir[now.p][i][2]; if (!ok(x, y, p) || vis[x][y][p]) continue; nxt.x = x, nxt.y = y, nxt.p = p; if (nxt == ed) return d[now.x][now.y][now.p] + 1; d[x][y][p] = d[now.x][now.y][now.p] + 1; vis[x][y][p] = 1; q.push(nxt); } } return -1;}int main(){ //freopen("0.txt", "r", stdin); while (~RII(n, m) && n + m) { stv.clear(); REP(i, n) { RS(s[i]); REP(j, m) { if (s[i][j] == 'X') { s[i][j] = '.'; stv.push_back(make_pair(i, j)); } if (s[i][j] == 'O') { s[i][j] = '.'; ed.x = i; ed.y = j; ed.p = 0; } } } int pp = 0; if (stv.size() == 2) getorder(stv[0].first, stv[0].second, stv[1].first, stv[1].second, pp); st.x = stv[0].first; st.y = stv[0].second; st.p = pp; int ans = bfs(); if (ans == -1) puts("Impossible"); else printf("%d\n", ans); } return 0;}
           
          
         
        
       
      
     
    
   
  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.