-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUVA-227.cpp
75 lines (72 loc) · 1.23 KB
/
UVA-227.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
char s[6][6];
char arr[10];
map<char, int> mp;
int addx[4] = {-1,1,0,0};
int addy[4] = {0,0,-1,1};
int ix, iy;
bool moving(int a) {
int tx=ix+addx[a];
int ty=iy+addy[a];
if(tx < 1 || tx > 5 || ty < 1 || ty > 5)
return false;
swap(s[ix][iy], s[tx][ty]);
ix = tx;
iy = ty;
return true;
}
int main() {
int t,i,j,k,a;
bool flag;
mp['A'] = 0;
mp['B'] = 1;
mp['L'] = 2;
mp['R'] = 3;
t=0;
while(1) {
for(i=1; i<=5; ++i) {
for(j=1; j<=5; ++j) {
scanf("%c", &s[i][j]);
if(s[i][j] == 'Z')
return 0;
if(s[i][j] == ' ') {
ix = i;
iy = j;
}
}
fgets(arr, 10, stdin);
}
if(t != 0)
puts("");
flag = true;
while(scanf("%s", arr) == 1) {
for(i=0; arr[i] != 0; ++i) {
if(arr[i] == '0')
continue;
if(flag == true && moving(mp[arr[i]]) == false)
flag = false;
}
if(arr[i-1] == '0')
break;
}
fgets(arr, 10, stdin);
printf("Puzzle #%d:\n", ++t);
if(flag == false) {
puts("This puzzle has no final configuration.");
continue;
}
for(i=1; i<=5; ++i) {
for(j=1; j<=5; ++j) {
printf("%c", s[i][j]);
if(j == 5)
puts("");
else
putchar(' ');
}
}
}
return 0;
}