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;
}

 

posted @ 2016-05-07 10:46  哲贤  阅读(196)  评论(0编辑  收藏  举报