UVa 227:Puzzle

来源:互联网 发布:php两年工作经验简历 编辑:程序博客网 时间:2024/05/21 10:34
#include <stdio.h>void swap(char* a, char* b){    char t = *a;    *a = *b;    *b = t;}int main(){    char puzzle[5][6], ch;    char* e = "This puzzle has no final configuration.";    int ct = 0;    while(1)    {        gets(puzzle[0]);        if('Z' == puzzle[0][0]) break;        gets(puzzle[1]);        gets(puzzle[2]);        gets(puzzle[3]);        gets(puzzle[4]);        int x, y;        for(x = 0; x < 5; ++x)        {            int flag = 0;            for(y = 0; y < 5; ++y)                if(' ' == puzzle[x][y]) { flag = 1; break; }            if(flag) break;        }        if(ct) putchar('\n');        printf("Puzzle #%d:\n", ++ct);        int error = 0;        while((ch = getchar()) != '0')        {            if(error) continue;            if('A' == ch)            {                if(x - 1 >= 0) { swap(&puzzle[x][y], &puzzle[x-1][y]); --x; continue; }                else { printf("%s\n", e); error = 1; continue; }            }            if('B' == ch)            {                if(x + 1 < 5) { swap(&puzzle[x][y], &puzzle[x+1][y]); ++x; continue; }                else { printf("%s\n", e); error = 1; continue; }            }            if('L' == ch)            {                if(y - 1 >= 0) { swap(&puzzle[x][y], &puzzle[x][y-1]); --y; continue; }                else { printf("%s\n", e); error = 1; continue; }            }            if('R' == ch)            {                if(y + 1 < 5) { swap(&puzzle[x][y], &puzzle[x][y+1]); ++y; continue; }                else { printf("%s\n", e); error = 1; continue; }            }        }        if(!error)        {            for(x = 0; x < 5; ++x)            {                for(y = 0; y < 4; ++y)                    printf("%c ", puzzle[x][y]);                printf("%c\n", puzzle[x][4]);            }        }        while(getchar() != '\n');    }    return 0;}
0 0
原创粉丝点击