博客
关于我
【luogu2033】【模拟】Chessboard Dance
阅读量:319 次
发布时间:2019-03-04

本文共 2334 字,大约阅读时间需要 7 分钟。

题目描述

在棋盘上跳舞是件有意思的事情。现在给你一张国际象棋棋盘和棋盘上的一些子以及你的初始位置和方向。求按一定操作后,棋盘的状态。

操作有四种,描述如下:

move n n是非负整数,表示你按目前所在方向前进n步,如果即将走出棋盘,则停止。如果面前有棋子,则将其向前推一步。

turn left 向左转90度

turn right 向右转90度

turn back 向后转

输入格式

输入前8行,每行8个字符,给出棋盘状态。“.”表示该格为空,字母表示棋子,不同字母表示不同的棋子。你所在位置用“^”、“<”、“>”、“v”四个字母中一个表示,分别表示你的方向上、左、右、下。

接下来有若干行,每行一个操作。以“#”结束。操作数不超过1000个。

输出格式

输出8行,每行8个字符,表示经过一系列操作后棋盘和你的状态。表示方法同输入。

输入输出样例
输入 #1
......bA.....^..................................................move 2turn rightmove 1#
输出 #1
......>b........................................................

解题思路

纯模拟,在处理转弯的时候需要小注意一下,因为棋盘只有8x8,所以推棋子时暴力推就好了


Code

#include 
#include
#include
using namespace std;const int way[5][2] = { { 0, 0}, { -1, 0}, { 0, -1}, { 1, 0}, { 0, 1}};//方向int x, y, z, k;char a[10][10], c, t;string s;bool check (int x, int y){ //判断是否超界 return (x > 0 && x <= 8 && y > 0 && y <= 8);}bool checka (char c){ //判断是不是字母 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');}int kay (char c){ //由于没用map,所以就把字符方向转成数字方向下标 if (c == '^') return 1; if (c == '<') return 2; if (c == 'v') return 3; if (c == '>') return 4; return 0; }char key (int x){ //在棋盘中要把数字方向转成字符方向 if (x == 1) return '^'; if (x == 2) return '<'; if (x == 3) return 'v'; if (x == 4) return '>';}void move (int s, int z){ while (check (x + way[z][0], y + way[z][1]) && (s--)) { a[x][y] = '.'; x += way[z][0], y += way[z][1];//往前走 if (checka (a[x][y]))//如果碰到了棋子 { int xx = x, yy = y, c = a[xx][yy]; while (check (xx + way[z][0], yy + way[z][1]) && checka (c))//暴力往前推 { xx += way[z][0], yy += way[z][1]; t = a[xx][yy], a[xx][yy] = c, c = t; } } } a[x][y] = key(z);}int main(){ for (int i = 1; i <= 8; i++) for (int j = 1; j <= 8; j++) { cin >> a[i][j]; if (kay (a[i][j])) { x = i, y = j; z = kay(a[i][j]); } } cin >> s; while (s != "#") { if (s == "move") { scanf ("%d", &k); move(k, z); }else { cin >> s; if (s == "left") { z++; if (z == 5) z = 1; a[x][y] = key(z); }else if (s == "right") { z--; if (z == 0) z = 4; a[x][y] = key(z); }else { z--; if (z == 0) z = 4; z--; if (z == 0) z = 4; a[x][y] = key(z); } } cin >> s; } for (int i = 1; i <= 8; i++) { for (int j = 1; j <= 8; j++) printf ("%c", a[i][j]); printf ("\n"); }}

转载地址:http://wuiq.baihongyu.com/

你可能感兴趣的文章
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>