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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/273917.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql在cmd命令行下的相关操作

1、设置新的root密码。 mysql -u root -p 直接回车&#xff0c;无需输入密码就可以进入数据库了。 此时在命令行下执行 use mysql &#xff08;切换到系统数据库&#xff09; 执行以下语句既可修改root用户密码&#xff1a; update user set passwordPASSWORD("123456…

python 多继承的问题

&#xff08;&#xff11;&#xff09;、关于Python支持多继承&#xff0c;如果父类中有相同的方法名&#xff0c;而在子类中调用时没有指定父类名&#xff0c;则Python解释器将从左向右按顺序进行搜索。 例如&#xff1a; class B():def a(self):print("this is B"…

python 常看

(1)、单链表的翻转 参考&#xff1a; https://www.cnblogs.com/mafeng/p/7149980.html 参考代码&#xff1a; def reverse_linkedlist2(head): if head None or head.next None: #边界条件 return head cur head #循环变量 tmp None #保存数据的临时变量 newhead None…

Android中的音频播放(MediaPlayer和SoundPool)

Android中音频和视频的播放我们最先想到的就是MediaPlayer类了&#xff0c;该类提供了播放、暂停、停止、和重复播放等方法。该类位于android.media包下&#xff0c;详见API文档。其实除了这个类还有一个音乐播放类那就是SoundPool&#xff0c;这两个类各有不同分析一下便于大家…

python中的静态方法和类方法

一、先看语法&#xff0c;python 类语法中有三种方法&#xff0c;实例方法&#xff0c;静态方法&#xff0c;类方法。 普通实例方法&#xff0c;第一个参数需要是self&#xff0c;它表示一个具体的实例本身。 如果用了staticmethod&#xff0c;那么就可以无视这个self&#xf…

我所遭遇过的中间件--VTK

我所遭遇过的中间件--VTK Vtk是我接触的第一款软件开发包,它引导我对图形学的入门.我是先学的VTK,后学的OpenGL和D3D.VTK是专为图形学开发,特点是接口清晰,好上手,又含有大量的图像处理算法.从VTK入手3D图形学,要比从OpenGL和D3D容易的多. 最初接触VTK是研一那年暑假,研一时我做…

java.lang.OutOfMemoryError: PermGen space 问题解决

Tomcat/bin/catalina.bat 或 .sh 文件中的“rem ----- Execute The Requested Command -”这个后面增加了下面的语句set JAVA_OPTS%JAVA_OPTS% -Xms256m -Xmx1024m -XX:PermSize256M -XX:MaxNewSize256m -XX:MaxPermSize512m Myeclipse配置选项 打开选项..输入tomcat关键字,然…

python中的__new__和__init__

一、__init__ 方法是什么&#xff1f; 使用Python写过面向对象的代码的同学&#xff0c;可能对 __init__ 方法已经非常熟悉了&#xff0c;__init__ 方法通常用在初始化一个类实例的时候。 这样便是__init__最普通的用法了。 但__init__其实不是实例化一个类的时候第一个被调…

python 中的__getattr__和__setattr__

__getattr__为内置方法&#xff0c;当使用点号获取实例属性时&#xff0c;如果属性不存在就自动调用__getattr__方法 __setattr__当设置类实例属性时自动调用&#xff0c;如j.name5 就会调用__setattr__方法 self.[name]5 因为这个类是从dict继承来的&#xff0c;是dict的超类 …

correct ways to define variables in python

http://stackoverflow.com/questions/9056957/correct-way-to-define-class-variables-in-python later say this转载于:https://www.cnblogs.com/luomingchuan/p/3475268.html

python 的钻石继承问题

如果子类继承自两个单独的超类&#xff0c;而那两个超类又继承自同一个公共基类&#xff0c;那么就构成了钻石继承体系。这种继承体系很像竖立的菱形&#xff0c;也称作菱形继承。 class Base:def __init__(self, value):print("This is Base __init__")self.value …

认知http响应头

HTTP&#xff08;HyperTextTransferProtocol&#xff09;是超文本传输协议的缩写&#xff0c;它用于传送WWW方式的数据&#xff0c;关于HTTP协议的详细内 容请参考RFC2616。HTTP协议采用了请求/响应模型。客户端向服务器发送一个请求&#xff0c;请求头包含请求的方法、URI、协…

Python3的方法解析顺序(MRO)

Python 2.3 的新式类的 C3 算法。它也是 Python 3 唯一支持的方式(笔者使用python3&#xff0c;所以就先讲这种的) 一个例子&#xff1a; class D(object): pass class E(object): pass class F(object): pass class C(D, F): pass class B(E, D): …

WPF 用 DataTemplate 合并DataGrid列表列头类似报表设计及行头列头样式 - 学习

WPF中 DataGrid 列头合并&#xff0c;类似于报表设计。效果图如下↓ 1.新建一个WPF项目WpfApplication1&#xff0c;新建一个窗体DataGridTest&#xff0c;前台代码如下&#xff1a; <Window x:Class"WpfApplication1.DataGridTest" xmlns"http://sch…

python 中的pickle库

序列化&#xff1a;我们把变量从内存中变成可存储或传输的过程称之为序列化&#xff0c;在Python中叫pickling&#xff0c;在其他语言中也被称之为serialization&#xff0c;marshalling&#xff0c;flattening等等&#xff0c;都是一个意思。 序列化之后&#xff0c;就可以把…

他山之石,可以攻玉——来自亚马逊的电商启示录

题记&#xff1a;“创新是我们的DNA&#xff0c;技术是我们改善客户体验的基础2009 年致股东的信” 1. 从亚马逊的成功讲起 1.1 历经8 年亏损始成正果 它是世界上所有电商的龙头和楷模&#xff0c;是毫无争议的行业标杆和旗帜&#xff0c;它在战略和经营上的一举一动都是关注的…

python数据结构-栈和队列的实现

&#xff11;、栈&#xff08;后进先出(last in first out&#xff0c;LIFO)&#xff09; 栈是一种特殊的列表&#xff0c;栈内的元素只能通过列表的一端访问&#xff0c;这一端称为栈顶。栈被称为一种后入先出&#xff08;LIFO&#xff0c;last-in-first-out&#xff09;的数…

c#只读字段和常量的区别,以及静态构造函数的使用 .

using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleApplication1{ /// <summary> /// 功能&#xff1a;c#只读字段和常量的区别&#xff0c;以及静态构造函数的使用 /// </summary> class Progra…

python中的json序列化

如果我们要在不同的编程语言之间传递对象&#xff0c;就必须把对象序列化为标准格式&#xff0c;比如XML&#xff0c;但更好的方法是序列化为JSON&#xff0c;因为JSON表示出来就是一个字符串&#xff0c;可以被所有语言读取&#xff0c;也可以方便地存储到磁盘或者通过网络传输…