1856: Sokoban
Submit Page Summary Time Limit: 1 Sec Memory Limit: 256 Mb Submitted: 97 Solved: 12
Description
Soko-ban is a Japanese word for a warehouse worker, and the name of a classic computer game created in the 1980s. It is a one-player game with the following premise. A single worker is in an enclosed warehouse with one or more boxes. The goal is to move those boxes to a set of target locations, with the number of target locations equalling the number of boxes. The player indicates a direction of motion for the worker using the arrow keys (up, down, left, right), according to the following rules.
- If the indicated direction of motion for the worker leads to an empty location (i.e., one that does not have a box or wall), the worker advances by one step in that direction.
- If the indicated direction of motion would cause the worker to move into a box, and the location on the other side of the box is empty, then both the worker and the box move one spot in that direction (i.e., the worker pushes the box).
- If the indicated direction of motion for a move would cause the worker to move into a wall, or to move into a box that has another box or a wall on its opposite side, then no motion takes place for that keystroke.
The goal is to simultaneously have all boxes on the target locations. In that case, the player is successful (and as a formality, all further keystrokes will be ignored). The game has been studied by computer scientists (in fact, one graduate student wrote his entire Ph.D. dissertation about the analysis of sokoban). Unfortunately, it turns out that finding a solution is very difficult in general, as it is both NP-hard and PSPACE-complete. Therefore, your goal will be a simpler task: simulating the progress of a game based upon a player's sequence of keystrokes. For the sake of input and output, we describe the state of a game using the following symbols:
Symbol | Meaning |
---|---|
. | empty space |
# | wall |
+ | empty target location |
b | box |
B | box on a target location |
w | worker |
W | worker on a target location |
For example, the initial configuration diagrammed at the beginning of this problem appears as the first input case below.
Input
Each game begins with a line containing integers R and C, where 4 ≤ R ≤ 15 represents the number of rows, and 4 ≤ C ≤ 15 represents the number of columns. Next will be R lines representing the R rows from top to bottom, with each line having precisely C characters, from left-to-right. Finally, there is a line containing at most 50 characters describing the player's sequence of keystrokes, using the symbols U, D, L, and R respectively for up, down, left, and right. You must read that entire sequence from the input, even though a particular game might end successfully prior to the end of the sequence. The data set ends with the line 0 0.
We will guarantee that each game has precisely one worker, an equal number of boxes and locations, at least one initially misplaced box, and an outermost boundary consisting entirely of walls.
Output
For each game, you should first output a line identifying the game number, beginning at 1, and either the word complete or incomplete, designating whether or not the player successfully completed that game. Following that should be a representation of the final board configuration.
Sample Input
8 9 ######### #...#...# #..bb.b.# #...#w#.# #...#b#.# #...++++# #...#..## ######### ULRURDDDUULLDDD 6 7 ####### #..#### #.+.+.# #.bb#w# ##....# ####### DLLUDLULUURDRDDLUDRR 0 0
Sample Output
Game 1: incomplete ######### #...#...# #..bb...# #...#.#.# #...#.#.# #...+W+B# #...#b.## ######### Game 2: complete ####### #..#### #.B.B.# #.w.#.# ##....# #######
Hint
Source
2011 Mid-Central USA Regional Contest
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1856
今天开始慢慢把多校的题尽量补一下
原理很简单 但是操作有点多 细心点写就好
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w",stdout);
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL;
typedef pair<int, int> PII;
char mp[20][20];
char mp2[20][20];
char mpcin[20][20];
struct node {
int x, y;
}tag[105];
bool judge(int x, int y, char c) {
if(mp[x][y] == '#') return 0;
if(c == 'U') {
if(mp2[x][y] == 'b' && (mp2[x - 1][y] == 'b'|| mp[x - 1][y] == '#')) return 0;
}
if(c == 'D') {
if(mp2[x][y] == 'b' && (mp2[x + 1][y] == 'b'|| mp[x + 1][y] == '#')) return 0;
}
if(c == 'L') {
if(mp2[x][y] == 'b' && (mp2[x][y - 1] == 'b'|| mp[x][y - 1] == '#')) return 0;
}
if(c == 'R') {
if(mp2[x][y] == 'b' && (mp2[x][y + 1] == 'b'|| mp[x][y + 1] == '#')) return 0;
}
return 1;
}
int main()
{
int cnt = 0;
int n, m;
while(~scanf("%d%d", &n, &m)) {
if(n == 0 && m == 0) break;
int tt = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
mp[i][j] = '.';
mp2[i][j] = ' ';
}
}
for(int i = 0; i < n; i++) {
scanf("%s", mpcin[i]);
}
int xx, yy;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(mpcin[i][j] == '#') {
mp[i][j] = '#';
}else if(mpcin[i][j] == '+') {
mp[i][j] = '+';
tag[tt].x = i;
tag[tt].y = j;
tt++;
}else if(mpcin[i][j] == 'b') {
mp2[i][j] = 'b';
}else if(mpcin[i][j] == 'w') {
mp2[i][j] = 'w';
xx = i;
yy = j;
}else if(mpcin[i][j] == 'W') {
mp[i][j] = '+';
mp2[i][j] = 'w';
xx = i;
yy = j;
tag[tt].x = i;
tag[tt].y = j;
tt++;
}else if(mpcin[i][j] == 'B') {
mp[i][j] = '+';
mp2[i][j] = 'b';
tag[tt].x = i;
tag[tt].y = j;
tt++;
}
}
}
char s[105];
scanf("%s", s);
int len = strlen(s);
int flag = 0;
for(int i = 0; i < len; i++) {
int cou = 0;
for(int i = 0; i < tt; i++) {
if(mp2[tag[i].x][tag[i].y] == 'b') cou++;
}
if(cou == tt) {
flag = 1;
break;
}
char c = s[i];
if(c == 'U') {
if(judge(xx - 1, yy, c)) {
if(mp2[xx - 1][yy] == 'b') {
mp2[xx - 1][yy] = 'w';
mp2[xx - 2][yy] = 'b';
mp2[xx][yy] = ' ';
xx--;
} else{
mp2[xx - 1][yy] = 'w';
mp2[xx][yy] = ' ';
xx--;
}
}
}
if(c == 'D') {
if(judge(xx + 1, yy, c)) {
if(mp2[xx + 1][yy] == 'b') {
mp2[xx + 1][yy] = 'w';
mp2[xx + 2][yy] = 'b';
mp2[xx][yy] = ' ';
xx++;
} else{
mp2[xx + 1][yy] = 'w';
mp2[xx][yy] = ' ';
xx++;
}
}
}
if(c == 'L') {
if(judge(xx, yy - 1, c)) {
if(mp2[xx][yy - 1] == 'b') {
mp2[xx][yy - 1] = 'w';
mp2[xx][yy - 2] = 'b';
mp2[xx][yy] = ' ';
yy--;
} else{
mp2[xx][yy - 1] = 'w';
mp2[xx][yy] = ' ';
yy--;
}
}
}
if(c == 'R') {
if(judge(xx, yy + 1, c)) {
if(mp2[xx][yy + 1] == 'b') {
mp2[xx][yy + 1] = 'w';
mp2[xx][yy + 2] = 'b';
mp2[xx][yy] = ' ';
yy++;
} else{
mp2[xx][yy + 1] = 'w';
mp2[xx][yy] = ' ';
yy++;
}
}
}
}
int cou = 0;
for(int i = 0; i < tt; i++) {
if(mp2[tag[i].x][tag[i].y] == 'b') cou++;
}
if(cou == tt) {
flag = 1;
}
if(flag) printf("Game %d: complete\n", ++cnt);
else printf("Game %d: incomplete\n", ++cnt);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(mp[i][j] == '#') cout << mp[i][j];
else if(mp2[i][j] == 'b') {
if(mp[i][j] == '+') cout << "B";
else cout << "b";
}else if(mp2[i][j] == 'w') {
if(mp[i][j] == '+') cout << "W";
else cout << "w";
}else if(mp[i][j] == '+') cout << "+";
else cout << ".";
}
cout << endl;
}
}
return 0;
}