【POJ 3322】 Bloxorz I

【题目链接】

           http://poj.org/problem?id=3322

【算法】

           广度优先搜索

【代码】

          

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h> 
using namespace std;
#define MAXN 510

int i,j,n,m;
char mp[MAXN][MAXN];

const int dx[4] = {0,0,-1,1};
const int dy[4] = {-1,1,0,0};
const int nx[3][4] = {{0,0,-2,1},{0,0,-1,1},{0,0,-1,2}};
const int ny[3][4] = {{-2,1,0,0},{-1,2,0,0},{-1,1,0,0}};
const int nxt[3][4] = {{1,1,2,2},{0,0,1,1},{2,2,0,0}};

struct info
{
        int x,y;
        int state;
};
inline bool ok(int x,int y)
{
        return x >= 1 && x <= n && y >= 1 && y <= m;
}
inline bool check(int x,int y,int state)
{
        if (!ok(x,y)) return false;
        if (state == 0 && (mp[x][y] == '#' || mp[x][y] == 'E')) return false;
        if (state == 1 && (!ok(x,y+1) || mp[x][y] == '#' || mp[x][y+1] == '#')) return false;
        if (state == 2 && (!ok(x+1,y) || mp[x][y] == '#' || mp[x+1][y] == '#')) return false;
        return true;
}
inline void bfs()
{
        int i,j,k,tx,ty,ts;
        info s,e,cur;
        queue< info > q;
        static int dist[MAXN][MAXN][3];
        while (!q.empty()) q.pop();
        for (i = 1; i <= n; i++)
        {
                for (j = 1; j <= m; j++)
                {
                        for (k = 0; k < 3; k++)
                        {
                                dist[i][j][k] = -1;
                        }
                }
        }
        for (i = 1; i <= n; i++)
        {
                for (j = 1; j <= m; j++)
                {
                        if (mp[i][j] == 'X')
                        {
                                s.x = i;
                                s.y = j;
                                s.state = 0;
                                for (k = 0; k < 4; k++)
                                {
                                        tx = i + dx[k];
                                        ty = j + dy[k];
                                        if (ok(tx,ty) && mp[tx][ty] == 'X')
                                        {
                                                s.x = min(i,tx);
                                                s.y = min(j,ty);
                                                if (k < 2) s.state = 1;
                                                else s.state = 2;
                                        }
                                }
                        }
                        if (mp[i][j] == 'O')
                        {
                                e.x = i;
                                e.y = j;
                                e.state = 0;        
                        }
                }
        }        
        dist[s.x][s.y][s.state] = 0;
        q.push(s);
        while (!q.empty())
        {
                cur = q.front();
                q.pop();
                for (i = 0; i < 4; i++)
                {
                        tx = cur.x + nx[cur.state][i];
                        ty = cur.y + ny[cur.state][i];
                        ts = nxt[cur.state][i];
                        if (check(tx,ty,ts) && dist[tx][ty][ts] == -1)
                        {
                                q.push((info){tx,ty,ts});
                                dist[tx][ty][ts] = dist[cur.x][cur.y][cur.state] + 1;
                                if (tx == e.x && ty == e.y && ts == e.state)
                                {
                                        printf("%d\n",dist[tx][ty][ts]);
                                        return;
                                }
                        }
                }
        }
        printf("Impossible\n");
}

int main() 
{
        
        while (scanf("%d%d",&n,&m) && n && m)
        {
                getchar(); 
                for (i = 1; i <= n; i++)
                {
                        for (j = 1; j <= m; j++)
                        {
                                mp[i][j] = getchar();
                        }        
                        getchar();
                }        
                bfs();
        }
            
        return 0;
    
}

猜你喜欢

转载自www.cnblogs.com/evenbao/p/9264571.html