UVA227

步骤:1.把输入的数字和空格保存。(这里用到gets函数读取整行)2.定位空格。3.输入指令。

#include<stdio.h>
#include<string.h>
char a[5][7];//声明数组
int main(){
    memset(a,0,sizeof(a));
    int blank_x=0;
    int blank_y=0;
    char s[100];
    char c[10];
    //输入网格
    gets(a[0]);
    gets(a[1]);
    gets(a[2]);
    gets(a[3]);
    gets(a[4]);
    //定位空格
    for(int x=0; x<5; x++)
    {
        for(int y=0; y<5; y++)
        {
            if(a[x][y] == ‘ ‘)
            {
                blank_x = x;
                blank_y = y;
            }
        }
    }
    gets(s);//输入指令
    for(int i=0;s[i] != ‘0‘;i++){
        if(s[i] == ‘A‘){
            if(!a[blank_x-1][blank_y])
                {
                 printf("1 This puzzle has no final configuration.\n");
                 return 0;}
            a[blank_x][blank_y]=a[blank_x-1][blank_y];
            a[blank_x-1][blank_y]=‘ ‘;
            blank_x=blank_x-1;
        }
        else if(s[i] == ‘B‘){
            if(!a[blank_x+1][blank_y])
                    {
                 printf("2 This puzzle has no final configuration.\n");
                 return 0;}
            a[blank_x][blank_y]=a[blank_x+1][blank_y];
            a[blank_x+1][blank_y]=‘ ‘;
            blank_x=blank_x+1;
        }
        else if(s[i] == ‘L‘){
            if(!a[blank_x][blank_y-1])
                    {
                 printf("3 This puzzle has no final configuration.\n");
                 return 0;}
            a[blank_x][blank_y]=a[blank_x][blank_y-1];
            a[blank_x][blank_y-1]=‘ ‘;
            blank_y=blank_y-1;
        }
        else if(s[i] == ‘R‘){
            if(!a[blank_x][blank_y+1])
                {
                 printf("4 This puzzle has no final configuration.\n");
                 return 0;}
            a[blank_x][blank_y]=a[blank_x][blank_y+1];
            a[blank_x][blank_y+1]=‘ ‘;
            blank_y=blank_y+1;
        }
    }
    //输出
    puts(a[0]);
    puts(a[1]);
    puts(a[2]);
    puts(a[3]);
    puts(a[4]);
    return 0;
}

OJ里还要加Puzzle #1:...

时间: 2024-10-13 07:28:11

UVA227的相关文章

UVA227 Puzzle

问题链接:UVA227 Puzzle.基础训练级的问题,用C语言编写程序. 问题简述:一个5×5的网格,一个格子是空的,其他格子各有一个字母,一共有四种指令:A,B,L,R,分别表示把空格上.下.左.右的相邻字母移到空格中.输入初始网格和指令序列,指令序列以数字0结束,输出指令执行完毕后的网格.如果有非法指令,应输出"This puzzle has no final configuration.". 这个题问题可能会出在输入处理上.根据实例,输入的指令序列可能是多行的,需要小心处理.

例题 3-5 谜题 uva227

A children’s puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, the frame

谜题 UVA227

这道题目还是不难的,但是要注意gcc里面gets已经不能用了,用gets_s还是可以的,尽管我并不知道有什么区别 #include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char a[20][20]; int x, y; char s[1000]; int blank_x = 0, blank_y = 0; int flag = 1; gets_s(a[0]); gets_s(a[1]); get

E - Puzzle( UVA-227)

 Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, t

uva227 谜题

 Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, t

Puzzle, ACM/ICPC World Finals 1993, UVa227

有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母.一共有4种指令:A, B, L, R,分别表示把空格上.下.左.右的相邻字母移到空格中.输入初始网格和指令序列(以数字0结束),输出指令执行完毕后的网格.如果有非法指令,应输出"Thispuzzle has no final configuration.",例如,图3-5中执行ARRBBL0后,效果如图3-6所示. 写的比较简陋,先写了容器,然后填充,写移动条件. 以下是用C写的源码: #include<stdi

算法竞赛入门经典第二版第三章习题

写这个的原因是看到一位大神的习题答案总结,于是自己心血来潮也想写一个这个,目的主要是督促自己刷题吧,毕竟自己太弱了. 习题3-1 得分 UVa 1585 大致就是设置一个变量记录到当前为止的连续的O的数量,碰到X就变0,水题. #include<stdio.h> #include<ctype.h> #include<string.h> char s[90]; int main(void) { int length,n,sum,num; scanf("%d&qu

UVA 227 Puzzle(基础字符串处理)

题目链接: https://cn.vjudge.net/problem/UVA-227 1 /* 2 问题 输入一个5*5的方格,其中有一些字母填充,还有一个空白位置,输入一连串 3 的指令,如果指令合法,能够得到一个移动后的方格就输出方格,不能就输出 4 "This puzzle has no final configuration." 5 6 解题思路 7 模拟,一个一个的读入字符,包括空格,再读入若干行指令,如果指令表面合法,进入模拟看内容是否合法,不合法直接输出提示. 8 9

2019年7月做题记录

POJ3299 POJ2159 POJ2739 POJ1083 POJ2262 POJ1503 POJ3006 POJ3094 POJ2255 POJ2965 PPOJ1328 POJ2109 POJ2586 UVA227 UVA1368 UVA1589 UVA201 POJ3295 HDU2089 HDU4734 POJ3252 HDU6581 HDU6582 POJ2104 HDU6601 HDU6578 HDU6586 HDU6590 HDU6609 HDU6600 洛谷P1522 洛谷P