UVa - 227 Puzzle

版权声明:原文地址https://coderjeremy.github.io/ https://blog.csdn.net/Coder_Jeremy/article/details/52810738

UVa - 227 Puzzle


一道水题

第一次是wrong answer,没有什么好说
第二次之后全是PE,真是醉了,这道题有两个坑
第一个坑是样例给的数是没有空格

TRGSJ
XDOKI
M VLN <<——这里的是真空行
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS << 这里不是空格,只有四个字符,需要用strlen()函数进行判断,如果默认这里是空格就错了
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z
第二个坑就是样例输出,要求是每两个答案输出一个空行,也就是说最后一个样例不能输出空行,最不喜欢这样的题目,输出后,总感觉是错误的,可是结果就能 AC
↓↓↓看图:第一行样例输出和第二行输入在一起,怎么看怎么别扭,然而这就是正确的答案,蓝瘦香菇。。。
这里写图片描述

这里写图片描述

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

int main()
{
    // freopen("output.txt","w",stdout);
    int k=1;
    while(1)
    {
        char s[5][5];
        memset(s,0,sizeof(s));
        int I,J;
        for(int i=0; i<5; i++)
        {
            gets(s[i]);
            for(int j=0; j<5; j++)
            {
                if(s[i][j]=='Z')
                {
                    return 0;;
                }
                else if(s[i][j]==' ')
                {
                    I=i;
                    J=j;
//                    printf("IO=%d  JO=%d\n",I,J);
                }
                else if(strlen(s[i])==4)
                {
                    I=i;
                    J=j;
                }
//                printf("****i=%d,j=%d\n",i,j);
            }
        }

        char m[100];
        memset(m,0,sizeof(m));
        for(int i=0; i<100; i++)
        {
            m[i]=getchar();
            if(m[i]=='\n')
                i=i-1;
            if(m[i]=='0')
                break;
        }
        int wrong=0;
        char temp;
        for(int i=0; i<strlen(m)-1; i++)
        {
            if(m[i]=='A')
            {
                if(I-1<0)
                {
                    wrong=1;
                    break;
                }
                temp=s[I][J];
                s[I][J]=s[I-1][J];
                s[I-1][J]=temp;
                I=I-1;
//                printf("I=%d  J=%d\n",I,J);

            }

            else if(m[i]=='B')
            {

                if(I+1>4)
                {
                    wrong=1;
                    break;
                }
                temp=s[I][J];
                s[I][J]=s[I+1][J];
                s[I+1][J]=temp;
                I=I+1;
//                printf("I=%d  J=%d\n",I,J);

            }

            else if(m[i]=='R')
            {

                if(J+1>4)
                {
                    wrong=1;
                    break;
                }
                temp=s[I][J];
                s[I][J]=s[I][J+1];
                s[I][J+1]=temp;
                J=J+1;
//                printf("I=%d  J=%d\n",I,J);
            }

            else if(m[i]=='L')
            {
                if(J-1<0)
                {
                    wrong=1;
                    break;
                }
                temp=s[I][J];
                s[I][J]=s[I][J-1];
                s[I][J-1]=temp;
                J=J-1;
//                printf("I=%d  J=%d\n",I,J);
            }

            else
            {
                wrong=1;
                break;
            }
        }
        if(k!=1)
            printf("\n");//简单水题,但是就是一个格式控制的问题,要求两个答案间输出一个空行
        printf("Puzzle #%d:\n",k++);
        if(wrong)
            printf("This puzzle has no final configuration.\n");
        else
        {
            for(int i=0; i<5; i++)
            {
                for(int j=0; j<5; j++)
                {
                    putchar(s[i][j]);
                    if(j<4)
                        printf(" ");
                    else
                        printf("\n");
                }
            }
        }
        getchar();
    }
    return 0;
}

蓝瘦香菇…………………

好好做题。。

猜你喜欢

转载自blog.csdn.net/Coder_Jeremy/article/details/52810738