UVa 227 Puzzle

来源:互联网 发布:2016最近网络最火的dj 编辑:程序博客网 时间:2024/06/05 23:52

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;}
0 0