UVA227

一道我目前为止写过的最长代码的题,小小纪念一下,思路很简单,就是模拟,但是他的输出格式,我不是很明白,是复制网友代码然后发现的规律。代码很简单,应该可以看懂,orz

#include<iostream>
#include<stdio.h>
using namespace std;
char s[6][6];
struct blank
{
    int row;//行
    int col;//列
}blank;
int input()
{
    int flag2 = 0;
    for (int i = 1; i <= 5; i++) {
        for (int j = 1; j <= 5; j++)
        {
            s[i][j] = getchar();
            if (s[i][j] == 'Z') {
                flag2 = 1; break;
            }
            if (s[i][j] == ' ')             //指向空白格
            {
                blank.col = j;
                blank.row = i;
            }
            if (j == 5)getchar();
        }
        if (flag2 == 1)break;
    }

    return flag2;
}
int move()
{
    char move;
    int flag = 0;
    while(cin>>move) 
    {    
        if (move == '0')break;
        switch (move) {
        case('A'): if (blank.row != 1)
        {
            s[blank.row][blank.col] = s[blank.row-1][blank.col];
            s[--blank.row][blank.col] = ' ';
        }
                   else flag = 1;
                   break;//上
        case('B'): if (blank.row != 5)
        {
            s[blank.row][blank.col] = s[blank.row+1][blank.col];
            s[++blank.row][blank.col] = ' ';
        }
                   else flag = 1;
                   break;//下
        case('R'): if (blank.col != 5)
        {
            s[blank.row][blank.col] = s[blank.row][blank.col+1];
            s[blank.row][++blank.col] = ' ';
        }
                   else  flag = 1; 
                   break;//右
        case('L'): if (blank.col != 1)
        {
            s[blank.row][blank.col] = s[blank.row][blank.col-1];
            s[blank.row][--blank.col] = ' ';
        }
                   else  flag = 1;
                   break;//左
        }
    }
    return flag;
}
void output(int count)
{
    if (count > 1)cout << endl;
    printf("Puzzle #%d:\n", count);
    for (int i = 1; i <= 5; i++)
    {
        for (int j = 1; j <= 5; j++)
        {
            if (j == 5)cout << s[i][j] << endl;
            else cout << s[i][j] << " ";
        }   
    }
}
int main()
{
    int count = 1;
    for (;;) {
        int t1,t2;

        t1 = input();
        if (t1)break;

        t2=move();
        if (t2==0)output(count++);
        else {
            if (count > 1)cout << endl;
            printf("Puzzle #%d:\n", count++);
            cout << "This puzzle has no final configuration." << endl; 
        }
        getchar();
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值