zoukankan      html  css  js  c++  java
  • 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++ 知识比较多,所以代码是直接参考陈锋的题解。

  • 相关阅读:
    jQuerychicun
    css3动画
    app开发,H5+CSS3页面布局小tips
    函数基础
    函数
    冒泡排序
    关于Vue+iview的前端简单的导入数据(excel)
    关于Vue+iview的简单下拉框滚动加载
    ES6中set的用法回顾
    百度地图api设置点的自定义图标不显示
  • 原文地址:https://www.cnblogs.com/fanlumaster/p/14224601.html
Copyright © 2011-2022 走看看