UVA-227

UVA-227

这道题写了有一个半小时,里面除了两三分钟写移动的代码,其他全都在处理输入输出格式,简直吐血。自己习惯STL,但是这次训练中刻意没有用string处理square,代码看起来好丑。

//#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<vector>
#include<cstring>
using namespace std;
bool movesquare(char s[5][5],string &m)
{
    
    
    int idx1 = 0, idx2 = 0;
    for (int i = 0; i< 5; ++i) {
    
    
        for (int j = 0; j < 5; ++j) {
    
    
            if (s[i][j] == ' ') {
    
     idx1 = i; idx2 = j; }
        }
    }
    for (auto i : m) {
    
    
        if (i == 'A') {
    
    
            if (idx1 - 1 < 0) return false;
            s[idx1][idx2] = s[idx1 - 1][idx2]; --idx1; s[idx1][idx2] = ' ';
        }
        else if(i=='B')
        {
    
    
            if (idx1 + 1 > 4) return false;
            s[idx1][idx2] = s[idx1 + 1][idx2]; ++idx1; s[idx1][idx2] = ' ';
        }
        else if(i=='L')
        {
    
    
            if (idx2 - 1 < 0) return false;
            s[idx1][idx2] = s[idx1][idx2 - 1]; --idx2; s[idx1][idx2] = ' ';
        }
        else if(i=='R')
        {
    
    
            if (idx2 + 1 > 4) return false;
            s[idx1][idx2] = s[idx1][idx2 + 1]; ++idx2; s[idx1][idx2] = ' ';
        }
        else if (i == '0') {
    
    
            return true;
        }
    }
    return true;
}
int main()
{
    
    
     //freopen("input.txt", "r", stdin);
    char tmp = 0; int case_ctr = 0;
    while(tmp=getchar()){
    
    
        if (tmp == 'Z') break;
        char square[5][5];
        memset(square, ' ', sizeof(square));
        square[0][0] = tmp;
        int idx1 = 0, idx2 = 1;
        while (idx1 < 5) {
    
    
            tmp = getchar();
            if (tmp == '\n') {
    
    
                ++idx1;
                idx2 = 0;
            }
            else {
    
    
                square[idx1][idx2] = tmp;
                ++idx2;
            }
        }
        string move; 
        while (true) {
    
    
            char tmp; tmp = getchar();
            if (tmp == '0') break;
            if (tmp != '\n') move.push_back(tmp);
        }
        getchar();

        if (case_ctr != 0) printf("\n");
        printf("Puzzle #%d:\n", ++case_ctr);
        if (movesquare(square, move)) {
    
    
            printf("%c %c %c %c %c\n", square[0][0], square[0][1], square[0][2], square[0][3], square[0][4]);
            printf("%c %c %c %c %c\n", square[1][0], square[1][1], square[1][2], square[1][3], square[1][4]);
            printf("%c %c %c %c %c\n", square[2][0], square[2][1], square[2][2], square[2][3], square[2][4]);
            printf("%c %c %c %c %c\n", square[3][0], square[3][1], square[3][2], square[3][3], square[3][4]);
            printf("%c %c %c %c %c\n", square[4][0], square[4][1], square[4][2], square[4][3], square[4][4]);
        }
        else {
    
    
            printf("This puzzle has no final configuration.\n");
        }
    }
    return 0;
}
//用一个二维char数组储存表格
//用getchar()来处理输入,输入tmp;如果输入tmp=z,就break,如果tmp!=z,五个scanf输入
//用一个char move[]储存步骤
//编写一个移动函数,首先查找' '的idx1,idx2(两个循环),然后写一个if else进行移动。

改用vector和string写之后40min AC(忘了加cin.get()是主要耗时)

//#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void print(const vector<string> &s)
{
    
    
    for (auto &i : s) {
    
    
        printf("%c %c %c %c %c\n", i[0], i[1], i[2], i[3], i[4]);
    }
}
bool moveSquare(vector<string> &s,const string &m)
{
    
    
    int idx1 = 0, idx2 = 0;
    for (int i = 0; i < 5; ++i) {
    
    
        for (int j = 0; j < 5; ++j) {
    
    
            if (s[i][j] == ' ') {
    
     idx1 = i; idx2 = j; }
        }
    }
    for (auto &i : m) {
    
    
        if (i == '0') return true;
        else if (i == 'A') {
    
    
            if (idx1 - 1 < 0) return false;
            else {
    
     s[idx1][idx2] = s[idx1 - 1][idx2]; --idx1; s[idx1][idx2] = ' '; }
        }
        else if (i == 'B') {
    
    
            if (idx1 + 1 > 4) return false;
            else {
    
     s[idx1][idx2] = s[idx1 + 1][idx2]; ++idx1; s[idx1][idx2] = ' '; }
        }
        else if (i == 'L') {
    
    
            if (idx2 - 1 < 0) return false;
            else {
    
     s[idx1][idx2] = s[idx1][idx2 - 1]; --idx2; s[idx1][idx2] = ' '; }
        }
        else if (i == 'R') {
    
    
            if (idx2 + 1 > 4) return false;
            else {
    
     s[idx1][idx2] = s[idx1][idx2 + 1]; ++idx2; s[idx1][idx2] = ' '; }
        }
    }
}
int main()
{
    
    
    //freopen("input.txt", "r", stdin);
    int case_ctr = 0;
    while (true) {
    
    
        vector<string> square; string tmp;
        getline(cin, tmp);
        if (tmp[0] == 'Z') break;
        square.push_back(tmp);
        for (int i = 1; i < 5; ++i) {
    
    
            getline(cin, tmp);
            square.push_back(tmp);
        }
        for (auto &i : square) {
    
    
            if (i.size() == 4) i.push_back(' ');
        }
        string move;
        do {
    
    
            string tmp; cin >> tmp;
            move += tmp;
        } while (!move.empty() && move.back() != '0');
        cin.get();

        if (case_ctr != 0) printf("\n");
        printf("Puzzle #%d:\n", ++case_ctr);
        if (moveSquare(square, move)) {
    
    
            print(square);
        }
        else {
    
    
            printf("This puzzle has no final configuration.\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u011917745/article/details/113804673