谜题(Puzzle)

这是一个关于解决5x5网格谜题的介绍,其中包含4种移动指令:A, B, L, R。文章提供了3个不同谜题的输入和输出示例,并强调了解题思路和最终配置的注意事项。" 126056231,11990213,Java编程语言详解,"['Java', '开发语言', 'Android开发', 'Web开发', '编程']

Description

有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母。一共有4种指令:A,B,L,R,分别表示把空格上下左右的相邻字母移动到空格中。

input

TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z

output

Puzzle #1:
T R G S J
X O K L I
M D V B N
W P   A E
U Q H C F

Puzzle #2:
A B C D
F G H I E
K L M N J
P Q R S O
T U V W X
Puzzle #3:

This puzzle has no final configuration.


思路分析:思路很清晰,按照题目的要求来就好了。注意输出格式,注意到原题目的一句话:Separate output from different puzzle records by one blank line.然后最后一组数据不能有空格。附上代码,已经经过测试。

#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <fstream>

using namespace std;

char s[5][10], order[100], order1[100];
int x, y;

int search()
{

    int i = 0;

    while(order[i] != '0')
    {
        if(order[i] == 'A')
        {
            if((x-1)>=0)
                {s[x][y] = s[x-1][y];
                x = x - 1;
                s[x][y] = 32;}
            else return 0;

        }
        else if(order[i] == 'B')
        {
            if((x+1)<=4)
            {
              // cout << s[x][y] << " " << s[x+1][y]<<endl;
                s[x][y] = s[x+1][y];
                x = x + 1;
                s[x][y] = 32;
            }
            else return 0;
        }
        else if(order[i] == 'L')
        {
            if((y-1)>=0)
            {
               // cout << s[x][y] << " " << s[x][y-1]<<endl;
                s[x][y] = s[x][y-1];
                y = y - 1;
                s[x][y] = 32;
            }
            else return 0;
        }
        else if(order[i] == 'R')
        {
            if((y+1)<=4)
            {
               // cout << s[x][y] << " " << s[x][y+1]<<endl;
                s[x][y] = s[x][y+1];
                y = y+1;
                s[x][y] = 32;
            }
            else return 0;
        }
        else return 0;
        i++;
       // cout << "x is " << x << "y is " << y << endl;
    }
    return 1;
}
int main()
{
    int i, j, l, num = 1;
   // ofstream savefile("D:\\test.txt");



    gets(s[0]);
    while(s[0][0] != 'Z' )
    {
        if(num!=1)
        {
            cout << endl;
           // savefile <<endl;
        }
        for(i = 1; i < 5; i++)
        {
            gets(s[i]);
        }

    gets(order);
    l = strlen(order);
   // cout << "the primer is " << order <<endl;
    while(order[l-1] != '0')
          {
              gets(order1);
              strcat(order, order1);
              l = strlen(order);
          //    cout << " the next is " << order << endl;
          }
  //  cout << "order is " << order << endl;


        for(i = 0; i < 5; i++)
        {
            for(j = 0; j < 5; j++)
            {
                    if(s[i][j] == 32)//空格是32
                    {
                        x = i;
                        y = j;
                       // cout << x << " " << y<<endl;
                    }
            }

        }

        if(search())
        {
           // savefile << "Puzzle #" << num <<":"<< endl;
            cout << "Puzzle #" << num++ <<":"<< endl;

            for(i = 0; i < 5; i++)
            {
                for(j = 0; j < 5; j++)
                {
                   if(j != 4) cout << s[i][j] << " ";
                   else cout << s[i][j];
                //   if(j != 4) savefile << s[i][j] << " ";
                 //  else savefile << s[i][j];
                }
                cout << endl;
               // savefile << endl;
            }
        }
        else
        {
          //  savefile << "Puzzle #" << num <<":"<< endl;
            cout << "Puzzle #" << num++ <<":"<< endl;

            cout << "This puzzle has no final configuration."<<endl;
          //  savefile << "This puzzle has no final configuration."<<endl;
        }




        gets(s[0]);


        memset(order, 32, sizeof(order));
    }
   // savefile.close();
}

/*#include <iostream>
#include <stdio.h>
using  namespace std;

int main()
{
    char s[100];
    int i = 0;
    scanf("%c", &s[0]);
    while(s[i] != '0')
    {
        scanf("%c",&s[++i]);
    }
    cout << s;
}*/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值