sstfxjq 2023-06-14 18:37 采纳率: 66.7%
浏览 19

算法竞赛入门经典(第二版) | 习题3-5 谜题 (UVa227,Puzzle)设计编程时遇到的存储错误。以及对您程序的提问

算法竞赛入门经典(第二版) | 习题3-5 谜题 (UVa227,Puzzle)
对您代码的提问:为什么a[0][0]要单独赋值呢?
您的程序是如何解决空格在行末这个问题的呢?

我编程遇到的问题:
不知道为什么下一组输入的时候总是会把上一组谜图输入的最后一行保留下来,哪怕最后清零它也会。
导致数据存下来是混乱的。希望您帮忙看看,是哪里错了?
因为代码复制过来格式是错的,希望您能打开uva,看下评论区。我的代码在那里面

  • 写回答

1条回答 默认 最新

  • threenewbee 2023-06-16 14:15
    关注
    #include<iostream>
    #include<string>
    using namespace std;
    
    //全局变量:存放5*5网格相关数据
    char puzzle[6][6];//存放网格数组
    int x0, y0;//存放空格的位置(下标从0开始),第x行,第y列
    
    //将读入的一串字符串分别存储到网格数组中去
    void read(int row, string str) {
        for (int i = 0; i < 5; i++) {
            puzzle[row][i] = str[i];
            if (str[i] == ' ') {
                x0 = row;
                y0 = i;
            }
        }
    }
    
    bool move(char order) {
        int x, y;//移动后空格的坐标
        switch (order)
        {
            //上
        case 'A':
            x = x0 - 1; y = y0;
            break;
            //下
        case 'B':
            x = x0 + 1; y = y0;
            break;
            //左
        case 'L':
            x = x0; y = y0 - 1;
            break;
            //右
        case 'R':
            x = x0; y = y0 + 1;
            break;
        default:
            break;
        }
    
        if ((x >= 0 && x <= 4) && (y >= 0 && y <= 4)) {
            puzzle[x0][y0] = puzzle[x][y];
            puzzle[x][y] = ' ';
            x0 = x; y0 = y;
    
            return true;
        }
        else {
            return false;
        }
    }
    
    
    int main()
    {
        string order;//存放指令序列
        string str;//输入网格数组时的中间存储量
        int icase = 0;//求解问题的次数
    
        while (1) {
            if (icase != 0)
                cin.get();//读取掉输入缓冲区的换行符
    
            //读取一行字符并处理 
            getline(cin, str);
            if (str[0] == 'Z')
                break;
    
            //不同样例间换行
            if (icase != 0)
                cout << endl;
    
            read(0, str);
            for (int i = 1; i <= 4; i++) {
                getline(cin, str);
                read(i, str);
            }
    
            //读取指令序列
            order.clear();
            int len;
            while (true) {
                string temp;
                cin >> temp;
    
                order += temp;
                len = order.length();
    
                if (order[len - 1] == '0')
                    break;
            }
    
            //看是否有解
            bool has_result = true;
            for (int i = 0; i < len - 1; i++) {
                if (move(order[i]) == false) {
                    has_result = false;
                    break;
                }
            }
    
            cout << "Puzzle #" << ++icase << ":" << endl;
    
            if (has_result) {
                for (int i = 0; i < 5; i++) {
                    for (int j = 0; j < 4; j++) {
                        cout << puzzle[i][j] << " ";
                    }
                    cout << puzzle[i][4] << endl;
                }
    
            }
            else {
                cout << "This puzzle has no final configuration." << endl;
            }
        }
    
        system("pause");
        return  0;
    }
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 6月14日