UVA-227 Puzzle(模拟)

题目:

题目浏览传送门

题意:

给出一个5*5的方格,里边有一个格子是空的,现在给出一串指令,A->空格向上移动,B->空格向下移动,R->空格向右移动,L->空格向左移动。

输出移动后的结果。

思路:

直接上模拟就好了,不过就是输入处理有点恶心,最好用scanf和printf来处理输入输出。

1、空格移动出界和出现不是‘A’、‘B’、‘R’、‘L’中的指令,这两种情况都算是“This puzzle has no final configuration.”。

2、另外在处理指令的时候,遇到1中的情况只要标记一下就好了,最终统一在‘0’的位置退出循环。(WA到怀疑人生)

例如这个样例:

AAAAA
BBBBB
NNNNN
JJJJJ
UUUU
AQ
0
Z

3、输出两两之间用一个空行隔开。

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin)
using namespace std;
typedef long long ll;
const int maxn = 5e3+10;
int mp[6][6];

void toSwap(int& a,int& b){
    int t = a;
    a = b,b = t;
}

bool Move(char op, int& r,int& c) {
    if(op=='A') {
        if(r-1 == 0)
            return false;
        toSwap(mp[r][c],mp[r-1][c]);
        r--;
        return true;
    } else if(op=='B') {
        if(r+1 == 6)
            return false;
        toSwap(mp[r][c],mp[r+1][c]);
        r++;
        return true;
    } else if(op == 'R') {
        if(c+1 == 6)
            return false;
        toSwap(mp[r][c],mp[r][c+1]);
        c++;
        return true;
    } else if(op == 'L'){
        if(c-1 == 0)
            return false;
        toSwap(mp[r][c],mp[r][c-1]);
        c--;
        return true;
    }
    return false;
}


void check() {
    for(int i = 1; i<=5; i++) {
        for(int j = 1; j<=5; j++) {
            if(j!=1)
                printf(" ");
            printf("%c",mp[i][j]+'A');
        }
        printf("\n");
    }
}

int main() {
    //FRE();
    char str[6];
    int cnt = 0;
    while(1) {
        int len;
        for(int i = 1; i<=5; i++) {
            gets(str);
            len = strlen(str);
            if(i == 1 && len == 1 && str[0] == 'Z'){
                return 0;
            }
            for(int j = 0; j<len; j++) {
                mp[i][j+1] = str[j]-'A';
            }
            if(len == 4){
                mp[i][5] = -33;
            }
        }
        int r,c;
        for(int i = 1; i<=5; i++) {
            for(int j = 1; j<=5; j++) {
                if(mp[i][j] < 0) {
                    r = i;
                    c = j;
                }
            }
        }
        //check();
        char op[10000];
        bool ok = true;
        while(1) {
            bool isend = false;
            gets(op);
            //cout<<op<<" GG "<<endl;
            len = strlen(op);
            for(int i = 0; i<len; i++) {
                //cout<<op[i]<<endl;
                if(op[i] == '0') {
                    isend = true;
                }
                else if(!Move(op[i],r,c)){
                    ok = false;
                }
            }
            if(isend) {
                break;
            }
        }
        //cout<<"HH"<<endl;
        if(cnt != 0){
            printf("\n");
        }
        if(!ok){
            printf("Puzzle #%d:\n",++cnt);
            printf("This puzzle has no final configuration.\n");
        }else {
            printf("Puzzle #%d:\n",++cnt);
            check();
        }
    }
    return 0;
}
/*
PutIn:
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z
PutOut:
Puzzle #1:
T R G S J
X O K L I
M D V B N
W P A E
U Q H C F
Puzzle #2:
A B C D
F G H I E
K L M N J
P Q R S O
T U V W X
Puzzle #3:
This puzzle has no final configuration.
*/
View Code

猜你喜欢

转载自www.cnblogs.com/sykline/p/9986608.html