UVa 227 Puzzle

Problem Description

有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母。一共有4种指令:A,B,L,R,分别表示把空格上,下,左,右,的相邻字母移到空格种。输入初始网格和指令序列(以数字0结束),输入指令执行完毕后的网格。如该有非法指令,应输出“This puzzle has no final configuration.”。

Sample Input

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

Sample Output

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.

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char s[10][10];
    char c;
    int i, j, cas = 1;
    memset(s, 0, sizeof(s));//初始化
    while(gets(s[0]))//输入网格第一行
    {
        if(s[0][0] == 'Z') break;//退出循环
        for(i = 1; i < 5; i++)//接下来四行
        {
            gets(s[i]);
        }
        int x, y, flag = 0, ll = 0;
        for(i = 0; i < 5; i++)
        {
            for(j = 0; j < 5; j++)
            {
                if(s[i][j] == ' ' || s[i][j] == '\0')//可能空格在最后面
                {
                    s[i][j] = ' ';//如果是最后面让它为空格
                    ll = 1;
                    x = i; y = j;//标记下标
                    while(1)
                    {
                        scanf("%c", &c);
                        if(c == 'A')
                        {
                            if(x - 1 >= 0 && x - 1 < 5)//满足指令
                            {
                                s[x][y] = s[x - 1][y];
                                x = x - 1;//更新空格的下标
                                s[x][y] = ' ';
                            }
                            else
                            {
                                flag = 1;
                                break;
                            }
                        }
                        else if(c == 'B')
                        {
                            if(x + 1 >= 0 && x + 1 < 5)//满足指令
                            {
                                s[x][y] = s[x + 1][y];
                                x = x + 1;//更新空格的下标
                                s[x][y] = ' ';
                            }
                            else
                            {
                                flag = 1;
                                break;
                            }
                        }
                        else if(c == 'L')
                        {
                            if(y - 1 >= 0 && y - 1 < 5)//满足指令
                            {
                                s[x][y] = s[x][y - 1];
                                y = y - 1;//更新空格的下标
                                s[x][y] = ' ';
                            }
                            else
                            {
                                flag = 1;
                                break;
                            }
                        }
                        else if(c == 'R')
                        {
                            if(y + 1 >= 0 && y + 1 < 5)//满足指令
                            {
                                s[x][y] = s[x][y + 1];
                                y = y + 1;//更新空格的下标
                                s[x][y] = ' ';
                            }
                            else
                            {
                                flag = 1;
                                break;
                            }
                        }
                        else if(c == '0') break;
                    }
                    while(c != '0')
                    {
                        scanf("%c", &c);
                    }
                }
                if(ll) break;//表示已经找到空格
            }
            if(ll) break;
        }
        getchar();
        if(cas != 1) printf("\n");
        printf("Puzzle #%d:\n", cas++);
        if(flag) printf("This puzzle has no final configuration.\n");//非法指令
        else{//不非法输出结果
            for(i = 0; i < 5; i++)
            {
                for(j = 0; j < 5; j++)
                {
                    if(j) printf(" ");
                    printf("%c", s[i][j]);
                }
                printf("\n");
            }
        }
    memset(s, 0, sizeof(s));//初始化
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值