题意: 给一个象棋局势,问黑棋是否死棋了,黑棋只有一个将,红棋可能有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 --模拟的更多相关文章

  1. HDU 4121 Xiangqi 模拟题

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

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

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

  3. HDU 4121 Xiangqi 我老了?

    Xiangqi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. HDU 4121 Xiangqi

    模拟吧,算是... 被这个题wa到哭,真是什么都不想说了...上代码 #include <iostream> #include <cstring> using namespac ...

  5. hdu 5071 Chat(模拟)

    题目链接:hdu 5071 Chat 题目大意:模拟题. .. 注意最后说bye的时候仅仅要和讲过话的妹子说再见. 解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子.数组A ...

  6. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

  7. HDU 2568[前进]模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2568 关键思想:傻傻地模拟 代码如下: #include<iostream> using ...

  8. hdu 4964 恶心模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=4964 给定语句,按照语法翻译html并输出. 就是恶心的模拟,递归搞就行了 处理id和class时,在一个'&g ...

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

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

随机推荐

  1. Linux服务器时间同步方法

    一般稍微大点的项目都会部署到好几台服务器做集群,同一个应用可能部署到几台服务器上,而处理业务中必须让不同的服务器上时间保持一致,这就需要进行服务器间的时间同步.我的做法是: 1,选择其中一台对外网开放 ...

  2. Java调用JavaScript

    1.main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  3. 最近提交并被合并的 jDiameter pull request 31 解决的问题

    使用过程中发现的问题都提交并合并了,应该会出现在1.7.0版本中: https://github.com/RestComm/jdiameter/pull/31 修复多个超时事件同时发生的问题. 修复B ...

  4. ABAP->内表数据下载到CSV格式(原创转载请注明)

    需求:将alv上面的数据计算到内表中区,然后通过自定义按钮进行下载到csv格式中 附加:现在基本不用csv导出了,但是有些变态需求强行要求,也只好研究出来了,excel与txt导出很简单,那就不多说了 ...

  5. jekyll

    bundle show minima查看安装路径 bundle exec github-pages versions 建立一个类似于master的分支,与master是完全独立 git checkou ...

  6. gridView使用

    只读 for (int i = 0; i <9; i++) { this.gridView1.Columns[i].OptionsColumn.ReadOnly = true; } 不显示面板 ...

  7. 【转】提高C#编程水平的50个要点

    1.总是用属性 (Property) 来代替可访问的数据成员2.在 readonly 和 const 之间,优先使用 readonly3.在 as 和 强制类型转换之间,优先使用 as 操作符4.使用 ...

  8. Java从零开始学四十七(注解简述)

    一.Java中注解Annotation 什么是注解:用来描述数据的数据(元数据). Java代码里的特殊标记.它为代码中添加用Java程序无法表达的额外信息提供一种形式化的方法,使用我们可以在未来的某 ...

  9. Android源码分析之AsyncTask

    AsyncTask相信从事Android开发的同学都不陌生,基本都应该用到了,和以前一样我们还是先来看看此类的summary.AsyncTask 可以确保更合理.容易的使用UI线程.这个类是设计用来执 ...

  10. GCD中的dispatch_semaphore的语法与作用

    (一)引入问题 当并行执行的处理更新数据时,会产生数据不一致的情况,有时应用程序还会异常结束,虽然使用Serial Dipatch queue和dispatch_barrier_async函数可避免这 ...