HDU 4121 Xiangqi --模拟
题意: 给一个象棋局势,问黑棋是否死棋了,黑棋只有一个将,红棋可能有2~7个棋,分别可能是车,马,炮以及帅。
解法: 开始写法是对每个棋子,都处理处他能吃的地方,赋为-1,然后判断将能不能走到非-1的点。但是WA了好久,也找不出反例,但就是觉得不行,因为可能有将吃子的情况,可能有hack点。但是比赛后还是被我调出来了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
#define N 1017 int chess[][],mp[][]; //0 : Non 1:G帅 2:R车 3:H Horse 4:C Cannon
int dx[] = {,-,,};
int dy[] = {,,-,};
bool InPalace(int nx,int ny) { if(nx >= && nx <= && ny >= && ny <= ) return true; return false; }
bool InChess(int nx,int ny) { if(nx >= && nx <= && ny >= && ny <= ) return true; return false;} int main()
{
int n,X,Y,i,j,k;
int x,y;
char ss[];
while(scanf("%d%d%d",&n,&X,&Y)!=EOF && (n+X+Y))
{
memset(chess,,sizeof(chess));
memset(mp,,sizeof(mp));
for(i=;i<=n;i++)
{
//0 : Non 1:G帅 2:R车 3:H Horse 4:C Cannon
scanf("%s%d%d",ss,&x,&y);
if(ss[] == 'G') chess[x][y] = ;
else if(ss[] == 'R') chess[x][y] = ;
else if(ss[] == 'H') chess[x][y] = ;
else if(ss[] == 'C') chess[x][y] = ;
}
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
if(chess[i][j] == ) // shuai
{
for(k=i-;k>=;k--)
{
if(chess[k][j] != )
{
mp[k][j] = -;
break;
}
if(k <= && chess[k][j] == ) mp[k][j] = -;
}
}
else if(chess[i][j] == ) // R che
{
for(int D=;D<;D++)
{
int kx = i, ky = j;
while()
{
kx = kx + dx[D];
ky = ky + dy[D];
if(!InChess(kx,ky)) break;
if(chess[kx][ky] == ) mp[kx][ky] = -;
else
{
mp[kx][ky] = -;
break;
}
}
}
}
else if(chess[i][j] == ) //Horse
{
if(InChess(i-,j) && chess[i-][j] == && (i- != X || j != Y)) //UP not blocked
{
if(InChess(i-,j-)) mp[i-][j-] = -;
if(InChess(i-,j+)) mp[i-][j+] = -;
}
if(InChess(i+,j) && chess[i+][j] == && (i+ != X || j != Y)) //DOWN not blocked
{
if(InChess(i+,j-)) mp[i+][j-] = -;
if(InChess(i+,j+)) mp[i+][j+] = -;
}
if(InChess(i,j+) && chess[i][j+] == && (i != X || j+ != Y)) //RIGHT not blocked
{
if(InChess(i-,j+)) mp[i-][j+] = -;
if(InChess(i+,j+)) mp[i+][j+] = -;
}
if(InChess(i,j-) && chess[i][j-] == && (i != X || j- != Y)) //LEFT not blocked
{
if(InChess(i-,j-)) mp[i-][j-] = -;
if(InChess(i+,j-)) mp[i+][j-] = -;
}
}
else if(chess[i][j] == ) //Cannon pao
{
for(int D=;D<;D++)
{
int kx = i, ky = j;
int cnt = ;
while()
{
kx = kx + dx[D];
ky = ky + dy[D];
if(!InChess(kx,ky)) break;
if(cnt == && chess[kx][ky] == ) mp[kx][ky] = -;
if(chess[kx][ky] != )
{
if(cnt == ) cnt++;
else if(cnt == )
{
mp[kx][ky] = -;
break;
}
}
}
}
}
}
}
int tag = ;
for(k=;k<;k++)
{
int kx = X + dx[k];
int ky = Y + dy[k];
if(!InChess(kx,ky) || !InPalace(kx,ky)) continue;
if(mp[kx][ky] != -) { tag = ; break; } //可以走
}
if(tag) puts("NO");
else puts("YES");
}
return ;
}
比赛中后来换了一种写法,枚举将能走的四个位置,然后判断此时是否能被吃掉。 如果没有一个安全的地方,那么就死棋了。 这种写起来就好些多了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
#define N 1017 int chess[][],mp[][]; //0 : Non 1:G帅 2:R车 3:H Horse 4:C Cannon
int dx[] = {,-,,};
int dy[] = {,,-,}; bool InPalace(int nx,int ny)
{
if(nx >= && nx <= && ny >= && ny <= ) return true;
return false;
} bool InChess(int nx,int ny)
{
if(nx >= && nx <= && ny >= && ny <= ) return true;
return false;
} struct node{
int x,y,type;
}q[]; bool Check(int X,int Y,int i,int j,int type) //将死则 return false !
{
int k;
if(type == ) //帅
{
if(j != Y) return true; //不是一行
for(k=i-;k>X;k--)
{
if(chess[k][j] != )
break;
}
if(k == X) return false; //老倌子见面,gg
return true;
}
else if(type == ) //R
{
int tag = ;
for(int D=;D<;D++)
{
int kx = i, ky = j;
while()
{
kx = kx + dx[D];
ky = ky + dy[D];
if(!InChess(kx,ky)) break;
if(kx == X && ky == Y) //车能碰到将
{
tag = ;
break;
}
if(chess[kx][ky] != )
break;
}
}
if(!tag) return false;
return true;
}
else if(type == ) //Horse
{
int tag = ;
if(InChess(i-,j) && chess[i-][j] == && (i- != X && j != Y)) //UP not blocked
{
if(InChess(i-,j-) && i- == X && j- == Y)
tag = ;
if(InChess(i-,j+) && i- == X && j+ == Y)
tag = ;
}
if(InChess(i+,j) && chess[i+][j] == && (i+ != X && j != Y)) //DOWN not blocked
{
if(InChess(i+,j-) && i+ == X && j- == Y)
tag = ;
if(InChess(i+,j+) && i+ == X && j+ == Y)
tag = ;
}
if(InChess(i,j+) && chess[i][j+] == && (i != X && j+ != Y)) //RIGHT not blocked
{
if(InChess(i-,j+) && i- == X && j+ == Y)
tag = ;
if(InChess(i+,j+) && i+ == X && j+ == Y)
tag = ;
}
if(InChess(i,j-) && chess[i][j-] == && (i != X && j- != Y)) //LEFT not blocked
{
if(InChess(i-,j-) && i- == X && j- == Y)
tag = ;
if(InChess(i+,j-) && i+ == X && j- == Y)
tag = ;
}
if(!tag) return false;
return true;
}
else if(type == ) //Cannon
{
int tag = ;
for(int D=;D<;D++)
{
int kx = i, ky = j;
int cnt = ;
while()
{
kx = kx + dx[D];
ky = ky + dy[D];
if(!InChess(kx,ky)) break;
if(cnt == && kx == X && ky == Y)
{
tag = ;
break;
}
if(chess[kx][ky] != )
cnt++;
}
}
if(!tag) return false;
return true;
}
return false;
} int main()
{
int n,X,Y,i,j,k;
int x,y;
char ss[];
while(scanf("%d%d%d",&n,&X,&Y)!=EOF && (n+X+Y))
{
memset(chess,,sizeof(chess));
int tot = ;
for(i=;i<=n;i++)
{
//0 : Non 1:G帅 2:R车 3:H Horse 4:C Cannon
scanf("%s %d%d",ss,&x,&y);
if(ss[] == 'G')
chess[x][y] = ;
else if(ss[] == 'R')
chess[x][y] = ;
else if(ss[] == 'H')
chess[x][y] = ;
else if(ss[] == 'C')
chess[x][y] = ;
node now;
now.x = x, now.y = y, now.type = chess[x][y];
q[++tot] = now;
}
int flag[];
int tag = ;
for(k=;k<;k++)
{
int nx = X + dx[k];
int ny = Y + dy[k];
memset(flag,,sizeof(flag));
if(!InChess(nx,ny) || !InPalace(nx,ny)) continue;
for(i=;i<=tot;i++)
{
if(nx == q[i].x && ny == q[i].y)
{
flag[i] = ;
chess[nx][ny] = ;
}
}
int smalltag = ;
for(i=;i<=tot;i++)
{
if(flag[i]) continue;
bool res = Check(nx,ny,q[i].x,q[i].y,q[i].type);
if(!res) { smalltag = ; break; }
}
if(smalltag)
{
tag = ;
break;
}
// 还原
for(i=;i<=tot;i++)
{
if(flag[i])
chess[q[i].x][q[i].y] = q[i].type;
}
}
if(tag) puts("NO");
else puts("YES");
}
return ;
}
HDU 4121 Xiangqi --模拟的更多相关文章
- HDU 4121 Xiangqi 模拟题
Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...
- HDU 4121 Xiangqi (算是模拟吧)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4121 题意:中国象棋对决,黑棋只有一个将,红棋有一个帅和不定个车 马 炮冰给定位置,这时当黑棋走,问你黑 ...
- HDU 4121 Xiangqi 我老了?
Xiangqi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 4121 Xiangqi
模拟吧,算是... 被这个题wa到哭,真是什么都不想说了...上代码 #include <iostream> #include <cstring> using namespac ...
- hdu 5071 Chat(模拟)
题目链接:hdu 5071 Chat 题目大意:模拟题. .. 注意最后说bye的时候仅仅要和讲过话的妹子说再见. 解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子.数组A ...
- hdu 4740【模拟+深搜】.cpp
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
- HDU 2568[前进]模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2568 关键思想:傻傻地模拟 代码如下: #include<iostream> using ...
- hdu 4964 恶心模拟
http://acm.hdu.edu.cn/showproblem.php?pid=4964 给定语句,按照语法翻译html并输出. 就是恶心的模拟,递归搞就行了 处理id和class时,在一个'&g ...
- TZOJ 4746 Xiangqi(模拟棋盘数组)
描述 Xiangqi is one of the most popular two-player board games in China. The game represents a battle ...
随机推荐
- Durandal介绍
Durandal是一个JS框架用于构建客户端single page application(SPAs).它支持MVC,MVP与MVVM前端构架模式.使用RequireJS做为其基本约定层,D ...
- ubuntu定时执行脚本(crond)
如果发现您的系统里没有这个命令,请安装下面两个软件包. vixie-cron crontabs crontab 是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类似使用者的时程表.-u ...
- jQuery coveringBad 效果对比
Covering-Bad 是一个可拉动大小的元素,覆盖在原有的元素上面,从而两者进行对比. 在线实例 实例演示1 实例演示2 使用方法 <div class="covered&q ...
- jquery动态创建节点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- javascript --- 将共享属性迁移到原型中去
当我们用一个构造函数创建对象时,其属性就会被添加到this中去.并且被添加到this中的属性实际上不会随着实体发生改变,这时,我们这种做法显得会很没有效率.例如: function her(){ th ...
- CSS 选择器汇总
CSS 选择器 CSS 元素选择器 CSS 选择器分组 CSS 类选择器详解 CSS ID 选择器详解 CSS 属性选择器详解 CSS 后代选择器 CSS 子元素选择器 CSS 相邻兄弟选择器 CSS ...
- SharePoint 2013 自定义模板页后在列表里修改不了视图
前言 最近系统从2010升级至2013,有自定义模板页.突然发现在列表中切换不了视图,让我很费解. 我尝试过以下解决方案: 去掉自定义css 去掉自定义js 禁用所有自定义功能 结果都没有效还是一样的 ...
- GitHub 基本常用知识解答
1.Fork.Watch.Star 是什么意思? fork的意思是从别人的代码库中复制一份到你自己的代码库,与普通的复制不同,fork包含了原有库中的所有提交记录, fork后这个代码库是完全独立的, ...
- C语言的判断语句
// // main.c // homeWork1222 //// #include <stdio.h> int main(int argc, const char * argv[]) { ...
- Mac OS X上安装 Ruby运行环境
环境 对于新入门的开发者,如何安装 Ruby和Ruby Gems 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 Ruby 开发环境.此安装方法同样适用于产品环境! 系统需求 ...