UVA 1589 象棋
题意:
给出一个黑方的将, 然后 红方有 2 ~ 7 个棋子, 给出摆放位置,问是否已经把黑将将死, 红方已经将军。
分析:
分情况处理, 车 马 炮, 红将情况跟车是一样的。
建一个数组board保存棋局, 然后建一个数组moveable用来算出黑将当前位置能走的格子(1可走, 0不可走, 一开始都是1), 判断一下黑将上下左右四个方位是否能走, 而且走了之后算一下当前位置moveable是否0即可。
代码:
#include <bits/stdc++.h>
using namespace std;
int N, bx, by;
int board[][];
int tboard[][];
int movable[][];
struct CHESS
{
char name;
int x, y;
}chess[];
void Chariot(int x, int y){
for(int i = x - ; i >= ; i--){
movable[i][y] = ;
if(tboard[i][y] && tboard[i][y] != 'B')
break;
}
for(int i = x + ; i <= ; i++){
movable[i][y] = ;
if(tboard[i][y] && tboard[i][y] != 'B')
break;
}
for(int i = y + ; i <= ; i++){
movable[x][i] = ;
if(tboard[x][i] && tboard[x][i] != 'B')
break;
}
for(int i = y - ; i >= ; i --){
movable[x][i] = ;
if(tboard[x][i] && tboard[x][i] != 'B')
break;
}
}
void Cannon(int x, int y)
{
for(int i = x - ; i >= ; i --){
if(tboard[i][y] && tboard[i][y] != 'B'){
for(int j = i - ; j >= ; j--){
movable[j][y] = ;
if(tboard[j][y] && tboard[j][y] != 'B')
break;
}
break;
}
}
for(int i = x + ; i <= ; i++){
if(tboard[i][y] && tboard[i][y] != 'B'){
for(int j = i+; j <= ; j++){
movable[j][y] = ;
if(tboard[j][y] && tboard[j][y] != 'B')
break;
}
break;
}
}
for(int i = y - ; i >= ; i--){
if(tboard[x][i] && tboard[x][i] != 'B'){
for(int j = i - ; j >= ; j--){
movable[x][j] = ;
if(tboard[x][j] && tboard[j][y] != 'B')
break;
}
break;
}
} for(int i = y + ; i <= ; i++){
if(tboard[x][i] && tboard[x][i] != 'B'){
for(int j = i + ; j <= ; j++){
movable[x][j] = ;
if(tboard[x][j] && tboard[j][y] != 'B')
break;
}
break;
}
} }
int dir[][] = {{-,},{,},{,},{,-}};//上右下左
int dir2[][] = {{-,-},{-,},{-,},{,},{,},{,-},{,-},{-,-}};
void Horse(int x, int y)
{
for(int i = ; i < ; i++){
int tx = x, ty = y;
tx += dir[i][];
ty += dir[i][];
if(tx >= && ty >= && tx <= && ty <= );
else continue;
if(tboard[tx][ty]) continue;
tx = x; ty = y;
tx += dir2[(i+)* - ][];
ty += dir2[(i+)* - ][];
if(tx >= && ty >= && tx <= && ty <= )
movable[tx][ty] = ;
tx = x; ty = y;
tx += dir2[(i+)* - ][];
ty += dir2[(i+)* - ][];
if(tx >= && ty >= && tx <= && ty <= )
movable[tx][ty] = ;
}
}
int main()
{ while(scanf("%d %d %d", &N, &bx, &by) != EOF){
if(N == ) break;
memset(board, , sizeof(board));
board[bx][by] = 'B';
for(int i = ; i <= ; i++){
for(int j = ; j<= ; j++){
movable[i][j] = ;
}
} for(int i = ; i < N; i++){
int x, y;
char name;
scanf(" %c", &name);
scanf("%d %d", &x, &y);
board[x][y] = name;
}
int ok = ;
for(int i = ; i < ; i++)
{
int tx = bx, ty = by;
tx += dir[i][];
ty += dir[i][];
if(tx >= && tx <= && ty >= && ty <= );
else continue;
for(int k = ; k <= ; k++){
for(int j = ; j <= ; j++){
tboard[k][j] = board[k][j];
}
}
tboard[tx][ty] = 'B'; for(int k = ; k <= ; k++){
for(int j = ; j<= ; j++){
movable[k][j] = ;
}
} for(int k = ; k <= ; k++){
for(int j = ; j <= ; j++){
switch(tboard[k][j]){
case 'G':
Chariot(k,j);
break;
case 'R':
Chariot(k,j);
break;
case 'H':
Horse(k,j);
break;
case 'C':
Cannon(k,j);
break;
}
}
} if(movable[tx][ty])
ok = ;
}
printf("%s\n", ok ? "NO":"YES"); }
return ;
}
UVA 1589 象棋的更多相关文章
- ●UVa 1589 Xiangqi(模拟)
●赘述题意 给出一个中国象棋残局,告诉各个棋子的位置,黑方只有1枚“将”,红方有至少2枚,至多7枚棋子,包含1枚“帅G”,和若干枚“车R”,“马H”,“炮C”.当前为黑方的回合,问黑方的“将”能否在移 ...
- 【每日一题】 UVA - 1589 Xiangqi 函数+模拟 wa了两天
题意:背景就是象棋, 题解:坑点1(wa的第一天):将军可以吃掉相邻的棋子,(然行列也写反了orz) 坑点2(wa的第二天):将军到马要反过来写,边界有误,并且第一次碰到的车才算(写到后来都忘了) # ...
- UVA 1589:Xiangqi (模拟 Grade D)
题目: 象棋,黑棋只有将,红棋有帅车马炮.问是否死将. 思路: 对方将四个方向走一步,看看会不会被吃. 代码: 很难看……WA了很多发,还越界等等. #include <cstdio> # ...
- uva 1589 by sixleaves
坑爹的模拟题目.自己对于这种比较复杂点得模拟题的能力概述还不够,还多加练习.贴别是做得时候一直再想如何检查车中间有没有棋子,炮中间有没有棋子.到网上参考别人的代码才发先这么简单的办法,自己尽然想不到. ...
- Uva - 1589 - Xiangqi
Xiangqi is one of the most popular two-player board games in China. The game represents a battle bet ...
- UVa 11538 象棋中的皇后
https://vjudge.net/problem/UVA-11538 题意: n×m的棋盘,有多少种方法放置两个相互攻击的皇后? 思路: 这两个皇后互相攻击的方式只有3种,在同一行,在同一列,或在 ...
- dir命令只显示文件名
dir /b 就是ls -f的效果 1057 -- FILE MAPPING_web_archive.7z 2007 多校模拟 - Google Search_web_archive.7z 2083 ...
- 【UVA】1589 Xiangqi(挖坑待填)
题目 题目 分析 无力了,noip考完心力憔悴,想随便切道题却码了250line,而且还是错的,知道自己哪里错了,但特殊情况判起来太烦了,唯一选择是重构,我却没有这勇气. 有空再写吧,最近真的 ...
- UVA 439 Knight Moves(BFS)
Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...
随机推荐
- 软件常用版本英文snapshot和ga
版本号,顾名思义,系统.架包.软件的标识号.版本号的数字信息通俗易懂, 格式:主版本号+次版本+(修正版本号build-可选)+(编译版本号-可选)+英文常见号(重点). 常见号:英文各种架包名,Ma ...
- Django中的cookie和session实现
cookie from django.shortcuts import render, HttpResponse, redirect # 此装饰器的作用就是讲所有没有cookie验证的页面都需要验证后 ...
- [AHOI2007]密码箱
Description 在一次偶然的情况下,小可可得到了一个密码箱,听说里面藏着一份古代流传下来的藏宝图,只要能破解密码就能打开箱子,而箱子背面刻着的古代图标,就是对密码的提示.经过艰苦的破译,小可可 ...
- _bzoj1007 [HNOI2008]水平可见直线【单调栈】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1007 按斜率排序,去掉斜率相同时,截距较小的直线(即只保留该斜率下截距最大的直线).若当前直 ...
- Linux环境下SolrCloud集群环境搭建关键步骤
Linux环境下SolrCloud集群环境搭建关键步骤. 前提条件:已经完成ZooKeeper集群环境搭建. 一.下载介质 官网下载地址:http://www.apache.org/dyn/close ...
- C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响)
C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响),如以下代码将无法通过编译. foreach (int x in myArray) { x++; //错误代码,因为改变 ...
- 10-1 浮动框架iframe
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- JavaScript——max-age
https://zhidao.baidu.com/question/391047416053664205.html 页面优化方式之一,延长过期时间.默认max-age=0
- vscode前端开发软件配搭好用的插件
使用方法,可以在官网中搜索需要的插件或者在VsCode的“”扩展“”中搜索需要的插件添加方法使用Ctrl+P, 输入 ext install xxxx ,搜索要安装的插件,点击安装按钮即可(各取所需插 ...
- webpac入门
基于node环境,必须确保node已经安装:node-v,npm-v 基础入门 前身:browserify 缺点:只能转化JS webpack作用:一切都是模块化(js.css图片等),一个模块加载器 ...