uva227puzzle模拟

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93800#problem/E

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

思路:模拟。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

int cn=1;

int main()
{
    char x[5][5];
    string str;
    while(gets(x[0]))
    {
        if(x[0][0]=='Z')break;
        for(int i=1;i<5;i++)gets(x[i]);
        for(int i=0;;i++){cin>>str[i];int ch;ch=int(str[i]);if(ch==48){getchar();break;}}
        int a,b;
        for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
        if(x[i][j]==' '){a=i;b=j;}
        int flag=0;
        for(int k=0;str[k]!='0';k++)
        {
            if(str[k]=='A'){x[a][b]=x[a-1][b];x[a-1][b]=' ';if(!a){if(cn!=1)cout<<endl;cout<<"Puzzle #"<<cn<<":"<<endl;cn++;cout<<"This puzzle has no final configuration."<<endl;flag=1;break;}else a--;}
            if(str[k]=='B'){x[a][b]=x[a+1][b];x[a+1][b]=' ';if(a==4){if(cn!=1)cout<<endl;cout<<"Puzzle #"<<cn<<":"<<endl;cn++;cout<<"This puzzle has no final configuration."<<endl;flag=1;break;}else a++;}
            if(str[k]=='L'){x[a][b]=x[a][b-1];x[a][b-1]=' ';if(!b){if(cn!=1)cout<<endl;cout<<"Puzzle #"<<cn<<":"<<endl;cn++;cout<<"This puzzle has no final configuration."<<endl;flag=1;break;}else b--;}
            if(str[k]=='R'){x[a][b]=x[a][b+1];x[a][b+1]=' ';if(b==4){if(cn!=1)cout<<endl;cout<<"Puzzle #"<<cn<<":"<<endl;cn++;cout<<"This puzzle has no final configuration."<<endl;flag=1;break;}else b++;}
        }
        if(flag)continue;
        if(cn!=1)cout<<endl;
        cout<<"Puzzle #"<<cn<<":"<<endl;
        cn++;
        for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
        {
            cout<<x[i][j];
            if(j<4)cout<<" ";
            else cout<<endl;
        }
        memset(x,' ',sizeof(x));
        str=" ";
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/mu-ye/p/5467913.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值