侧边栏
首页代码

习题3-5 谜题(Puzzle, ACM/ICPC World Finals 1993, UVa227)

#include<stdio.h>
#include<string.h>
char s[5][5];
int main()
{
    while(gets(s[0]))
    {
        int t = 0;
        if(s[0][0] == 'Z') break;
        int i ,j ,m ,n;
        for(i = 0;i < 5;i++)
        {
            if(i) gets(s[i]);
            for(j = 0;j < 5;j++)
            {
                if(s[i][j] == ' ')
                {
                    m = i;
                    n = j;
                    break;
                }
            }
        }
        bool ok = true;
        int x = m,y = n;
        for(;;)
        {
            char c = getchar();
            if(c == ' ') ok = false;
            if(c == '0') break;
            if(c == 'A') x -= 1;
            else if(c == 'B') x += 1;
            else if(c == 'L') y -= 1;
            else if(c == 'R') y += 1;
            if(x < 0||x>4||t<0||y>4)
            {
                ok = false;
                if(c != '\n') c = getchar();
            }
            s[m][n] = s[x][y];
            s[x][y] = ' ';
            m = x;
            n = y;
        }
        printf("Puzzled #%d\n",++t);
        if(ok)
        {
            for(i = 0;i<5;i++)
            {
                for(j = 0;j<5;j++)
                {
                    if(j) printf(" ");
                    printf("%c",s[i][j]);
                }
                printf("\n");
            }
        }
        else printf("This puzzle has no final configuration.");
    }
    return 0;
}

 

posted @ 2020-09-25 09:40  AlexStraightUp  阅读(137)  评论(0)    收藏  举报
页脚HTML代码