UVa227 - Puzzle

文章目录[x]
  1. 1:void swap(char a, char b){
    char t = a; a = b; b = t;
    }
    int main(){
    //freopen("date.in","r",stdin);
    //freopen("date.out","w",stdout);
    char s[6][6] = {0}, n;
    int x1, y1, x2, y2;
    int i, j, is = 0;
    for(i = 0; i<5; i++){
    for(j = 0; j<5; j++){
    s[i][j] = getchar();
    if(s[i][j] == ' ')x1 = x2 = i, y1 = y2 = j;
    }
    getchar();
    //printf("%s\n",s[i]);
    }
    while(scanf("%c",&n) && n != '0'){
    switch(n){
    case'A': x2 -= 1; break;
    case'B': x2 += 1; break;
    case'L': y2 -= 1; break;
    case'R': y2 += 1; break;
    default: is = 1;
    }
    swap(&s[x1][y1],&s[x2][y2]);
    x1 = x2; y1 = y2;
    }
    if(is) printf("This puzzle has no final configuration.\n");
    else{
    for(i = 0; i<5; i++)
    printf("%s\n",s[i]);
    }
    return 0;
    }
    /*
    Sample Input:
    TRGSJ
    XDOKI
    M VLN
    WPABE
    UQHCF
    ARRBBL0
    ABCDE
    FGHIJ
    KLMNO
    PQRS
    TUVWX
    AAA
    LLLL0
    ABCDE
    FGHIJ
    KLMNO
    PQRS
    TUVWX
    AAAAABBRRRLL0
    Z

            
//UVa227 - Puzzle

include

void swap(char a, char b){
char t = a; a = b; b = t;
}
int main(){
//freopen("date.in","r",stdin);
//freopen("date.out","w",stdout);
char s[6][6] = {0}, n;
int x1, y1, x2, y2;
int i, j, is = 0;
for(i = 0; i<5; i++){
for(j = 0; j<5; j++){
s[i][j] = getchar();
if(s[i][j] == ' ')x1 = x2 = i, y1 = y2 = j;
}
getchar();
//printf("%s\n",s[i]);
}
while(scanf("%c",&n) && n != '0'){
switch(n){
case'A': x2 -= 1; break;
case'B': x2 += 1; break;
case'L': y2 -= 1; break;
case'R': y2 += 1; break;
default: is = 1;
}
swap(&s[x1][y1],&s[x2][y2]);
x1 = x2; y1 = y2;
}
if(is) printf("This puzzle has no final configuration.\n");
else{
for(i = 0; i<5; i++)
printf("%s\n",s[i]);
}
return 0;
}
/*
Sample Input:
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z

Sample Output:
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.
*/

点赞

发表评论

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像