习题3-5 谜题 UVa227

给定一个5*5的字母矩阵,按照给定的指令,移动字母,成功输出最后指令即可,否则输出This puzzle has no final configuration.

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
 
char puzzle[10][10];
int r0,c0;//记录空格的位置
const char* turns = "ABLR";
int dx[] = {-1,1,0,0};
int dy[] = {0,0,-1,1};
 
void read(int row, string str)
{
    for(int j=0;j<5;j++){
        puzzle[row][j] = str[j];
        if(str[j]==' ') { r0 = row; c0 = j;}
    }
}
 
bool move_p(char turn)
{
    int flag = 1;
    //int t = (strchr(turns,turn)-turns);
    int t;
    if(turn == 'A') t =0;
    else if(turn == 'B') t =1;
    else if(turn == 'L') t =2;
    else if(turn == 'R') t =3;
    int x,y;
    x = r0 + dx[t]; y = c0 + dy[t];
    if(x>=0&&x<=4&&y>=0&&y<=4){
        puzzle[r0][c0] = puzzle[x][y];
        puzzle[x][y] = ' ';
        r0 = x; c0 =y;
    }
    else flag = 0;
    if(flag) return true;
    else return false;
}
 
int main()
{
    //freopen("227.txt","r",stdin);
    string str,order;
    int icase = 0;
    while(1)
    {
        if(icase!=0) cin.get();
        getline(cin,str); //cin>>str;
        if(str[0]=='Z') break;
        if(icase!=0) cout<<endl;//不同样例之间换行
        read(0,str);
        for(int i=1;i<=4;i++)
        {
            getline(cin,str);
            read(i,str);
        }
        int ok,len;
        order.clear();
        //读入全部指令
        while(1)
        {
            string temp; cin>>temp;
            order += temp;
            len = temp.length();
            if(temp[len-1]=='0') break;
            else continue;
        }
 
        ok = 1;len = order.length();
        for(int i=0;i<len-1;i++)
        {
            if(move_p(order[i])) continue;
            else { ok = 0; break;}
        }
        cout<<"Puzzle #"<<++icase<<":"<<endl;
        if(ok)
        {
            int i,j;
            for(i=0;i<5;i++){
                for(j=0;j<4;j++) cout<<puzzle[i][j]<<" ";
                cout<<puzzle[i][j]<<endl;
            }
            //cout<<endl;
        }
        else cout<<"This puzzle has no final configuration."<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值