FIve in a row
Alice and Bob play 5-in-a-row game. They have a playing field of size 10 × 10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.
In current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins immediately.
Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal.
Input
You are given matrix 10 × 10 (10 lines of 10 characters each) with capital Latin letters 'X' being a cross,
letters 'O' being a nought and '.' being an empty cell. The number of 'X' cells is equal to the number of 'O' cells
and there is at least one of each type. There is at least one empty cell.
It is guaranteed that in the current arrangement nobody has still won.
Output
Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'.
Example
XX.XX.....
.....OOOO.
..........
..........
..........
..........
..........
..........
..........
..........
YES
XXOXX.....
OO.O......
..........
..........
..........
..........
..........
..........
..........
..........
NO
题意:
多组输入,五子棋的规则。给一个10*10的棋盘,棋盘内有两种棋子“X”和“O”,现在要再加入一个棋子“X”,使得棋子“X”可以按照五子棋的规则连成五个或五个以上。如果加入一个可以连成的话输出“YES”,不能的话输出“NO”。
题解:向8个方向模拟,判断是否可行。(分为4个主要方向)
AC代码:
#include<stdio.h>
#include<string.h>
char map[10][10];
int dis[4][4] = { { 0,1,0,-1 },{ 1,0,-1,0 },{ 1,1,-1,-1 },{ -1,1,1,-1 } };
int win(int x, int y)
{
int xl = x, yl = y, n = 0, m = 0;
while (n + 1<5)
{
m = 0;
xl = x;
yl = y;
while (map[xl][yl] == 'X')
{
m++;
xl = xl + dis[n][0];
yl = yl + dis[n][1];
if (m == 5)
return 1;
if (xl<0 || yl<0 || xl >= 10 || yl >= 10)//过界,停止
break;
}
m = m - 1;
xl = x;
xl = y;
while (map[xl][yl] == 'X')
{
m++;
xl = xl + dis[n][2];
yl = yl + dis[n][3];
if (m == 5)
return 1;
if (xl<0 || yl<0 || xl >= 10 || yl >= 10)//过界,停止
break;
}
n++;
}
return 0;
}
int main()
{
while (~scanf("%s", map[0]))
{
for (int i = 1; i<10; i++)
scanf("%s", map[i]);
int flag = 0;
for (int i = 0; i<10; i++)
{
for (int j = 0; j<10; j++)
{
if (map[i][j] == '.')
{
map[i][j] = 'X';
if (win(i, j))
{
flag = 1;
break;
}
map[i][j] = '.';
}
}
if (flag)
break;
}
if (flag)
printf("YES\n");
else printf("NO\n");
}
return 0;
}
FIve in a row的更多相关文章
- 浅析MySQL基于ROW格式的二进制日志
上文分析的二进制日志实际上是基于STATEMENT格式的,下面我们来看看基于ROW格式的二进制日志,毕竟,两者对应的binlog事件类型也不一样,同时,很多童鞋反映基于ROW格式的二进制日志无法查到原 ...
- java.lang.IllegalStateException:Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx...}: java.lang.IllegalSta ...
- ResultSet can not re-read row data for column 1.
error:ResultSet can not re-read row data for column 1. 将数据类型改为varchar(max)后,查询数据错误 改正:将jdbc驱动改为jtds驱 ...
- 解决一则enq: TX – row lock contention的性能故障
上周二早上,收到项目组的一封邮件: 早上联代以下时间点用户有反馈EDI导入"假死",我们跟踪了EDI导入服务,服务是正常在跑,可能是处理的慢所以用户感觉是"假死" ...
- CI生成查询记录集result(),row(),row_array().....
result() 该方法执行成功返回一个对象数组,失败则返回一个空数组. 一般情况下,我们使用下面的方法遍历结果,代码就像这样: $query = $this->db->query(&qu ...
- SQL——行值表达式(Row Value Expressions)
概述 最近接触了一个新概念——行值表达式,也叫做行值构造器.这是一个很强大的SQL功能,通常我们所操作的SQL表达式都只能针对一行中的单一字段进行操作比较,而行值表达式可以针对一行中的多个字段进行操作 ...
- MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误
前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sa ...
- WPF DataGrid 鼠标双击选中的DataGridRow及Row数据
设置DataGrid的MouseDoubleClick事件 代码 //DataGrid鼠标双击事件 Private void dataGrid_MouseDoubleClick(object send ...
- ORACLE等待事件:enq: TX - row lock contention
enq: TX - row lock contention等待事件,这个是数据库里面一个比较常见的等待事件.enq是enqueue的缩写,它是一种保护共享资源的锁定机制,一个排队机制,先进先出(FIF ...
- ORACLE AWR结合ASH诊断分析enq: TX - row lock contention
公司用户反馈一系统在14:00~15:00(2016-08-16)这个时间段反应比较慢,于是生成了这个时间段的AWR报告, 如上所示,通过Elapsed Time和DB Time对比分析,可以看出在这 ...
随机推荐
- Linux服务器更改Apache2默认页面
方式一 获取root权限 su root //或者 sudo -i 进入 /var/www目录下 cd /var/www 创建目录 mkdir -m 777 myhtml // myhtml为自己创建 ...
- 【原创】使用批处理脚本生成包并自动上传到nuget
Hello 大家好,我是TANZAME,我们又见面了. NuGet 是什么这里就不再重复啰嗦,园子里一搜一大把.今天要跟大家分享的是,在日常开发过程中如何统一管理我们的包,如何通过批处理脚本生成包并自 ...
- SQL Server 2014:在修改表的内容时,提示“此单元格已更改,尚未将更改提交到数据库”,怎么处理?
那一行上的属性为“不允许为null”的所有字段都填上对应信息,按回车键或者点击下一行任意一个单元格便会自动将更改的信息提交到数据库.
- 分组取topN
假设有这样一个文件,文件内容如下 class1 class2 class1 class1 class2 class2 class1 class2 class1 class2 要求按照班级分组取出每个班 ...
- suseoj The wheat of the prime minister
1202: 2018四川理工学院大学生ACM程序设计:The wheat of the prime minister 时间限制: 1 Sec 内存限制: 128 MB提交: 4 解决: 3[提交] ...
- nyoj 95-众数问题 (map)
95-众数问题 内存限制:64MB 时间限制:3000ms 特判: No 通过数:16 提交数:29 难度:3 题目描述: 所谓众数,就是对于给定的含有N个元素的多重集合,每个元素在S中出现次数最多的 ...
- 总结:mysql的各种增删改查!
(原创总结)分为数据库的增删改查,数据表(和字段)的增删改查,数据的增删改查 三部分!// 创建用户并授权 GRANT SELECT ON bodydb.user TO us@localhost ID ...
- ubantu删除源码安装文件
1.在安装目录下执行 make uninstall (如安装目录为/opt/software/opencv3.1.0/release) 2.删除系统相关文件 cd /usr sudo find . - ...
- 关于PHP中依赖注入的详细介绍
依赖注入原理: 依赖注入是一种允许我们从硬编码的依赖中解耦出来,从而在运行时或者编译时能够修改的软件设计模式.简而言之就是可以让我们在类的方法中更加方便的调用与之关联的类. 实例讲解: 假设有一个这样 ...
- matlab-汉字unicode编码转换
str='黑大哥'bianma=unicode2native(str); disp(bianma); pp=native2unicode(bianma);disp(pp)