UVA-227 Puzzle

题目在这
这个题把我折腾了快一下午,要么就是输入有问题,要么就是输出有问题,前前后后wa了好多次,果然还是自己太菜了,基本的输入输出都掌握不了,我觉得此题已经不基础了,要吐,(╥╯^╰╥)
注意以下几点:
1.要保证能够读入空格,所以不能使用scanf函数,对于gets函数,是不提倡的,因为它早就被C11删除了。
2.每一组的数据输入完毕后要进行getchar,吞掉回车。
3.在输入上下左右的字母时,必须全部读入,否则剩下的会在下面的组中读取。
4.建议用坐标来进行记录空格所在的地方,(我本来是每一次都遍历一遍寻找空格的位置,这样循环终止的条件比较复杂)。

总结——字符串的输入输出

输入输出
scanf:%s遇到空白符会停止;%c%c类似于getchar(),不会跳过任何一个字符,包括空格,tab,‘\n’等。但是,要注意:空格的作用。printf,%c,%s
gets(不建议)puts,自动添加一个回车,至\0结束
cincout
fgetc,其中fgetc(stdin)等价于getchar
getchar:a = getchar(),不会忽略任何一个字符,包括换行 ,tab,enter在内putchar(a),不会忽略任何一个字符,包括换行 ,tab,enter在内
fgets(a,n,stdin),当读取 (n-1) 个字符时,或者读取到换行符时,或者到达文件末尾时结束
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <vector>
#include <queue>
#include <ctime>
#define ll long long
#define inf 0x3f3f3f3f
#define local
using namespace std;
const int N = 6;
char a[10][10]= {'\0'};
char *h= {"This puzzle has no final configuration.\n"};

int main()
{
#ifdef local
    freopen("input.txt","r",stdin);
#endif // local
    int num=1;
    while(fgets(a[0],7,stdin))
    {
        int x=0,y=0;
        if(a[0][0]=='Z')
            break;
        for(int i=1; i<5; i++)
        {
            fgets(a[i],7,stdin);
        }
        for(int i=0; i<5; i++)
        {
            for(int j=0; j<5; j++)
            {
                if(a[i][j]==' ')
                {
                    x=i,y=j;
                    break;
                }
            }
        }
        char t;
        int flag=1,x1,y1;
        char mo[105]={'\0'};
        int g=0;
        while(cin>>t&&t!='0')
            mo[g++]=t;
        for(int i=0; i<g; i++)
        {
            switch(mo[i])
            {
            case 'A':
            {
                x1=x-1;
                y1=y;
                break;
            }
            case 'B':
            {
                x1=x+1;
                y1=y;
                break;
            }
            case 'L':
            {
                y1=y-1;
                x1=x;
                break;
            }
            case 'R':
            {
                y1=y+1;
                x1=x;
                break;
            }
            default:
                break;
            }
            if(x1<0||y1<0||x1>4||y1>4)
            {
                flag=0;
                break;
            }
            else
            {
                swap(a[x][y],a[x1][y1]);
                x=x1;
                y=y1;
            }
        }
        getchar();//吞掉回车
        if(num>1)
            printf("\n");
        printf("Puzzle #%d:\n",num);
        num++;
        if(flag==0)
            printf("%s",h);
        else
        {
            for(int i=0; i<5; i++)
            {
                for(int j=0; j<4; j++)
                {
                    printf("%c ",a[i][j]);
                }
                printf("%c\n",a[i][4]);
            }
        }

    }
    return 0;
}

参考:
https://blog.csdn.net/denao/article/details/74531841
https://blog.csdn.net/lecholin/article/details/70147512

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值