模拟题考验coding能力,一定要思路清晰,按照模块化思想,有哪些情况,需要哪些功能都要事先分析好了。高手的模拟题代码往往结构很清晰,功能模块写成函数,没有过多重复代码,让人一看便明。

方法选择的好坏会影响编程复杂度,这题老将最多只能往四个位置走,就枚举这四个位置,每个位置再枚举每个红子看是不是有子能吃了它。枚举时注意有可能老将这一步吃了一个红子。

有一种情况是一开始双方老将就见面,这其实不叫红棋在将军,实际中红棋不能这么将。但是毕竟是一道编程题,出题人可能也不是太懂棋。。。所以要考虑这种情况。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<cmath>
  7. #include<map>
  8. #include<set>
  9. #include<vector>
  10. #include<algorithm>
  11. #include<stack>
  12. #include<queue>
  13. #include<cctype>
  14. #include<sstream>
  15. using namespace std;
  16. #define pii pair<int,int>
  17. #define LL long long int
  18. const int eps=1e-;
  19. const int INF=;
  20. const int maxn=+;
  21. int n,X,Y;
  22. int maps[][];
  23. int dx[]= {,-,,};
  24. int dy[]= {,,,-};
  25. struct Red
  26. {
  27. char type;
  28. int x,y;
  29. } red[];
  30. bool can_eat(int r,int a,int b)
  31. {
  32. int x=red[r].x;
  33. int y=red[r].y;
  34. if(x==a&&y==b) return false;//老将吃掉了这个子
  35. else if(red[r].type=='G')
  36. {
  37. if(y!=b) return false;//老将不碰面
  38. for(int i=min(a,x)+; i<max(a,x); i++)
  39. {
  40. if(maps[i][b]) return false;//中间隔了子,老将不能碰面
  41. }
  42. return true;
  43. }
  44. else if(red[r].type=='R')
  45. {
  46. if((x!=a)&&(y!=b)) return false;
  47. else if(x==a)
  48. {
  49. for(int i=min(y,b)+; i<max(y,b); i++)
  50. {
  51. if(maps[x][i]) return false;
  52. }
  53. return true;
  54. }
  55. else if(y==b)
  56. {
  57. for(int i=min(x,a)+; i<max(x,a); i++)
  58. {
  59. if(maps[i][y]) return false;
  60. }
  61. return true;
  62. }
  63. }
  64. else if(red[r].type=='C')
  65. {
  66. if((x!=a)&&(y!=b)) return false;
  67. else if(x==a)
  68. {
  69. int num=;
  70. for(int i=min(y,b)+; i<max(y,b); i++)
  71. {
  72. if(maps[x][i]) num++;
  73. }
  74. if(num==) return true;
  75. return false;
  76. }
  77. else if(y==b)
  78. {
  79. int num=;
  80. for(int i=min(x,a)+; i<max(x,a); i++)
  81. {
  82. if(maps[i][y]) num++;
  83. }
  84. if(num==) return true;
  85. return false;
  86. }
  87. }
  88. else //red[r].type=='H'
  89. {
  90. if(x+==a&&y+==b&&maps[x+][y]=='\0') return true;
  91. if(x+==a&&y+==b&&maps[x][y+]=='\0') return true;
  92. if(x-==a&&y+==b&&maps[x-][y]=='\0') return true;
  93. if(x-==a&&y+==b&&maps[x][y+]=='\0') return true;
  94. if(x-==a&&y-==b&&maps[x-][y]=='\0') return true;
  95. if(x-==a&&y-==b&&maps[x][y-]=='\0') return true;
  96. if(x+==a&&y-==b&&maps[x+][y]=='\0') return true;
  97. if(x+==a&&y-==b&&maps[x][y-]=='\0') return true;
  98. return false;
  99. }
  100. }
  101. bool pan();
  102. int main()
  103. {
  104. //freopen("in8.txt","r",stdin);
  105. //freopen("out.txt","w",stdout);
  106. while(cin>>n>>X>>Y)
  107. {
  108. if(n==&&X==&&Y==) break;
  109. int num,tx,ty;
  110. memset(maps,,sizeof(maps));
  111. bool ans=true;
  112. for(num=; num<n; num++)
  113. {
  114. cin>>red[num].type>>red[num].x>>red[num].y;
  115. maps[red[num].x][red[num].y]=;
  116. }
  117. if(pan())
  118. {
  119. puts("NO");
  120. continue;
  121. }
  122. for(int i=; i<; i++)
  123. {
  124. tx=X+dx[i];
  125. ty=Y+dy[i];
  126. if(tx>=&&tx<=&&ty>=&&ty<=)//该步移动合法
  127. {
  128. bool capture=false;
  129. for(int j=; j<n; j++) //检查现在有没有红子能吃黑将
  130. {
  131. if(can_eat(j,tx,ty))
  132. {
  133. capture=true;
  134. break;
  135. }
  136. }
  137. if(capture==true) continue;
  138. else
  139. {
  140. ans=false;
  141. break;
  142. }
  143. }
  144. else continue;
  145. }
  146. if(ans==false) printf("NO\n");
  147. else printf("YES\n");
  148. }
  149. //fclose(stdin);
  150. //fclose(stdout);
  151. return ;
  152. }
  153. bool pan()
  154. {
  155. for(int i=; i<n; i++)
  156. {
  157. if(red[i].type=='G')
  158. {
  159. if(Y==red[i].y)
  160. {
  161. for(int j=min(red[i].x,X)+; j<max(red[i].x,X); j++)
  162. {
  163. if(maps[j][Y])
  164. {
  165. return false;
  166. }
  167. }
  168. }
  169. else
  170. {
  171. return false;
  172. }
  173. }
  174. }
  175. return true;
  176. }

hdu4121 poj4001 Xiangqi(模拟)的更多相关文章

  1. HDU 4121 Xiangqi 模拟题

    Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...

  2. TZOJ 4746 Xiangqi(模拟棋盘数组)

    描述 Xiangqi is one of the most popular two-player board games in China. The game represents a battle ...

  3. HDU 4121 Xiangqi --模拟

    题意: 给一个象棋局势,问黑棋是否死棋了,黑棋只有一个将,红棋可能有2~7个棋,分别可能是车,马,炮以及帅. 解法: 开始写法是对每个棋子,都处理处他能吃的地方,赋为-1,然后判断将能不能走到非-1的 ...

  4. UVA 1589:Xiangqi (模拟 Grade D)

    题目: 象棋,黑棋只有将,红棋有帅车马炮.问是否死将. 思路: 对方将四个方向走一步,看看会不会被吃. 代码: 很难看……WA了很多发,还越界等等. #include <cstdio> # ...

  5. dir命令只显示文件名

    dir /b 就是ls -f的效果 1057 -- FILE MAPPING_web_archive.7z 2007 多校模拟 - Google Search_web_archive.7z 2083 ...

  6. Xiangqi(简单模拟)

    4746: Xiangqi 时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte 总提交: 15            测试通过:2 描述 Xiangqi i ...

  7. BFS、模拟:UVa1589/POJ4001/hdu4121-Xiangqi

    Xiangqi Xiangqi is one of the most popular two-player board games in China. The game represents a ba ...

  8. ●UVa 1589 Xiangqi(模拟)

    ●赘述题意 给出一个中国象棋残局,告诉各个棋子的位置,黑方只有1枚“将”,红方有至少2枚,至多7枚棋子,包含1枚“帅G”,和若干枚“车R”,“马H”,“炮C”.当前为黑方的回合,问黑方的“将”能否在移 ...

  9. HDU 4121 Xiangqi (算是模拟吧)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4121 题意:中国象棋对决,黑棋只有一个将,红棋有一个帅和不定个车 马 炮冰给定位置,这时当黑棋走,问你黑 ...

随机推荐

  1. celery的安装和使用

    celery是python开发的分布式任务调度模块,接口简单,开发容易,五分钟就写出一个异步发送邮件的服务,celery本身不含消息服务,它使用第三方消息服务来传递任务,目前,celery支持的消息服 ...

  2. Django_Form表单补充

    Form表单 问题1:  注册页面输入为空,报错:keyError:找不到password def clean(self): print("---",self.cleaned_da ...

  3. iOS oc 调用 swift

    如股票oc要调用swift里面的代码 需要包含固定这个头文件 项目名称 LiqunSwiftDemo-Swift.h #ProjectName#-Swift.h 固定的写法 swift 目的 是取代o ...

  4. Java底层代码实现单文件读取和写入(解决中文乱码问题)

    需求: 将"E:/data/车站一次/阿坝藏族羌族自治州.csv"文件中的内容读取,写入到"E:/data//车站一次.csv". 代码: public cla ...

  5. Linux Shell编程 awk命令

    概述 awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入(stdin).一个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是l ...

  6. imx6qsbd lvds dtc

    lvds显示屏调试参考 1.基于飞思卡尔imxsolosabresd开发板Linux-3.10.53 lvds屏幕调试: http://blog.csdn.net/qq_37375427/articl ...

  7. Docker容器技术-镜像分发

    一.镜像分发 1.镜像及镜像库的命名方式 指定镜像名称和标签的方法: 在狗偶见镜像时 通过docker tag命令 [root@bogon ~]# cd identidock/ [root@bogon ...

  8. debian内核代码执行流程(二)

    继续上一篇文章<debian内核代码执行流程(一)>未完成部分. acpi_bus_init调用acpi_initialize_objects,经过一系列复杂调用后输出下面信息: [ IN ...

  9. linux 虚拟机在线添加新磁盘

    在线添加磁盘,扩展LVM卷案例   一.添加硬盘,在线扫描出来 首先到虚拟机那里添加一块硬盘,注意必须是SCSI类型的硬盘. 扫描硬盘,不用重启操作系统的. echo "- - -" ...

  10. Yii框架和Vue的完美结合完成前后端分离项目

    背景说明 本文假设你对Yii和Vue都比较熟悉,至少都在项目里用过,另外笔者新人,以后不定时放一些干货,欢迎程序媛关注 Yii是一个PHP全端框架,典型的mvc的项目结构,后端接口都是一个控制器里放了 ...