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;
}
最新文章
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查
- Elasticsearch、Logstash、Kibana搭建统一日志分析平台
- oracle often commands
- javaWeb开发模式
- logback配置详解(二)
- Android apk 的安装过程
- html---textarea初始化时就有个table空格以及tab键操作无效
- c++ namespace命名空间详解
- 华为OJ之放砝码
- [Google Codejam] Round 1A 2016 - The Last Word
- Linux系统用户管理
- 【Win 10 应用开发】UI Composition 札记(三):与 XAML 集成
- 实施一个SAP项目大概分为下面几个过程
- RabbitMQ跟Redis做消息队列的区别
- unity中的UGUI一些组件的使用
- 爬虫--反爬--css反爬---大众点评爬虫
- 无根树的计数——prufer序列
- 实战fortran77基础语法
- c#开发activex注册问题
- dedecms提取某栏目及子栏目名称到首页怎么弄
热门文章
- 我们碰到了大麻烦,一个新来的传教士惹恼了上帝,上帝很愤怒,要求我们把圣经(bbe.txt)背熟,直至他说哪个单词,我们就要飞快的回答出这个单词在第几行第几个单词位置。听说你是个优秀的程序员,那么髟助我们完成这个不可能的任务吧
- 银联ISO8583报文解析过程
- ES6学习笔记01 -- 暂时性死区 ( temporal dead zone )
- (转)白话数字签名(2)——软件&;设备
- tornado的使用-参数篇
- MySQL的安装+可视化工具+JDBC的增删改查
- 通过ESP8266调节继电器时间
- element表格点击行即选中该行复选框
- TreeMap的源码学习
- 作业要求20191031-7 beta week 1/2 Scrum立会报告+燃尽图 05