其他分享
首页 > 其他分享> > 谜题(Puzzle,ACM/ICPC World Finals 1993,UVa227)

谜题(Puzzle,ACM/ICPC World Finals 1993,UVa227)

作者:互联网

题目

在这里插入图片描述

思路

这一道题乍一看好像有一定难度,其实仔细分析下来只是一道很简单的数组之间元素交换的题目,就是过程复杂了一点。

代码

#include <iostream>

using namespace std;

int main()
{
    char a[5][5]={'T','R','G','S','J','X','D','O','K','I','M','#','V','L','N','W','P','A','B','E','U','Q','H','C','F'};
    char b[1000];
    int n,x,y,c;
    char p,temp;
    n=c=0;
    for(int i=0;i<1000;i++)
    {
        n++;
        cin>>b[i];
        if(b[i]=='0')
        {
            n--;
            break;
        }
    }
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
        {
            if(a[i][j]=='#')
            {
                x=i;
                y=j;
            }
        }
    for(int i=0;i<n;i++)
    {
        p=b[i];
        if(p=='A')
        {
            if(x-1>=0)
            {
                temp=a[x-1][y];
                a[x-1][y]=a[x][y];
                a[x][y]=temp;
                x--;
            }
            else cout<<"This puzzle has reached the top"<<endl;
        }
        else if(p=='R')
        {
            if(y+1<5)
            {
                temp=a[x][y+1];
                a[x][y+1]=a[x][y];
                a[x][y]=temp;
                y++;
            }
            else cout<<"This puzzle has reached the rightmost"<<endl;
        }
        else if(p=='B')
        {
            if(x+1<5)
            {
                temp=a[x+1][y];
                a[x+1][y]=a[x][y];
                a[x][y]=temp;
                x++;
            }
            else cout<<"This puzzle has reached the bottom"<<endl;
        }
        else if(p=='L')
        {
            if(y-1>=0)
            {
                temp=a[x][y-1];
                a[x][y-1]=a[x][y];
                a[x][y]=temp;
                y--;
            }
            else cout<<"This puzzle has reached the leftmost"<<endl;
        }
        else
        {
            cout<<"This puzzle has no final configuration";
            c=1;
            break;

        }
    }
    if(c==0)
    {
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<5;j++)
                cout<<a[i][j];
            cout<<endl;
        }
    }
    return 0;
}

标签:1993,UVa227,cout,temp,int,Puzzle,else,--,输入
来源: https://blog.csdn.net/Mark_mgx/article/details/120481756