Puzzle UVA - 227

Puzzle UVA - 227

题目传送:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=163

题目大意:5x5的方格,内有一空格,通过上下左右移动使空格位置不断变换。

分析:强行做就好,题目很水,输入的时候需要仔细考虑下,而且输出格式坑了些,它样例给的输出格式就不标准。。。。。

AC Code:

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

char maps[5][7];
char cmd[1001];

int main()
{
    int cases = 0;
    while (gets(maps[0])) {
        if (maps[0][0] == 'Z') break;
        for (int i = 1 ; i < 5 ; ++ i)
            gets(maps[i]);
        int b_x = 0,b_y = 0;
        for (int i = 0 ; i < 5 ; ++ i)
            for (int j = 0 ; j < 5 ; ++ j)
                if (maps[i][j] == ' ') {
                    b_x = i;b_y = j;
                    break;
                }
        int count = 0;
        while (~scanf("%c",&cmd[count]))
            if (cmd[count] != '0') count ++;
            else break;
        cmd[count] = 0;getchar();

        int flag = 0,x = b_x,y = b_y;
        for (int i = 0 ; cmd[i] ; ++ i) {
            switch(cmd[i]) {
                case 'A':   x = b_x-1;y = b_y; break;
                case 'B':   x = b_x+1;y = b_y; break;
                case 'L':   x = b_x;y = b_y-1; break;
                case 'R':   x = b_x;y = b_y+1; break;
            }

            if (x < 0 || x > 4 || y < 0 || y > 4) {
                flag = 1;break;
            }else {
                maps[b_x][b_y] = maps[x][y];
                maps[x][y] = ' ';
                b_x = x; b_y = y;
            }
        }

        if (cases ++) printf("\n");//注意这里的换行
        printf("Puzzle #%d:\n",cases);

        if (flag)
            printf("This puzzle has no final configuration.\n");
        else {
            for (int i = 0 ; i < 5 ; ++ i) {
                printf("%c",maps[i][0]);
                for (int j = 1 ; j < 5 ; ++ j)
                    printf(" %c",maps[i][j]);
                printf("\n");
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值