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 ...
随机推荐
- 非线性数据拟合-nls
code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...
- django性能优化
1. 内存.内存,还是加内存 2. 使用单独的静态文件服务器 3. 关闭KeepAlive(如果服务器不提供静态文件服务,如:大文件下载) 4. 使用memcached 5. 使用select_rel ...
- AX2012 R3 Data upgrade checklist sync database step, failed to create a session;
最近在做AX2012 R3 CU9 到CU11的upgrade时 (用的Admin帐号), 在Date upgrade 的 synchronize database 这步 跑了一半,报出错误 说“fa ...
- R语言学习笔记:向量
向量是R语言最基本的数据类型. 单个数值(标量)其实没有单独的数据类型,它只不过是只有一个元素的向量. x <- c(1, 2, 4, 9) x <- c(x[1:3], 88, x[4] ...
- 弃用的同步get和post请求
#import "ViewController.h" #import "Header.h" @interface ViewController () <N ...
- Git使用之设置SSH Key
设置SSH Key 1. 检查是否已经有SSH Key. $cd ~/.ssh 如果说没有这个目录,就直接看第三步 2. 备份 3. 生成一个新的SSH. $ssh-keygen - ...
- ShellExecuteA()&MessageBoxA()
#include<windows.h> #include<stdlib.h> void main() { ) { ShellExecuteA(, , , ); //0代表系统启 ...
- javascript 的默认对象
一.日期对象 格式 : 日期对象名称=new Date([日期参数]) 日期参数: 1.省略(最常用) 2.英文-参数格式 ...
- javascript简介和基本语法
javascript简介 1.javascript是个脚本语言,需要有宿主文件,他的宿主文件是html文件. 用法:为了保险起见一般写在</html>之后<javascript ...
- Angular动态注册组件(controller,service...)
使用angular的场景一般是应用类网站 这也意味着会有很多的controller,service,directive等等 正常情况下我们要把这些内容一次性下载并注册,由于文件较多,对首次加载的效率影 ...