几经修改,总算A掉了!
感觉主要就是字符串的输入格式 ,可以读取空格的gets(),另外就是 1 str[]的读入,及其在 2 的读取终止判断,总之这道题对字符串的学习很有 帮助!
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=163
#include<stdio.h>#include<string.h>
int main(){
int sx, sy, flag, coun = 0;
char maps[10][10], str[1001];
while( gets ( maps[0] ) ) {
if( maps[0][0] == 'Z' ) break;
for(int i = 1; i < 5; i++){
gets( maps[i] );
}
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
if(maps[i][j] == ' '){
sx = i;
sy = j;
break;
}
int k = 0;
while( ~scanf( "%c", &str[k] ) )
if( str[k] != '0' ) k++;
else break;
str[k] = 0; getchar();
flag = 1;
int x, y;
for(int i = 0; i < k; i++){
if(str[i] == 'A') {
x = sx-1; y = sy;
}
else if(str[i] == 'B'){
x = sx + 1; y = sy;
}
else if(str[i] == 'L'){
x = sx; y = sy - 1;
}
else if(str[i] == 'R'){
x = sx; y = sy + 1;
}
if(x < 0 || x > 4 || y < 0 || y > 4) {
flag = 0;
break;
}
maps[sx][sy] = maps[x][y];
maps[x][y] = ' ';
sx = x; sy = y;
}
if( coun++ ) printf("\n");
printf("Puzzle #%d:\n",coun);
if(flag) {
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
printf("%c",maps[i][j]);
if(j != 4) printf(" ");
}
printf("\n");
}
}
else printf("This puzzle has no final configuration.\n");
}
return 0;
}
SampleInput
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z
SampleOutput
Puzzle #1:
T R G S J
X O K L I
M D V B N
W P A E
U Q H C F
Puzzle #2:
A B C D
F G H I E
K L M N J
P Q R S O
T U V W X
Puzzle #3:
This puzzle has no final configuration.