UVa227 - Puzzle 题解

题目

题目链接

UVa227 - Puzzle

题目大意

一个拼图由5x5的格子构成。每个格子上有一个独一无二的字母,还有一个格子是空的。可以执行4种移动让拼图的格局发生变化,分别为A,B,L,R。Above为将空格子上面的格子移动;Below为将空格子下面的格子移动;Left为将空格子左边的格子移动;Right为将空格子右边的格子移动。比如这个移动为ARRBBL。移动ARRBBL的前后状态

输入由几个拼图组成。每个拼图由初始状态和移动序列构成。每个拼图的前5行为迷宫描述,接下来的行给移动序列。拼图中的格子由字母组成,空格子用空格代替。移动序列包含若干个A,B,L,R,移动序列可能有很多行,但它的截止标志为数字0。输入截止标志为字符Z。

每个拼图的输出开头为标志号码(Puzzle #1, Puzzle #2等)。如果这个拼图没有最终状态,则输出"This puzzle has no final configuration.",否则输出拼图最终状态。拼图里的每2个字符之间应该有1个空格间隔开来。每2个拼图之间应该有1个空行间隔开来。

样例输入

TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS 
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS 
TUVWX
AAAAABBRRRLL0
Z

样例输出

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.

题解

题目说的挺绕口的,其实就是ABRL分别让空白格子向上下右左移动。

这道题最关键的就是在于输入数据的处理。

原题目里给的样例的行末尾的空格会被吞掉,这是真特么的坑。
还有每2个拼图之间要有空行,最后一个拼图之后要有一个回车。

Then show the code.

#include <stdio.h>
#include <string.h>

char puzzle[10][10];

int main(){
    char c;
    int kase = 0;
    
    //输入处理
    while((c = getchar()) && c != 'Z'){
        int x, y;
        memset(puzzle, 0, sizeof(puzzle));
        puzzle[0][0] = c;
        if(c == ' ') x = y = 0;
        for(int i=0; i<5; i++){
            for(int j=0; j<5; j++){
                if(i!=0 || j!=0){
                    puzzle[i][j] = c = getchar();
                    if(c == ' '){
                        x = i;
                        y = j;
                    }
                }
            }
            //吞掉行尾的回车
            getchar();
        }

        //将移动指令放到一个字符串中
        char cmds[100];
        memset(cmds, 0, sizeof(cmds));
        for(int i=0; c!='0'; ){
            c = getchar();
            if(c == ' ') continue;
            cmds[i++] = c;
        }
        getchar(); //吞掉回车
        
        //0 <= x <= 4
        //0 <= y <= 4
        int flag = 0, len = strlen(cmds);
        for(int i=0; i<len; i++){
            c = cmds[i];
            switch(c){
                case 'A':
                    puzzle[x][y] = puzzle[x-1][y];
                    puzzle[x-1][y] = ' ';
                    x--; break;
                case 'B':
                    puzzle[x][y] = puzzle[x+1][y];
                    puzzle[x+1][y] = ' ';
                    x++; break;
                case 'L':
                    puzzle[x][y] = puzzle[x][y-1];
                    puzzle[x][y-1] = ' ';
                    y--; break;
                case 'R':
                    puzzle[x][y] = puzzle[x][y+1];
                    puzzle[x][y+1] = ' ';
                    y++; break;
                case '0':
                    flag = 1; break;
                default:
                    break;
            } 
            if(flag) break;
            //如果空白格子出界了 那么就是"This puzzle has no final configuration"
            if(x<0 || x>4 || y<0 || y>4){
                flag = 2;
                break;
            }
        }

        if(kase) printf("\n");
        printf("Puzzle #%d:\n", ++kase);
        if(flag == 2){
            printf("This puzzle has no final configuration.\n");
            continue;
        }else{
            for(int i=0; i<5; i++){
                for(int j=0; j<5; j++){
                    printf("%c", puzzle[i][j]);
                    if(j < 4) printf(" ");
                }
                printf("\n");
            }
        }
    }
    return 0;
}
posted @ 2020-11-06 13:26  1v7w  阅读(193)  评论(0编辑  收藏  举报