刷题11:Puzzle

题目链接:https://vjudge.net/problem/UVA-227
题目解析:此题需要考虑以下几方面
1.每次循环后需要加入getchar();
2.操作步骤那个地方要等它全部处理完再break
代码:


```cpp
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
char ch[7][7];//分布
int fl[7][7];//标志变量,表格
char c;//中间变量
int k;//中间变量
int flag;//如果非法移动,标志变量
int ex,ey;
int cou=0;//判断是否需要空格
void shuru()
{
    c=(char)k;
    ch[1][1]=c;
    for(int i=2; i<=5; i++)
        scanf("%c",&ch[1][i]);
    getchar();
    for(int j=2; j<=4; j++)
    {
        for(int i=1; i<=5; i++)
            scanf("%c",&ch[j][i]);
        getchar();
    }
    for(int i=1; i<=5; i++)
        scanf("%c",&ch[5][i]);
}
void A()
{
    if(fl[ex-1][ey]==0)
    {
        flag=0;
        return;
    }
    char ck;
    ck=ch[ex-1][ey];
    ch[ex-1][ey]=ch[ex][ey];
    ch[ex][ey]=ck;
    ex--;
}
void B()
{
    if(fl[ex+1][ey]==0)
    {
        flag=0;
        return;
    }
    char ck;
    ck=ch[ex+1][ey];
    ch[ex+1][ey]=ch[ex][ey];
    ch[ex][ey]=ck;
    ex++;
}
void R()
{
    if(fl[ex][ey+1]==0)
    {
        flag=0;
        return;
    }
    char ck;
    ck=ch[ex][ey+1];
    ch[ex][ey+1]=ch[ex][ey];
    ch[ex][ey]=ck;
    ey++;
}
void L()
{
    if(fl[ex][ey-1]==0)
    {
        flag=0;
        return;
    }
    char ck;
    ck=ch[ex][ey-1];
    ch[ex][ey-1]=ch[ex][ey];
    ch[ex][ey]=ck;
    ey--;
}
int main()
{
    memset(fl,0,sizeof(fl));
    for(int i=1; i<=5; i++)
        for(int j=1; j<=5; j++)
            fl[i][j]=1;
    while((k=getchar())!=EOF)
    {
        cou++;
        char w=(char)k;
        if(w=='Z')
            break;
        if(cou!=1)
            cout<<endl;
        cout<<"Puzzle #"<<cou<<":"<<endl;
        shuru();
        for(int i=1; i<=5; i++)
        {
            for(int j=1; j<=5; j++)
                if(ch[i][j]==' ')
                {
                    ex=i;
                    ey=j;
                    break;
                }
        }
        flag=1;
        for(;;)
        {
            char cc;
            scanf("%c",&cc);
            if(cc=='0')
                    break;
            if(flag)
            {
                if(cc=='A')
                    A();
                else if(cc=='R')
                    R();
                else if(cc=='B')
                    B();
                else if(cc=='L')
                    L();
            }
        }
        if(!flag)
            cout<<"This puzzle has no final configuration."<<endl;
        else
        {
            for(int i=1; i<=5; i++)
            {
                for(int j=1; j<=5; j++)
                {
                    if(j!=1)
                        cout<<" ";
                    cout<<ch[i][j];
                }
                cout<<endl;
            }
        }
        getchar();
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值