HDU 4121 Xiangqi 模拟题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=4121
首先对标题赞一个,非要叫 “Xiangqi” 而不是 ”中国象棋“ 或者 ”Chinese chess“ 。。
然后是题意:黑棋只剩下一个”将“了,红棋各种 ”车” “马” “炮“,判断黑棋是不是死棋了。。。
对于一个会下中国象棋的选手简直简单啊,第一次手贱加脑残WA一次,稍微一改就0ms AC了,模拟题没什么可以讲的,代码中有详细注释。
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 5 char palace[11][10]; //棋盘 6 7 //判断(x, y)点有没有棋子 8 char has_chess(int x, int y) 9 { 10 if(x < 1 || x > 10 || y < 1 || y > 9) 11 return 0; 12 return palace[x][y]; 13 } 14 15 //判断(x, y)点是不是棋子c 16 bool is_chess(int x, int y, char c) 17 { 18 return has_chess(x, y) == c; 19 } 20 21 //判断(x, y)点的左边直线上有没有棋子c,有的话返回横坐标 22 int has_left(int x, int y, char c) 23 { 24 for(int i = 1; i < y; i++) 25 { 26 if(palace[x][i] == c) 27 return i; 28 } 29 return 0; 30 } 31 32 //判断(x, y)点的右边直线上有没有棋子c,有的话返回横坐标 33 int has_right(int x, int y, char c) 34 { 35 for(int i = 9; i > y; i--) 36 { 37 if(palace[x][i] == c) 38 return i; 39 } 40 return 0; 41 } 42 43 //判断(x, y)点的上边直线上有没有棋子c,有的话返回纵坐标 44 int has_up(int x, int y, char c) 45 { 46 for(int i = 1; i < x; i++) 47 { 48 if(palace[i][y] == c) 49 return i; 50 } 51 return 0; 52 } 53 54 //判断(x, y)点的下边直线上有没有棋子c,有的话返回纵坐标 55 int has_down(int x, int y, char c) 56 { 57 for(int i = 10; i > x; i--) 58 { 59 if(palace[i][y] == c) 60 return i; 61 } 62 return 0; 63 } 64 65 //判断(x1, y)到(x2, y)之间棋子的数量 66 int cnt_chess_x(int x1, int x2, int y) 67 { 68 if(x1 > x2) 69 std::swap(x1, x2); 70 int cnt = 0; 71 for(x1++; x1 < x2; x1++) 72 { 73 if(palace[x1][y]) 74 cnt++; 75 } 76 return cnt; 77 } 78 79 //判断(x, y1)到(x, y2)之间棋子的数量 80 int cnt_chess_y(int y1, int y2, int x) 81 { 82 if(y1 > y2) 83 std::swap(y1, y2); 84 int cnt = 0; 85 for(y1++; y1 < y2; y1++) 86 { 87 if(palace[x][y1]) 88 cnt++; 89 } 90 return cnt; 91 } 92 93 //判断能否被“帅”吃掉 94 bool general(int x, int y) 95 { 96 int gx, gy; 97 for(int i = 8; i <= 10; i++) 98 for(int j = 4; j <= 6; j++) 99 if(palace[i][j] == 'G') 100 { 101 gx = i; 102 gy = j; 103 } 104 if(gy != y)return 0; 105 return cnt_chess_x(gx, x, y) == 0; 106 } 107 108 //判断能否被“车”吃掉 109 bool chariot(int x, int y) 110 { 111 int tmp = has_left(x, y, 'R'); 112 if(tmp != 0 && cnt_chess_y(tmp, y, x) == 0) 113 return 1; 114 tmp = has_right(x, y, 'R'); 115 if(tmp != 0 && cnt_chess_y(tmp, y, x) == 0) 116 return 1; 117 tmp = has_up(x, y, 'R'); 118 if(tmp != 0 && cnt_chess_x(tmp, x, y) == 0) 119 return 1; 120 tmp = has_down(x, y, 'R'); 121 if(tmp != 0 && cnt_chess_x(tmp, x, y) == 0) 122 return 1; 123 return 0; 124 } 125 126 //判断能否被“马”吃掉 127 bool horse(int x, int y) 128 { 129 return(is_chess(x-2, y-1, 'H') && !has_chess(x-1, y-1) 130 || is_chess(x-2, y+1, 'H') && !has_chess(x-1, y+1) 131 || is_chess(x+2, y-1, 'H') && !has_chess(x+1, y-1) 132 || is_chess(x+2, y+1, 'H') && !has_chess(x+1, y+1) 133 || is_chess(x-1, y-2, 'H') && !has_chess(x-1, y-1) 134 || is_chess(x-1, y+2, 'H') && !has_chess(x-1, y+1) 135 || is_chess(x+1, y-2, 'H') && !has_chess(x+1, y-1) 136 || is_chess(x+1, y+2, 'H') && !has_chess(x-1, y+1)); 137 } 138 139 //判断能否被“炮”吃掉 140 bool cannon(int x, int y) 141 { 142 int tmp = has_left(x, y, 'C'); 143 if(tmp != 0 && cnt_chess_y(tmp, y, x) == 1) 144 return 1; 145 tmp = has_right(x, y, 'C'); 146 if(tmp != 0 && cnt_chess_y(tmp, y, x) == 1) 147 return 1; 148 tmp = has_up(x, y, 'C'); 149 if(tmp != 0 && cnt_chess_x(tmp, x, y) == 1) 150 return 1; 151 tmp = has_down(x, y, 'C'); 152 if(tmp != 0 && cnt_chess_x(tmp, x, y) == 1) 153 return 1; 154 return 0; 155 } 156 157 //判断是不是死棋 158 bool is_die(int x, int y) 159 { 160 if(x < 1 || x > 3 || y < 4 || y > 6) 161 return 1; 162 return (general(x, y) || chariot(x, y) || horse(x, y) || cannon(x, y)); 163 } 164 165 int main() 166 { 167 int n, xt, yt; 168 int x, y; 169 char s[2]; 170 while(scanf("%d %d %d", &n, &xt, &yt) != EOF && (n || xt || yt)) 171 { 172 memset(palace, 0, sizeof(palace)); 173 for(int i = 0; i < n; i++) 174 { 175 scanf("%s %d %d", s, &x, &y); 176 palace[x][y] = s[0]; 177 } 178 179 //如果“将”向周围走一步不死或者在原地不死,就说明不是死棋 180 //(话说不知道该不该判断原地,因为我没读懂题,不知道轮到谁走棋了。。。) 181 if(!is_die(xt, yt) || !is_die(xt+1, yt) || !is_die(xt-1, yt) || 182 !is_die(xt, yt+1) || !is_die(xt, yt-1)) 183 printf("NO\n"); 184 else 185 printf("YES\n"); 186 } 187 return 0; 188 }
View Code
转载于:https://www.cnblogs.com/wolfred7464/p/3457549.html
HDU 4121 Xiangqi 模拟题相关推荐
- HDU 4121 Xiangqi --模拟
题意: 给一个象棋局势,问黑棋是否死棋了,黑棋只有一个将,红棋可能有2~7个棋,分别可能是车,马,炮以及帅. 解法: 开始写法是对每个棋子,都处理处他能吃的地方,赋为-1,然后判断将能不能走到非-1的 ...
- HDU 4121 Xiangqi
题目链接 Problem Description Xiangqi is one of the most popular two-player board games in China. The gam ...
- HDU 4121 Xiangqi (算是模拟吧)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4121 题意:中国象棋对决,黑棋只有一个将,红棋有一个帅和不定个车 马 炮冰给定位置,这时当黑棋走,问你黑 ...
- HDU 4431 Mahjong(模拟题)
题目链接 写了俩小时+把....有一种情况写的时候漏了...代码还算清晰把,想了很久才开写的. 1 #include <cstdio> 2 #include <cstring> ...
- HDU 1262 寻找素数对 模拟题
题目描述:输入一个偶数,判断这个偶数可以由哪两个差值最小的素数相加,输出这两个素数. 题目分析:模拟题,注意的是为了提高效率,在逐个进行判断时,只要从2判断到n/2就可以了,并且最好用打表法判断素数. ...
- I'm stuck! ccf模拟题。
ccf模拟题. I'm stuck! 时间限制: 1.0s 内存限制: 256.0MB 问题描述 给定一个R行C列的地图,地图的每一个方格可能是'#', '+', '-', '|', '.', 'S' ...
- 重庆社区计算机考试题库,2020重庆社区工作者考试题库:模拟题100题(64)
2020年重庆社区工作者考试正在如火如荼的开展,为了帮助大家做好备考工作,社区工作者考试模拟题,希望考生们能与小编共同坚持--每日一练! 2020年社区工作者考试模拟题100题64 1. 在市场经济条 ...
- 计算机一级考试模拟题函数,2015年计算机一级考试模拟题(四)
2015年计算机一级考试模拟题(四) 请用Word 2003对考生文件夹下WORD.DOC文档中的文字进行编辑.排版和保存,具体要求如下: (1)将标题段("十年后的家电")文字设 ...
- java格林认证_Java考试格林模拟题
Java考试格林模拟题 question 14) which of the following lines of code will compile without error 1) int i=0; ...
最新文章
- QQ爬虫-爬取QQ空间
- R语言一次性读入多个csv文件实战:一次导入多个csv形成一个统一的dataframe、原生R方法、readr包、data.table
- Python3学习笔记:使用代理访问url地址
- python实时得到鼠标的位置
- arch linux网络配置,关于archlinux网络的 配置
- .NET平台PE结构分析之Metadata(一)
- CodeForces - 1484D Playlist(循环链表+bfs)
- VS2013配置编译Caffe-Win10_X64
- dm365 resize
- Hadoop自学笔记(三)MapReduce简单介绍
- CCKS 2019 | 百度 CTO 王海峰详解知识图谱与语义理解
- mysql大数据表无主键_oracle转mysql 表没有主键
- require.js的AMD规范详解
- MvvmCross框架在XamarinForms中的使用入门
- 不混淆 android jni,JNI 防混淆 Android proguard
- 勒让德方程(多项式)和缔合勒让德方程(多项式)和球谐函数
- Linux新手快速入门(万字超详细)
- 【C语言:丹尼斯·里奇的不朽遗产 】
- 软件测试思想者 - 再识王阳明
- Word支持的正则表达式
热门文章
- vue-cli3使用cdn引入
- es6 --- 模块
- dedecms后台怎么添加发布软件?织梦后台软件内容管理
- 基于.Net Framework 4.0 Web API开发(4):ASP.NET Web APIs 基于令牌TOKEN验证的实现
- 使用python matplotlib画图
- 主域控宕机无法恢复后,如何配置辅助域控继续工作
- 深山红叶PE工具箱嫦娥一号纪念版 V30[1115]
- C#枚举、值、字符串的相互转换
- HALCON示例程序color_pieces.hdev通过MLP训练器对彩色棋子进行分类识别
- python自带的shell、其性能优于ipython吗_Python自带的shell,其性能优于IPython