• UVa227


    题目链接

    https://vjudge.net/problem/UVA-227

    代码

    #include <iostream>
    #include <vector>
    #include <map>
    
    using namespace std;
    
    struct Point
    {
        int x, y;
        Point (int x = 0, int y = 0): x(x), y(y) {}
    };
    typedef Point Vector;
    
    Point operator + (const Point& A, const Point& B)
    {
        return Point(A.x + B.x, A.y + B.y);
    }
    
    const int GSize = 5; // 网格的行数和列数
    vector<string> grid;
    Point ePos;
    map<char, Vector> DIRS;
    
    // 判断坐标是否会越界
    bool valid(const Point &p)
    {
        return p.x >= 0 && p.x < GSize && p.y >= 0 && p.y < GSize;
    }
    
    void printGrid()
    {
        for (int i = 0; i < GSize; i++)
        {
            for (int j = 0; j < GSize; j++)
            {
                if (j) printf(" ");
                printf("%c", grid[i][j]);
            }
            printf("
    ");
        }
    }
    
    bool tryMove(char cmd)
    {
        if (!DIRS.count(cmd)) return false;
        Point p = ePos + DIRS[cmd]; // 移动空的格子
        if (!valid(p)) return false;
        swap(grid[p.x][p.y], grid[ePos.x][ePos.y]); // 交换空格和移动之后的空格的位置本来存储的数据
        ePos = p; // 更新 empty Position
        return true;
    }
    
    int main()
    {
        int t = 1;
        string line;
        DIRS['A'] = Vector(-1, 0);
        DIRS['B'] = Vector(1, 0);
        DIRS['L'] = Vector(0, -1);
        DIRS['R'] = Vector(0, 1);
    
        while (true)
        {
            grid.clear();
            ePos.x = -1; ePos.y = -1;
            for (int i = 0; i < GSize; i++)
            {
                getline(cin, line);
                if (line == "Z") return 0;
                for (int j = 0; j < GSize; j++)
                    if (line[j] == ' ')
                    {
                        ePos.x = i;
                        ePos.y = j;
                    }
                grid.push_back(line);
            }
            string moves;
            while (true)
            {
                getline(cin, line);
                bool end = *(line.rbegin()) == '0';
                if (!end)
                    moves.append(line);
                else
                    moves.append(line, 0, line.size() - 1); // 如果最后一个字符是 0,那么就去掉最后的 0
                if (end) break;
            }
            bool legal = true;
            for (int i = 0; i < moves.size(); i++)
            {
                if (!tryMove(moves[i]))
                {
                    legal = false;
                    break;
                }
            }
            if (t > 1) printf("
    ");
            printf("Puzzle #%d:
    ", t++);
            if (legal) printGrid();
            else printf("This puzzle has no final configuration.
    ");
        }
    }
    

    按:这题涉及的 C++ 知识比较多,所以代码是直接参考陈锋的题解。

  • 相关阅读:
    一句话解释c#中的特性,你了解多少
    CentOS虚拟机如何设置共享文件夹,并在Windows下映射网络驱动器?
    samba服务器配置及window网络磁盘映射
    PHP中各种Hash算法性能比较
    Redis持久化
    设置redis主从出现的问题
    Redis启动多端口、运行多实例
    Redis学习手册(主从复制)
    tengine/nginx-tomcat动静分离遇到的问题
    使用 Git Hooks 实现自动项目部署
  • 原文地址:https://www.cnblogs.com/fanlumaster/p/14224601.html
Copyright © 2020-2023  润新知