#include <iostream> #include <stdio.h> #include <cstdio> using namespace std; struct Point { int row; int column; }; int abs(int number) { return number < 0 ? -number : number; } int signum(int number) { return number > 0 ? 1 : number < 0 ? -1 : 0; } int mod(int value, int divisor) { return ((value % divisor) + divisor) % divisor; } const int EMPTY = '.'; const int LEFT = 0; const int UP = 1; const int RIGHT = 2; const int DOWN = 3; int directionParse(int character) { switch (character) { case 'L': return LEFT; case 'P': return RIGHT; case 'G': return UP; case 'D': return DOWN; } return -1; } int orientation(int direction) { return direction % 2; } int howRotateDirection(int directionA, int directionB) { return mod(directionB - directionA + 1, 4) - 1; } int rotateDirection(int clockwise, int direction) { return mod(direction + clockwise, 4); } int ROW = 0; int COLUMN = 1; const int MAX = 500; int nRows; int nColumns; bool isLegal(Point point) { return point.row < nRows && point.column < nColumns; } Point next(Point point) { if (point.column == nColumns - 1) { return { point.row + 1, 0 }; } else { return { point.row, point.column + 1 }; } } int readBoardAt(Point point, int board[][MAX]) { return board[point.row][point.column]; } void writeBoardAt(Point point, int value, int board[][MAX]) { board[point.row][point.column] = value; } void printBoard(int board[][MAX]) { for (int iRow = 0; iRow < nRows; iRow++) { for (int iColumn = 0; iColumn < nColumns; iColumn++) { printf("%c", board[iRow][iColumn]); } printf("\n"); } } void moveBoard(int direction, int board[][MAX]) { if (direction == LEFT) { for (int iRow = 0; iRow < nRows; iRow++) { int cursor = 0; for (int iColumn = 0; iColumn < nColumns; iColumn++) { if (board[iRow][iColumn] != EMPTY) { board[iRow][cursor] = board[iRow][iColumn]; cursor++; } } for (int iColumn = cursor; iColumn < nColumns; iColumn++) { board[iRow][iColumn] = EMPTY; } } } else if (direction == RIGHT) { for (int iRow = 0; iRow < nRows; iRow++) { int cursor = nColumns - 1; for (int iColumn = nColumns - 1; iColumn >= 0; iColumn--) { if (board[iRow][iColumn] != EMPTY) { board[iRow][cursor] = board[iRow][iColumn]; cursor--; } } for (int iColumn = cursor; iColumn >= 0; iColumn--) { board[iRow][iColumn] = EMPTY; } } } else if (direction == UP) { for (int iColumn = 0; iColumn < nColumns; iColumn++) { int cursor = 0; for (int iRow = 0; iRow < nRows; iRow++) { if (board[iRow][iColumn] != EMPTY) { board[cursor][iColumn] = board[iRow][iColumn]; cursor++; } } for (int iRow = cursor; iRow < nRows; iRow++) { board[iRow][iColumn] = EMPTY; } } } else if (direction == DOWN) { for (int iColumn = 0; iColumn < nColumns; iColumn++) { int cursor = nRows - 1; for (int iRow = nRows - 1; iRow >= 0; iRow--) { if (board[iRow][iColumn] != EMPTY) { board[cursor][iColumn] = board[iRow][iColumn]; cursor--; } } for (int iRow = cursor; iRow >= 0; iRow--) { board[iRow][iColumn] = EMPTY; } } } } void copyBoard(int source[][MAX], int target[][MAX]) { for (Point iPoint = { 0, 0 } ; isLegal(iPoint); iPoint = next(iPoint)) { int value = readBoardAt(iPoint, source); writeBoardAt(iPoint, value, target); } } Point readGraphAt(Point point, Point graph[][MAX]) { return graph[point.row][point.column]; } void writeGraphAt(Point point, Point value, Point graph[][MAX]) { graph[point.row][point.column] = value; } void copyGraph(Point source[][MAX], Point target[][MAX]) { for (Point iPoint = { 0, 0 } ; isLegal(iPoint); iPoint = next(iPoint)) { Point value = readGraphAt(iPoint, source); writeGraphAt(iPoint, value, target); } } int main() { int board[MAX][MAX]; // read board cin >> nRows; cin >> nColumns; for (int iRow = 0; iRow < nRows; iRow++) { getchar(); for (int iColumn = 0; iColumn < nColumns; iColumn++) { board[iRow][iColumn] = getchar(); } } // read moves int firstMove = -1; int rotations = 0; { int nMoves; cin >> nMoves; getchar(); firstMove = directionParse(getchar()); int lastMove = firstMove; int lastRotation = 0; nMoves--; for (int i = 0; i < nMoves; i++) { int iMove = directionParse(getchar()); int angle = mod(iMove - lastMove, 4); lastMove = iMove; if(angle == 0) { continue; } else if(angle == 2) { if(lastRotation == 0) { firstMove = iMove; } rotations -= lastRotation; if(rotations == 0) { rotations -= lastRotation; } lastRotation = -lastRotation; } else if(angle == 1) { if(lastRotation == 1 || lastRotation == 0) { rotations++; if(rotations == 0) { rotations++; } } lastRotation = 1; } else if(angle == 3) { if(lastRotation == -1 || lastRotation == 0) { rotations--; if(rotations == 0) { rotations--; } } lastRotation = -1; } } } int clockwise = signum(rotations); // handle short cases if (abs(rotations) <= 2 + 3 + 4) { for (int iRotation = 0; iRotation <= abs(rotations); iRotation++) { int move = rotateDirection(iRotation * clockwise, firstMove); moveBoard(move, board); } printBoard(board); return 0; } // move to LEFT UP int previousMove = -100; int lastMove = firstMove; moveBoard(lastMove, board); while (lastMove + previousMove != 1) { previousMove = lastMove; lastMove = rotateDirection(clockwise, lastMove); moveBoard(lastMove, board); rotations -= clockwise; } // calculate space int spaceInRow[nRows]; for (int iRow = 0; iRow < nRows; iRow++) { spaceInRow[iRow] = 0; for (int iColumn = 0; iColumn < nColumns; iColumn++) { if (board[iRow][iColumn] == EMPTY) { spaceInRow[iRow]++; } } } int spaceInColumn[nColumns]; for (int iColumn = 0; iColumn < nColumns; iColumn++) { spaceInColumn[iColumn] = 0; for (int iRow = 0; iRow < nRows; iRow++) { if (board[iRow][iColumn] == EMPTY) { spaceInColumn[iColumn]++; } } } // calculate single cycle graph Point singleCycleGraph[MAX][MAX]; for (Point iPoint = { 0, 0 } ; isLegal(iPoint); iPoint = next(iPoint)) { if (readBoardAt(iPoint, board) == EMPTY) { writeGraphAt(iPoint, iPoint, singleCycleGraph); continue; } int row = iPoint.row; int column = iPoint.column; if (clockwise == 1) { column += spaceInRow[row]; // RIGHT row += spaceInColumn[nColumns - 1 - column]; // DOWN column -= spaceInRow[nRows - 1 - row];// LEFT row -= spaceInColumn[column]; // UP } else { row += spaceInColumn[column]; column += spaceInRow[nRows - 1 - row]; row -= spaceInColumn[nColumns - 1 - column]; column -= spaceInRow[row]; } writeGraphAt(iPoint, { row, column }, singleCycleGraph); } // initialize result graph with identities; Point allCyclesGraph[MAX][MAX]; for (Point iPoint = { 0, 0 }; isLegal(iPoint); iPoint = next(iPoint)) { writeGraphAt(iPoint, iPoint, allCyclesGraph); } int absCycles = rotations * clockwise / 4; Point copy[MAX][MAX]; for (int power = 20; power >= 0; power--) { // square graph copyGraph(allCyclesGraph, copy); for (Point iPoint = { 0, 0 }; isLegal(iPoint); iPoint = next(iPoint)) { Point firstJump = readGraphAt(iPoint, copy); Point secondJump = readGraphAt(firstJump, copy); writeGraphAt(iPoint, secondJump, allCyclesGraph); } // add 1 single cycle if((absCycles >> power) % 2 == 1){ copyGraph(allCyclesGraph, copy); for (Point iPoint = { 0, 0 }; isLegal(iPoint); iPoint = next(iPoint)) { Point firstJump = readGraphAt(iPoint, copy); Point secondJump = readGraphAt(firstJump, singleCycleGraph); writeGraphAt(iPoint, secondJump, allCyclesGraph); } } } int initialBoard[500][500]; copyBoard(board , initialBoard); // apply merged links to board for (int iRow = 0; iRow < nRows; iRow++) { for (int iColumn = 0; iColumn < nColumns; iColumn++) { if (initialBoard[iRow][iColumn] == EMPTY) { continue; } int row = allCyclesGraph[iRow][iColumn].row; int column = allCyclesGraph[iRow][iColumn].column; board[row][column] = initialBoard[iRow][iColumn]; } } // apply trailing moves int absRemainingRotations = abs(rotations) % 4; for (int i = 0; i < absRemainingRotations; i++) { lastMove = rotateDirection(clockwise, lastMove); moveBoard(lastMove, board); } printBoard(board); return 0; }
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | #include <iostream> #include <stdio.h> #include <cstdio> using namespace std; struct Point { int row; int column; }; int abs(int number) { return number < 0 ? -number : number; } int signum(int number) { return number > 0 ? 1 : number < 0 ? -1 : 0; } int mod(int value, int divisor) { return ((value % divisor) + divisor) % divisor; } const int EMPTY = '.'; const int LEFT = 0; const int UP = 1; const int RIGHT = 2; const int DOWN = 3; int directionParse(int character) { switch (character) { case 'L': return LEFT; case 'P': return RIGHT; case 'G': return UP; case 'D': return DOWN; } return -1; } int orientation(int direction) { return direction % 2; } int howRotateDirection(int directionA, int directionB) { return mod(directionB - directionA + 1, 4) - 1; } int rotateDirection(int clockwise, int direction) { return mod(direction + clockwise, 4); } int ROW = 0; int COLUMN = 1; const int MAX = 500; int nRows; int nColumns; bool isLegal(Point point) { return point.row < nRows && point.column < nColumns; } Point next(Point point) { if (point.column == nColumns - 1) { return { point.row + 1, 0 }; } else { return { point.row, point.column + 1 }; } } int readBoardAt(Point point, int board[][MAX]) { return board[point.row][point.column]; } void writeBoardAt(Point point, int value, int board[][MAX]) { board[point.row][point.column] = value; } void printBoard(int board[][MAX]) { for (int iRow = 0; iRow < nRows; iRow++) { for (int iColumn = 0; iColumn < nColumns; iColumn++) { printf("%c", board[iRow][iColumn]); } printf("\n"); } } void moveBoard(int direction, int board[][MAX]) { if (direction == LEFT) { for (int iRow = 0; iRow < nRows; iRow++) { int cursor = 0; for (int iColumn = 0; iColumn < nColumns; iColumn++) { if (board[iRow][iColumn] != EMPTY) { board[iRow][cursor] = board[iRow][iColumn]; cursor++; } } for (int iColumn = cursor; iColumn < nColumns; iColumn++) { board[iRow][iColumn] = EMPTY; } } } else if (direction == RIGHT) { for (int iRow = 0; iRow < nRows; iRow++) { int cursor = nColumns - 1; for (int iColumn = nColumns - 1; iColumn >= 0; iColumn--) { if (board[iRow][iColumn] != EMPTY) { board[iRow][cursor] = board[iRow][iColumn]; cursor--; } } for (int iColumn = cursor; iColumn >= 0; iColumn--) { board[iRow][iColumn] = EMPTY; } } } else if (direction == UP) { for (int iColumn = 0; iColumn < nColumns; iColumn++) { int cursor = 0; for (int iRow = 0; iRow < nRows; iRow++) { if (board[iRow][iColumn] != EMPTY) { board[cursor][iColumn] = board[iRow][iColumn]; cursor++; } } for (int iRow = cursor; iRow < nRows; iRow++) { board[iRow][iColumn] = EMPTY; } } } else if (direction == DOWN) { for (int iColumn = 0; iColumn < nColumns; iColumn++) { int cursor = nRows - 1; for (int iRow = nRows - 1; iRow >= 0; iRow--) { if (board[iRow][iColumn] != EMPTY) { board[cursor][iColumn] = board[iRow][iColumn]; cursor--; } } for (int iRow = cursor; iRow >= 0; iRow--) { board[iRow][iColumn] = EMPTY; } } } } void copyBoard(int source[][MAX], int target[][MAX]) { for (Point iPoint = { 0, 0 } ; isLegal(iPoint); iPoint = next(iPoint)) { int value = readBoardAt(iPoint, source); writeBoardAt(iPoint, value, target); } } Point readGraphAt(Point point, Point graph[][MAX]) { return graph[point.row][point.column]; } void writeGraphAt(Point point, Point value, Point graph[][MAX]) { graph[point.row][point.column] = value; } void copyGraph(Point source[][MAX], Point target[][MAX]) { for (Point iPoint = { 0, 0 } ; isLegal(iPoint); iPoint = next(iPoint)) { Point value = readGraphAt(iPoint, source); writeGraphAt(iPoint, value, target); } } int main() { int board[MAX][MAX]; // read board cin >> nRows; cin >> nColumns; for (int iRow = 0; iRow < nRows; iRow++) { getchar(); for (int iColumn = 0; iColumn < nColumns; iColumn++) { board[iRow][iColumn] = getchar(); } } // read moves int firstMove = -1; int rotations = 0; { int nMoves; cin >> nMoves; getchar(); firstMove = directionParse(getchar()); int lastMove = firstMove; int lastRotation = 0; nMoves--; for (int i = 0; i < nMoves; i++) { int iMove = directionParse(getchar()); int angle = mod(iMove - lastMove, 4); lastMove = iMove; if(angle == 0) { continue; } else if(angle == 2) { if(lastRotation == 0) { firstMove = iMove; } rotations -= lastRotation; if(rotations == 0) { rotations -= lastRotation; } lastRotation = -lastRotation; } else if(angle == 1) { if(lastRotation == 1 || lastRotation == 0) { rotations++; if(rotations == 0) { rotations++; } } lastRotation = 1; } else if(angle == 3) { if(lastRotation == -1 || lastRotation == 0) { rotations--; if(rotations == 0) { rotations--; } } lastRotation = -1; } } } int clockwise = signum(rotations); // handle short cases if (abs(rotations) <= 2 + 3 + 4) { for (int iRotation = 0; iRotation <= abs(rotations); iRotation++) { int move = rotateDirection(iRotation * clockwise, firstMove); moveBoard(move, board); } printBoard(board); return 0; } // move to LEFT UP int previousMove = -100; int lastMove = firstMove; moveBoard(lastMove, board); while (lastMove + previousMove != 1) { previousMove = lastMove; lastMove = rotateDirection(clockwise, lastMove); moveBoard(lastMove, board); rotations -= clockwise; } // calculate space int spaceInRow[nRows]; for (int iRow = 0; iRow < nRows; iRow++) { spaceInRow[iRow] = 0; for (int iColumn = 0; iColumn < nColumns; iColumn++) { if (board[iRow][iColumn] == EMPTY) { spaceInRow[iRow]++; } } } int spaceInColumn[nColumns]; for (int iColumn = 0; iColumn < nColumns; iColumn++) { spaceInColumn[iColumn] = 0; for (int iRow = 0; iRow < nRows; iRow++) { if (board[iRow][iColumn] == EMPTY) { spaceInColumn[iColumn]++; } } } // calculate single cycle graph Point singleCycleGraph[MAX][MAX]; for (Point iPoint = { 0, 0 } ; isLegal(iPoint); iPoint = next(iPoint)) { if (readBoardAt(iPoint, board) == EMPTY) { writeGraphAt(iPoint, iPoint, singleCycleGraph); continue; } int row = iPoint.row; int column = iPoint.column; if (clockwise == 1) { column += spaceInRow[row]; // RIGHT row += spaceInColumn[nColumns - 1 - column]; // DOWN column -= spaceInRow[nRows - 1 - row];// LEFT row -= spaceInColumn[column]; // UP } else { row += spaceInColumn[column]; column += spaceInRow[nRows - 1 - row]; row -= spaceInColumn[nColumns - 1 - column]; column -= spaceInRow[row]; } writeGraphAt(iPoint, { row, column }, singleCycleGraph); } // initialize result graph with identities; Point allCyclesGraph[MAX][MAX]; for (Point iPoint = { 0, 0 }; isLegal(iPoint); iPoint = next(iPoint)) { writeGraphAt(iPoint, iPoint, allCyclesGraph); } int absCycles = rotations * clockwise / 4; Point copy[MAX][MAX]; for (int power = 20; power >= 0; power--) { // square graph copyGraph(allCyclesGraph, copy); for (Point iPoint = { 0, 0 }; isLegal(iPoint); iPoint = next(iPoint)) { Point firstJump = readGraphAt(iPoint, copy); Point secondJump = readGraphAt(firstJump, copy); writeGraphAt(iPoint, secondJump, allCyclesGraph); } // add 1 single cycle if((absCycles >> power) % 2 == 1){ copyGraph(allCyclesGraph, copy); for (Point iPoint = { 0, 0 }; isLegal(iPoint); iPoint = next(iPoint)) { Point firstJump = readGraphAt(iPoint, copy); Point secondJump = readGraphAt(firstJump, singleCycleGraph); writeGraphAt(iPoint, secondJump, allCyclesGraph); } } } int initialBoard[500][500]; copyBoard(board , initialBoard); // apply merged links to board for (int iRow = 0; iRow < nRows; iRow++) { for (int iColumn = 0; iColumn < nColumns; iColumn++) { if (initialBoard[iRow][iColumn] == EMPTY) { continue; } int row = allCyclesGraph[iRow][iColumn].row; int column = allCyclesGraph[iRow][iColumn].column; board[row][column] = initialBoard[iRow][iColumn]; } } // apply trailing moves int absRemainingRotations = abs(rotations) % 4; for (int i = 0; i < absRemainingRotations; i++) { lastMove = rotateDirection(clockwise, lastMove); moveBoard(lastMove, board); } printBoard(board); return 0; } |