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 ...
随机推荐
- 贪心 UVALive 6832 Bit String Reordering
题目传送门 /* 贪心:按照0或1开头,若不符合,选择后面最近的进行交换.然后选取最少的交换次数 */ #include <cstdio> #include <algorithm&g ...
- 题解报告:hdu 2058 The sum problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2058 问题描述 给定一个序列1,2,3,...... N,你的工作是计算所有可能的子序列,其子序列的总 ...
- Linux oraenv Tips
Linux for the Oracle DBA -Customizing the Oracle User's Environment There are many ways to customize ...
- 转】在Ubuntu中安装Cassandra
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/ 感谢! Posted: Mar 22, 2014 Tags: cas ...
- Netflix正式开源其API网关Zuul 2--转
微信公众号:聊聊架构 5 月 21 日,Netflix 在其官方博客上宣布正式开源微服务网关组件 Zuul 2.Netflix 公司是微服务界的楷模,他们有大规模生产级微服务的成功应用案例,也开源了相 ...
- vue--组件中的自定义事件
父组件通过props向子组件传递数据,子组件通过自定义事件向父组件传递信息. 在子组件中通过$emit触发事件,父组件在直接使用子组件的地方使用v-on(即@)来监听子组件触发的事件. 举例:(不知道 ...
- 【C++】模板简述(三):类模板
上文简述了C++模板中的函数模板的格式.实例.形参.重载.特化及参数推演,本文主要介绍类模板. 一.类模板格式 类模板也是C++中模板的一种,其格式如下: template<class 形参名1 ...
- echarts 外观效果修改
<!DOCTYPE html> <html> <head> <title></title> <link rel="style ...
- 计算器Pro应用项目源码
本计算器实现了一些简单的功能,可能本身还存在一些缺陷,希望大家提建议,能够改进一下. 源码项目我已经上传到源码天堂那里了:http://code.662p.com/list/11_1.html < ...
- libevent学习之网络通信
服务器端要实现网络通信,肯定会用到socket等函数,这几个函数应该没什么问题.libevent默认情况下是单线程的,可以配置成多线程,每个线程有一个event_base,对应一个struct eve ...