开始碰到这个题时觉得太麻烦了直接跳过没做,现在放假了再次看这个题发现没有想象中那么麻烦,主要是题目理解要透彻,基本思路就是用结构体数组存下红方棋子,让黑将军每次移动一下,看移动后是否有一个红方棋子可以吃掉它,这样做黑将军吃子这一情况就可以完美的跳过了,因为只看不在黑将军移动后位置的棋子是否能吃掉他。二维数组模拟棋盘,注意皇宫,玩过象棋的应该对规则比较熟悉,注意憋马脚的情况,不懂得可以看看中国象棋的规则。下面附上我的AC代码,主要是输入那块很烦,只是用getchar()读掉一个回车的话本地测试是对的,交上去却会出现问题,只好用字符串读,然后取首字母。

AC代码:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h> struct qi{
char name;
int x, y;
}hei, qizi[];
int qipan[][], n;
int judge();
int deal(int a, int b);
int H(int, int, int, int);
int C(int, int, int, int);
int R(int, int, int, int);
int main()
{
char s[];
while(scanf("%d %d %d", &n, &hei.x, &hei.y) == && n && hei.x && hei.y)
{
memset(qipan,,sizeof(qipan));
int i;
for(i = ; i < n; i++)
{
scanf("%s %d %d", s, &qizi[i].x, &qizi[i].y);
qizi[i].name = s[];
qipan[qizi[i].x][qizi[i].y] = qizi[i].name;
}
printf("%s\n", judge() ? "YES" : "NO");
}
return ;
} int judge()
{
int i;
for(i = hei.x; i <= ; i++)
{
if(qipan[i][hei.y] != )
{
if(qipan[i][hei.y] == 'G') return ;
else break;
}
}
if(hei.x > && deal(hei.x - , hei.y)) return ;
else if(hei.x < && deal(hei.x + , hei.y)) return ;
else if(hei.y > && deal(hei.x, hei.y - )) return ;
else if(hei.y < && deal(hei.x, hei.y + )) return ;
return ;
} int deal(int a, int b)
{
int i;
for(i = ; i < n; i++)
{
if(qizi[i].x == a && qizi[i].y == b)
continue;
if(qizi[i].name == 'R' || qizi[i].name == 'G')
{
if(R(qizi[i].x, qizi[i].y, a, b))
return ;
}
else if(qizi[i].name == 'C')
{
if(C(qizi[i].x, qizi[i].y, a, b))
return ;
}
else if(qizi[i].name == 'H')
{
if(H(qizi[i].x, qizi[i].y, a, b))
return ;
}
}
return ;
} int H(int x, int y, int a, int b)
{
if(qipan[x + ][y] == && x + == a && (y - == b || y + == b))
return ;
if(qipan[x - ][y] == && x - == a && (y - == b || y + == b))
return ;
if(qipan[x][y + ] == && y + == b && (x - == a || x + == a))
return ;
if(qipan[x][y - ] == && y - == b && (x - == a || x + == a))
return ;
return ;
} int C(int x, int y, int a, int b)
{
int i;
if(x == a)
{
int min = y > b ? b : y;
int max = y > b ? y : b;
int num = ;
for(i = min + ; i < max; i++)
if(qipan[x][i])
num++;
if(num == )
return ;
return ;
}
if(y == b)
{
int min = x > a ? a : x;
int max = x > a ? x : a;
int num = ;
for(i = min + ; i < max; i++)
if(qipan[i][y])
num++;
if(num == )
return ;
return ;
}
return ;
} int R(int x, int y, int a, int b)
{
int i;
if(x == a)
{
int min = y > b ? b : y;
int max = y > b ? y : b;
for(i = min + ; i < max; i++)
if(qipan[x][i] != )
return ;
return ;
}
if(y == b)
{
int min = x > a ? a : x;
int max = x > a ? x : a;
for(i = min + ; i < max; i++)
if(qipan[i][y] != )
return ;
return ;
}
return ;
}

UVA1589——xiangqi的更多相关文章

  1. UVA1589 Xiangqi

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

  2. [刷题]算法竞赛入门经典(第2版) 4-1/UVa1589 - Xiangqi

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1589 #include<iostream> #incl ...

  3. 【习题4-1 Uva1589】Xiangqi

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 车是可以被吃掉的... 注意这个情况. 其他的模拟即可. [代码] #include <bits/stdc++.h> u ...

  4. HDU 4121 Xiangqi 我老了?

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

  5. HDU 4121 Xiangqi 模拟题

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

  6. Uva - 1589 - Xiangqi

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

  7. [算法竞赛入门经典] 象棋 ACM/ICPC Fuzhou 2011, UVa1589 较详细注释

    Description: Xiangqi is one of the most popular two-player board games in China. The game represents ...

  8. Xiangqi(简单模拟)

    4746: Xiangqi 时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte 总提交: 15            测试通过:2 描述 Xiangqi i ...

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

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

随机推荐

  1. reaver 破解wifi

    1. 无线网卡设置为监控模式 airmon-ng start wlan0 2. 获取附近路由信息 airodump-ng wlan0mon 3. 使用reaver破解wifi reaver -i wl ...

  2. 博弈论 && 题目

    终于我也开始学博弈了,说了几个月,现在才学.学多点套路,不深学.(~~) 参考刘汝佳蓝书p132 nim游戏: 假设是两维的取石子游戏,每次可以在任意一堆拿任意数量个(至少一根,因为这样游戏的状态集有 ...

  3. Spring Security在标准登录表单中添加一个额外的字段

    概述 在本文中,我们将通过向标准登录表单添加额外字段来实现Spring Security的自定义身份验证方案. 我们将重点关注两种不同的方法,以展示框架的多功能性以及我们可以使用它的灵活方式. 我们的 ...

  4. Elasticsearch优化

    2.out of memory错误 因为默认情况下es对字段数据缓存(Field Data Cache)大小是无限制的,查询时会把字段值放到内存,特别是facet查询,对内存要求非常高,它会把结果都放 ...

  5. C#数据类型 值传递和引用传递

    /// <summary> /// 电脑类 /// </summary> public class Computer { public string Type { get; s ...

  6. C#、VSTO讀取Excel類

    之前寫的類存在Excel進程不能結束的Bug,重寫ExcelReader類,類實例清理時Excel進程自動結束. class ExcelReader { // Excel Object public ...

  7. ES5数组遍历

    reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. array.reduce(function(total, currentValue, curren ...

  8. 缓存List并写入文件持久化

    LIfe is half spent before we know what is it. 缓存List并写入文件持久化 需要缓存一个List集合,比如缓存一个输入框中用户之前输入过的内容,下次当用户 ...

  9. Ubuntu获取root 权限,开机自动登入root

    新机器获取root权限,只需要给root 增加密码: sudo passwd root 修改开机自动登入: #sudo gedit /etc/lightdm/lightdm.conf 修改参数: au ...

  10. Android studio 3.1.1 找不到DDMS

    先找到AndroidStudio配置的SDK路径: 在SDK的/tools/路径下[就是和配置ADB一样的路径]有个monitor.bat 的批处理文件: 鼠标连续点击两下monitor.bat这个批 ...