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

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

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

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-;
const int INF=;
const int maxn=+;
int n,X,Y;
int maps[][];
int dx[]= {,-,,};
int dy[]= {,,,-};
struct Red
{
char type;
int x,y;
} red[];
bool can_eat(int r,int a,int b)
{
int x=red[r].x;
int y=red[r].y;
if(x==a&&y==b) return false;//老将吃掉了这个子
else if(red[r].type=='G')
{
if(y!=b) return false;//老将不碰面
for(int i=min(a,x)+; i<max(a,x); i++)
{
if(maps[i][b]) return false;//中间隔了子,老将不能碰面
}
return true;
}
else if(red[r].type=='R')
{
if((x!=a)&&(y!=b)) return false;
else if(x==a)
{
for(int i=min(y,b)+; i<max(y,b); i++)
{
if(maps[x][i]) return false;
}
return true;
}
else if(y==b)
{
for(int i=min(x,a)+; i<max(x,a); i++)
{
if(maps[i][y]) return false;
}
return true;
}
}
else if(red[r].type=='C')
{
if((x!=a)&&(y!=b)) return false;
else if(x==a)
{
int num=;
for(int i=min(y,b)+; i<max(y,b); i++)
{
if(maps[x][i]) num++;
}
if(num==) return true;
return false;
}
else if(y==b)
{
int num=;
for(int i=min(x,a)+; i<max(x,a); i++)
{
if(maps[i][y]) num++;
}
if(num==) return true;
return false;
}
}
else //red[r].type=='H'
{
if(x+==a&&y+==b&&maps[x+][y]=='\0') return true;
if(x+==a&&y+==b&&maps[x][y+]=='\0') return true;
if(x-==a&&y+==b&&maps[x-][y]=='\0') return true;
if(x-==a&&y+==b&&maps[x][y+]=='\0') return true;
if(x-==a&&y-==b&&maps[x-][y]=='\0') return true;
if(x-==a&&y-==b&&maps[x][y-]=='\0') return true;
if(x+==a&&y-==b&&maps[x+][y]=='\0') return true;
if(x+==a&&y-==b&&maps[x][y-]=='\0') return true;
return false;
}
}
bool pan();
int main()
{
//freopen("in8.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(cin>>n>>X>>Y)
{
if(n==&&X==&&Y==) break;
int num,tx,ty;
memset(maps,,sizeof(maps));
bool ans=true;
for(num=; num<n; num++)
{
cin>>red[num].type>>red[num].x>>red[num].y;
maps[red[num].x][red[num].y]=;
}
if(pan())
{
puts("NO");
continue;
}
for(int i=; i<; i++)
{
tx=X+dx[i];
ty=Y+dy[i];
if(tx>=&&tx<=&&ty>=&&ty<=)//该步移动合法
{
bool capture=false;
for(int j=; j<n; j++) //检查现在有没有红子能吃黑将
{
if(can_eat(j,tx,ty))
{
capture=true;
break;
}
}
if(capture==true) continue;
else
{
ans=false;
break;
}
}
else continue;
}
if(ans==false) printf("NO\n");
else printf("YES\n");
}
//fclose(stdin);
//fclose(stdout);
return ;
}
bool pan()
{
for(int i=; i<n; i++)
{
if(red[i].type=='G')
{
if(Y==red[i].y)
{
for(int j=min(red[i].x,X)+; j<max(red[i].x,X); j++)
{
if(maps[j][Y])
{
return false;
}
}
}
else
{
return false;
}
}
}
return true;
}

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. Charles安装与使用

    Charles是在 Mac 下常用的网络封包截取工具,在做 移动开发时,我们为了调试与服务器端的网络通讯协议,常常需要截取网络封包来分析. Charles 通过将自己设置成系统的网络访问代理服务器,使 ...

  2. 请写一个python逻辑,计算一个文件中的大写字母数量

    import os os.chdir(r'C:\Users\Administrator\Desktop')#os.chdir切换到指定目录 with open('a.txt') as today: c ...

  3. Git配置出现的问题

    git是代码版本同步工具,适用于团队开发,进公司第一堂课就是配置Git.接下来就把其中遇到的问题记录一下,与大家共享一下. 首先,在Bitbucket上注册账户,之后给管理员说一下,让他邀请你加入开发 ...

  4. linux 快速清空文件内容

    Tomcat 的catelina.out 如果不配置按照日期产生会在一个文件中产生大量的输出日志. 清除日志如果直接删除catelina.out将无法输出日志.如果想输出日志只能重启Tomcat才会产 ...

  5. Linux sh远程连接失败 sshd.service启动失败

    今天不小心在本机的虚拟机执行了 chmod -R 777 /var 导致/var目录下所有权限全部是777 其中 /var/empty/sshd目录权限必须是744,却被改成了777,致使sh远程连接 ...

  6. android 7.0 (nougat)的编译优化-ninja

    http://blog.csdn.net/songjam/article/details/52640501 版权声明:本文为博主原创文章,未经博主允许不得转载. 从官方的定义,ninja大大缩短了an ...

  7. 解决:Requested 'libdrm_radeon >= 2.4.56' but version of libdrm_radeon is 2.4.52

    checking for NOUVEAU... yes checking for RADEON... no configure: error: Package requirements (libdrm ...

  8. Windows 7安装PHP运行环境和开发环境

    1. 安装Apache 下载地址:http://www.apache.org/dyn/closer.cgi/httpd/binaries/win32 如需更改端口:打开Apache安装目录下conf目 ...

  9. 【bzoj1876】[SDOI2009]SuperGCD(高精度)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1876 一道简单的高精度+Stein算法(或者叫辗转相除法)求最大公约数板子题. md还 ...

  10. 1.java实现——正规表达式判断

    目标:这个代码仅局限于所展示的正规表达式判断,也就是这是一个较单一的正规表达式判断(简易版). 既然是简易版的,所以若要修改这个正规表达式也是非常容易的,只要将二维数组中的数组修改即可.数组数据依据, ...