Uva227
大概意思就是空格的移动,如果不合法就输出This puzzle has no final configuration.否则
输出最终的移动的答案,注意输出格式,最后多一个换行也是错的。
大概意思就是空格的移动,如果不合法就输出This puzzle has no final configuration.否则
输出最终的移动的答案,注意输出格式,最后多一个换行也是错的。
#include <stdio.h>
#include <string.h>
const int maxn = 7, N = 105;
char str[maxn][maxn], ch[N];
int dx[5] = { 0, -1, 1, 0, 0 }, dy[5] = { 0, 0, 0, -1, 1 };
int check ( int x, int y ) //检查是否出界
{
return x < 0 || x >= 5 || y < 0 || y >= 5;
}
template < class T >
void swap ( T &a, T &b ) //模板交换
{
T t = a;
a = b;
b = t;
}
void print ( int n ) //打印
{
for ( int i = 0; i < n; i ++ )
{
for ( int j = 0; j < n; j ++ )
printf ( j == 0 ? "%c" : " %c", str[i][j] );
printf ( "\n" );
}
}
int main ( )
{
int x, y, flag, cas = 0;
//freopen ( "in0.in", "r", stdin );
while ( 1 )
{
flag = 0;
fgets ( str[0], maxn, stdin );
if ( str[0][0] == 'Z' ) //当字符为Z时退出
break ;
for ( int i = 1; i < 5; i ++ )
fgets ( str[i], maxn, stdin );
for ( int i = 0; i < 5; i ++ )
for ( int j = 0; j < 5; j ++ )
{
if ( str[i][j] == ' ' )
{
x = i;
y = j;
}
}
int n = 0;
while ( ~ scanf ( "%c", &ch[n] ) )
{
if ( ch[n] == '0' )
break ;
n ++;
}
ch[n] = '\0'; //赋结束标志
getchar ( ); //吃掉\n
/*printf ( "%d\n", n );
for ( int i = 0; i <= n; i ++ )
printf ( "1.%s\n", ch[i] );*/
for ( int i = 0; i <= n; i ++ )
{
//printf ( "5.%c\n", ch[i][j] );
int pos = 0;
switch ( ch[i] )
{
case 'A' :
pos = 1;
break ;
case 'B' :
pos = 2;
break ;
case 'L' :
pos = 3;
break ;
case 'R' :
pos = 4;
break ;
}
int nx = x+dx[pos];
int ny = y+dy[pos];
//printf ( "%d %d\n", nx, ny );
if ( check ( nx, ny ) )
{
flag = 1;
break ;
}
swap ( str[x][y], str[nx][ny] );
x = nx;
y = ny;
}
if ( cas ++ )
//*注意空格不能放在下面打印,那样会多一个换行,会出错
printf ( "\n" );
printf ( "Puzzle #%d:\n", cas );
if ( flag )
printf ( "This puzzle has no final configuration.\n" );
else
print ( 5 );
}
return 0;
}