【UVa】[227]Puzzle

Problem Description

Loading...

Problem Analysis

My first impression was that the problem statement is so long…
They say it’s a problem from ACM/ICPC World Finals 1993…
Feels pretty badass~

So…
I’ll just post all the ideas I wrote for this problem a few days ago first…

Use a 2D string array to store the grid (each row n is one string)
Record the command sequence in a string and use a loop to process it
When A, B, L, or R is read, execute the corresponding command
For any other character, break; and output “This puzzle has no final configuration.”
For input, you can use a nested loop and use x, y to record where the blank is
(record the blank in the grid as 0)
(after the inner loop ends, use getchar() to absorb the newline)
A: up, B: down, L: left, R: right (A: move the letter above into the blank)
Example code:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// ARRBBL 其对应变化为
// A:
map(x,y)=map(x,y-1);
map(x,y-1)=0;
y=y-1;
// B:
map(x,y)=map(x,y+1);
map(x,y+1)=0;
y=y+1;
// L:
map(x,y)=map(x-1,y);
map(x-1,y)=0;
x=x-1;
// R:
map(x,y)=map(x+1,y);
map(x+1,y)=0;
x=x+1;

After the transformation, output the result (you should be able to use a single loop with printf)
printf("%s\n",map[y]);
/* needs testing */The most straightforward way is still to use nested loops with putchar()
(after the inner loop ends, output “\n”)

That’s what I was thinking back then
And today I’m going to write it for real~

First, I ran some tests related to 2D arrays
As it turned out

cpp
1
2
3
char map[5][5];
for(int i=0; i<5; i++)
    scanf("%s",map[i]);

This way of writing stores the characters in
map[0][1-5]
The same applies to printf
You can’t print it like that…
See: [Reading Notes - 2D Character Arrays](/content/post/note/competitive-programming/7/算法竞赛入门经典(7) – 二维字符数组)

So when reading the grid, you don’t necessarily have to use getchar()
But since this involves detecting spaces in the grid
it’s still better to use a nested loop with getchar()

After many attempts
Even after fixing all kinds of pitfalls, it still got WA…
I underestimated this problem
I’ll set a flag first

WA code:

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<string.h>
int main() {
    int x,y;
    int k,kase=0;
    char s[1000];
    char map[5][5];
    while(scanf("%c",&map[0][0]),map[0][0]!='Z') {
        for(int i=0; i<5; i++) {
            int flag=1;
            for(int j=0; j<5; j++) {
                if(i||j) {
                    map[i][j]=getchar();
                    if(map[i][j]==' ') {
                        y=i;
                        x=j;
                    } else if(map[i][j]=='\n') {
                        map[i][j]=' ';
                        y=i;
                        x=j;
                        flag=0;
                    }
                } else
                    continue;
            }
            if(flag)
                getchar();
        }
        int t=-1;
        memset(s,0,sizeof(s));
        while((s[++t]=getchar())!='0');
        if(kase)
            printf("\n");
        printf("Puzzle #%d:\n",++kase);
        for(k=0; k<t; k++) {
            if(s[k]=='A') {
                if(y-1<0) {
                    printf("This puzzle has no final configuration.\n");
                    break;
                } else {
                    map[y][x]=map[y-1][x];
                    map[y-1][x]=0;
                    y=y-1;
                }
            } else if(s[k]=='B') {
                if(y+1>=5) {
                    printf("This puzzle has no final configuration.\n");
                    break;
                } else {
                    map[y][x]=map[y+1][x];
                    map[y+1][x]=0;
                    y=y+1;
                }
            } else if(s[k]=='L') {
                if(x-1<0) {
                    printf("This puzzle has no final configuration.\n");
                    break;
                } else {
                    map[y][x]=map[y][x-1];
                    map[y][x-1]=0;
                    x=x-1;
                }
            } else if(s[k]=='R') {
                if(x+1>=5) {
                    printf("This puzzle has no final configuration.\n");
                    break;
                } else {
                    map[y][x]=map[y][x+1];
                    map[y][x+1]=0;
                    x=x+1;
                }
            } else if(s[k]=='\n') {
                continue;
            } else {
                printf("This puzzle has no final configuration.\n");
                break;
            }
        }
        if(k==t) {
            for(int i=0; i<5; i++) {
                for(int j=0; j<5; j++) {
                    putchar(map[i][j]);
                    if(j!=4)
                        putchar(' ');
                }
                printf("\n");
            }
        }
        memset(map,0,sizeof(map));
        char ch;
        while((ch = getchar()) != '\n' && ch != EOF);
    }
    return 0;
}

Problem link: Puzzle - UVA 227 - Virtual Judge

Loading...