zoukankan      html  css  js  c++  java
  • poj1753

    简单的BFS,加上用位操作来定义和改写状态。ac时运行了100多ms,看discussion里面有人跑的很快,应该有更优化的方法吧,没细究了。

    #include <cstdio>
    #include <queue>
    using namespace std;
    
    const int ROW = 4;
    const int ajacent[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
    
    struct Status{
        int s;
        int step;
    };
    
    int getStatus(char** board)
    {
        int ans = 0;
        for (int i = 0; i < ROW; i++){
            for (int j = 0; j < ROW; j++){
                ans <<= 1;
                if (board[i][j] == 'b'){
                    ans++;
                }
                
            }
        }
        return ans;
    }
    
    int main()
    {
        char* board[ROW];
        bool visited[65536];
        memset(visited, 0, sizeof(visited));
        queue<Status> Q;
        for (int i = 0; i < ROW; i++){
            board[i] = (char*)malloc(5 * sizeof(char));
        }
        for (int i = 0; i < ROW; i++){
            scanf("%s", board[i]);
        }
        Status tmpSt;
        tmpSt.s = getStatus(board);
        tmpSt.step = 0;
        Q.push(tmpSt);
        visited[tmpSt.s] = true;
        while (!Q.empty()){
            Status current = Q.front();
            if (current.s == 0 || current.s == 0xffff)
                break;
            for (int i = 0; i < ROW; i++){
                for (int j = 0; j < ROW; j++){
                    int tmpS = current.s ^ (1 << (ROW * i + j));
                    for (int k = 0; k < 4; k++){
                        int tmpI = i + ajacent[k][0],
                            tmpJ = j + ajacent[k][1];
                        if (tmpI >= 0 && tmpI < 4 && tmpJ >= 0 && tmpJ < 4){
                            tmpS ^= (1 << (ROW * tmpI + tmpJ));
                        }
                    }
                    if (!visited[tmpS]){
                        tmpSt.s = tmpS;
                        tmpSt.step = current.step + 1;
                        Q.push(tmpSt);
                        visited[tmpS] = true;
                    }
                }
            }
            Q.pop();
        }
        if (!Q.empty()){
            printf("%d
    ", Q.front().step);
        }
        else{
            printf("Impossible
    ");
        }
        return 0;
    }
  • 相关阅读:
    Hadoop作业提交多种方案具体流程详解
    教你一步搭建Flume分布式日志系统
    Hadoop详细配置
    linux 运行springboot sqoop项目
    SQL+ 正则表达式
    SQL+ 正则表达式
    DB2中实现正则表达式
    DB2中实现正则表达式
    spring4整合cxf3
    spring4整合cxf3
  • 原文地址:https://www.cnblogs.com/caiminfeng/p/4911727.html
Copyright © 2011-2022 走看看