Problem Description
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
| |
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:
| |
Problem link: Puzzle - UVA 227 - Virtual Judge