您的位置:首页 > 其它

UVa 227:Puzzle

2017-02-01 14:10 309 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: