POJ 3344 & HDU 2414 Chessboard Dance(模拟)
来源:互联网 发布:ubuntu 硬盘大小 编辑:程序博客网 时间:2024/05/14 21:12
题目链接:
PKU:http://poj.org/problem?id=3344
HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2414
Description
Another boring Friday afternoon, Betty the Beetle thinks how to amuse herself. She goes out of her hiding place to take a walk around the living room in Bennett's house. Mr. and Mrs. Bennett are out to the theatre and there is a chessboard on the table! "The best time to practice my chessboard dance," Betty thinks! She gets so excited that she does not note that there are some pieces left on the board and starts the practice session! She has a script showing her how to move on the chessboard. The script is a sequence like the following example:
At each instant of time Betty, stands on a square of the chessboard, facing one of the four directions (up, down, left, right) when the board is viewed from the above. Performing a "move n" instruction, she moves n squares forward in her current direction. If moving n squares goes outside the board, she stays at the last square on the board and does not go out. There are three types of turns: turn right, turn left, and turn back, which change the direction of Betty. Note that turning does not change the position of Betty.
If Betty faces a chess piece when moving, she pushes that piece, together with all other pieces behind (a tough beetle she is!). This may cause some pieces fall of the edge of the chessboard, but she doesn't care! For example, in the following figure, the left board shows the initial state and the right board shows the state after performing the script in the above example. Upper-case and lower-case letters indicate the white and black pieces respectively. The arrow shows the position of Betty along with her direction. Note that during the first move, the black king (r) falls off the right edge of the board!
You are to write a program that reads the initial state of the board as well as the practice dance script, and writes the final state of the board after the practice.
Input
There are multiple test cases in the input. Each test case has two parts: the initial state of the board and the script. The board comes in eight lines of eight characters. The letters r, d, t, a, c, p indicate black pieces, R, D, T, A, C, P indicate the white pieces and the period (dot) character indicates an empty square. The square from which Betty starts dancing is specified by one of the four characters <, >, ^, and v which also indicates her initial direction (left, right, up, and down respectively). Note that the input is not necessarily a valid chess game status.
The script comes immediately after the board. It consists of several lines (between 0 and 1000). In each line, there is one instruction in one of the following formats (n is a non-negative integer number):
move n
turn left
turn right
turn back
At the end of each test case, there is a line containing a single # character. The last line of the input contains two dash characters.
Output
The output for each test case should show the state of the board in the same format as the input. Write an empty line in the output after each board.
Sample Input
.....c...p..A..tD..>T.Pr....aP.Pp.d.C........p.R................move 2turn rightmove 3turn leftturn leftmove 1#--
Sample Output
.....c...p..A..tD.....TP....a..Pp.d.C^.........R.....P.......p..
Source
题意:
模拟推箱子的过程,按照所给的步骤推完后输出最终的棋盘状态!
PS:
注意在推的过程中,不能走出棋盘外,所推的棋子可以出棋盘。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int maxn = 27;char mm[maxn][maxn];struct node{ int x; int y; int dir;} p;char dir[4] = {'v','>','^','<'};//写为下、右、上、左是为了turn back操作int dx[4] = {1,0,-1,0};int dy[4] = {0,1,0,-1};int vis(int x, int y){ if(x<1 || x>8) return 0; if(y<1 || y>8) return 0; return 1;}void DFS(int x, int y){ if(mm[x][y]=='.') { mm[x][y]=mm[x-dx[p.dir]][y-dy[p.dir]]; mm[x-dx[p.dir]][y-dy[p.dir]]='.'; } else { DFS(x+dx[p.dir],y+dy[p.dir]); mm[x][y]=mm[x-dx[p.dir]][y-dy[p.dir]]; mm[x-dx[p.dir]][y-dy[p.dir]]='.'; }}void Rush(){ for(int i = 0; i <= 8; i++) { //初始化棋盘外的一圈全为'.' mm[0][i]=mm[i][0]=mm[9][i]=mm[i][9]='.'; } mm[p.x][p.y] = '.'; if(!vis(p.x+dx[p.dir],p.y+dy[p.dir])) return ; DFS(p.x+dx[p.dir],p.y+dy[p.dir]); p.x += dx[p.dir]; p.y += dy[p.dir];}int main(){ char a[maxn], b[maxn]; int step; while(scanf("%s",mm[1]+1)) { if(mm[1][1] == '-') break; for(int i = 2; i <= 8; i++) { scanf("%s",mm[i]+1); } for(int i = 1; i <= 8; i++) { for(int j = 1; j <= 8; j++) { if(mm[i][j] == 'v') { p.x = i,p.y = j,p.dir = 0; } else if(mm[i][j] == '>') { p.x = i,p.y = j,p.dir = 1; } else if(mm[i][j] == '^') { p.x = i,p.y = j,p.dir = 2; } else if(mm[i][j] == '<') { p.x = i,p.y = j,p.dir = 3; } } } while(scanf("%s",a)) { if(a[0] == '#') break; if(a[0] == 't') { scanf("%s",b); if(b[0] == 'l') p.dir=(p.dir+1)%4; else if(b[0]=='r') p.dir=(p.dir+3)%4; else p.dir=(p.dir+2)%4; } else { scanf("%d",&step); for(int i = 0; i < step; i++) { Rush(); } } } mm[p.x][p.y] = dir[p.dir]; for(int i = 1; i <= 8; i++) { for(int j = 1; j <= 8; j++) { printf("%c",mm[i][j]); } printf("\n"); } printf("\n"); } return 0;}
- POJ 3344 & HDU 2414 Chessboard Dance(模拟)
- HDU 2414 Chessboard Dance (强行模拟)
- HDU 2414 Chessboard Dance
- HDU 2414 Chessboard Dance
- POJ 3344 Chessboard Dance 可能会
- 【瞎搞】 HDU 2414 Chessboard Dance
- UVALive 4222 Dance(模拟)
- poj 1657 Distance on Chessboard(模拟贪心)
- 思维 hdu 5100 (chessboard)
- 【HDU】-5100-Chessboard(思维)
- Chessboard HDU
- POJ 2446 Chessboard (二分匹配)
- POJ 2446 Chessboard (匈牙利算法)
- POJ 2446 Chessboard(二分图匹配)
- POJ 2446 Chessboard(二分图匹配)
- POJ 2446 Chessboard(二分图匹配)
- POJ 2446 Chessboard(二分匹配-hungary)
- poj Chessboard 2446 (最大匹配&转换)
- bzoj1689 [Usaco2005 Open] Muddy roads 泥泞的路
- 章鱼哥出品_VB.NET ComboBox、 TextBox 实现自动完成、自动过滤 模糊查询的功能
- POJ 3046 - Ant Counting(dp多种背包变型)
- 程序员哲思-“不知道”的三重境界
- .net强制退出主窗口的方法——Application.Exit()方法和Environment.Exit(0)方法
- POJ 3344 & HDU 2414 Chessboard Dance(模拟)
- NSSortDescriptor(数组排序)
- 判断两链表是否相交
- oracle 8.1.7下载
- android面试1
- ACdream 1224(贪心处理)
- jquery 实现iframe高度自适应
- POJ3352-Road Construction(边连通分量)
- acdream 1220 哈夫曼树