HNUNUOJ11241 Sokoban[模拟推箱子]

Sokoban
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 17, Accepted users: 14
Problem 11241 : No special judgement
Problem 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 belo

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.#.#
##....#
#######
Problem Source
  mcpc2011 

Submit   Discuss   Judge Status  Problems  Ranklist 

模拟推箱子,按照一定的步骤去走

code:

  1 #include<iostream>
  2 using namespace std;
  3 
  4 int r,c;
  5 bool flag;
  6 int sx,sy;
  7 char map[20][20];
  8 char mov[60];
  9 int cnt;
 10 int dir[4][2]={
 11     -1,0,
 12     1,0,
 13     0,-1,
 14     0,1
 15 };
 16 
 17 void solve()
 18 {
 19     int i,dr;
 20     int len=strlen(mov);
 21     for(i=0;i<len;i++)
 22     {
 23         if(mov[i]=='U')        dr=0;
 24         else if(mov[i]=='D')    dr=1;
 25         else if(mov[i]=='L')    dr=2;
 26         else    dr=3;
 27         int tx=sx+dir[dr][0];
 28         int ty=sy+dir[dr][1];
 29         if(map[tx][ty]!='#')
 30         {
 31             int nextx=tx+dir[dr][0];
 32             int nexty=ty+dir[dr][1];
 33             if(map[tx][ty]=='.')
 34             {
 35                 map[tx][ty]='w';
 36                 if(map[sx][sy]=='W')
 37                     map[sx][sy]='+';
 38                 else
 39                     map[sx][sy]='.';
 40                 sx+=dir[dr][0];
 41                 sy+=dir[dr][1];
 42             }
 43             else if(map[tx][ty]=='+')
 44             {
 45                 map[tx][ty]='W';
 46                 if(map[sx][sy]=='W')
 47                     map[sx][sy]='+';
 48                 else
 49                     map[sx][sy]='.';
 50                 sx+=dir[dr][0];
 51                 sy+=dir[dr][1];
 52             }
 53             else if(map[tx][ty]=='b')
 54             {
 55                 if(map[nextx][nexty]=='.')
 56                 {
 57                     map[nextx][nexty]='b';
 58                     map[tx][ty]='w';
 59                     if(map[sx][sy]=='W')
 60                          map[sx][sy]='+';
 61                     else
 62                         map[sx][sy]='.';
 63                         sx+=dir[dr][0];
 64                     sy+=dir[dr][1];
 65                 }
 66                 else if(map[nextx][nexty]=='+')
 67                 {
 68                     map[nextx][nexty]='B';
 69                     map[tx][ty]='w';
 70                     if(map[sx][sy]=='W')
 71                          map[sx][sy]='+';
 72                     else
 73                         map[sx][sy]='.';
 74                         sx+=dir[dr][0];
 75                     sy+=dir[dr][1];
 76                     cnt--;
 77                 }
 78             }
 79             else if(map[tx][ty]=='B')
 80             {
 81                 if(map[nextx][nexty]=='.')
 82                 {
 83                     map[nextx][nexty]='b';
 84                     map[tx][ty]='W';
 85                     if(map[sx][sy]=='W')
 86                          map[sx][sy]='+';
 87                     else
 88                         map[sx][sy]='.';
 89                         sx+=dir[dr][0];
 90                     sy+=dir[dr][1];
 91                     cnt++;
 92                 }
 93                 else if(map[nextx][nexty]=='+')
 94                 {
 95                     map[nextx][nexty]='B';
 96                     map[tx][ty]='W';
 97                     if(map[sx][sy]=='W')
 98                          map[sx][sy]='+';
 99                     else
100                         map[sx][sy]='.';
101                         sx+=dir[dr][0];
102                     sy+=dir[dr][1];
103                 }
104             }
105         }
106         if(cnt==0)
107         {
108             flag=true;
109             return;
110         }
111     }
112 }
113 
114 int main()
115 {
116     int i,j;
117     int cases=1;
118     while(~scanf("%d%d",&r,&c),r||c)
119     {
120         cnt=0;
121         flag=false;
122         getchar();
123         for(i=0;i<r;i++)
124         {
125             for(j=0;j<c;j++)
126             {
127                 scanf("%c",&map[i][j]);
128                 if(map[i][j]=='w'||map[i][j]=='W')
129                 {
130                     sx=i;
131                     sy=j;
132                 }
133                 if(map[i][j]=='b')
134                     cnt++;
135             }
136             getchar();
137         }
138         gets(mov);
139         solve();
140         if(flag)
141             printf("Game %d: complete\n",cases++);
142         else
143             printf("Game %d: incomplete\n",cases++);
144         for(i=0;i<r;i++)
145         {
146             for(j=0;j<c;j++)
147             {
148                 printf("%c",map[i][j]);
149             }
150             printf("\n");
151         }
152     }
153     return 0;
154 }
155 /*
156 6 8
157 ########
158 #......#
159 #..b...#
160 #..WB..#
161 #......#
162 ########
163 LUURD
164 */
原文地址:https://www.cnblogs.com/XBWer/p/2687478.html