Five-In-a-Row CodeForces
来源:互联网 发布:不履行网络安全法 编辑:程序博客网 时间:2024/06/07 18:50
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.
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.
Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'.
XX.XX..........OOOO.................................................................................
YES
XXOXX.....OO.O......................................................................................
NO
题意:
下五子棋,判断在10*10的棋盘上再放入一个子,即“X”,是否能得到五个连续的棋子。
题解:
遍历图,找到“.”,将其替换为“X”,尝试其八种方向,每两个相反的方向得到的棋子个数相加。如果满足五个,返回。如果不满足,尝试所有方向。注意有大于五个的可能。搜索之后把“.”替换回来。
代码如下:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char mp[11][11];int p;void dfs(int x,int y){ int tx=x; int ty=y; int i,j; int sum=0; sum=0; for(i=tx; i>=0; i--) if(mp[i][ty]=='X') sum++; else break; for(i=tx+1; i<10; i++) if(mp[i][ty]=='X') sum++; else break; if(sum>=5) p=1; sum=0; for(i=tx+1,j=ty+1; i<10,j<10; i++,j++) if(mp[i][j]=='X') sum++; else break; for(i=tx,j=ty; i>=0,j>=0; i--,j--) if(mp[i][j]=='X') sum++; else break; if(sum>=5) p=1; sum=0; for(i=ty+1; ty<10; i++) if(mp[tx][i]=='X') sum++; else break; for(i=ty; i>=0; i--) if(mp[tx][i]=='X') sum++; else break; if(sum>=5) p=1; sum=0; for(i=tx+1,j=ty-1; i<10,j>=0; i++,j--) if(mp[i][j]=='X') sum++; else break; for(i=tx,j=ty; i>=0,j<10; i--,j++) if(mp[i][j]=='X') sum++; else break; if(sum>=5) p=1; sum=0;}int main(){ int i,j; for(i=0; i<10; i++) scanf("%s",mp[i]); for(i=0; i<10; i++) { for(j=0; j<10; j++) if(mp[i][j]=='.') { p=0; mp[i][j]='X'; dfs(i,j); mp[i][j]='.'; if(p) break; } if(p) break; } if(p) printf("YES\n"); else printf("NO\n");}
- Five-In-a-Row CodeForces
- Five-In-a-Row CodeForces
- Five-In-a-Row CodeForces
- Five-In-a-Row CodeForces
- Five-In-a-Row CodeForces
- Five-In-a-Row CodeForces
- Codeforces 825 B Five-In-a-Row
- CodeForces 825B Five-In-a-Row
- Educational Codeforces Round 25 B. Five-In-a-Row
- Educational Codeforces Round 25 B. Five-In-a-Row
- Educational Codeforces Round 25 B. Five-In-a-Row
- CodeForces 825B:Five-In-a-Row(模拟)
- B. Five-In-a-Row
- hdu 2291 Five in a Row, Again
- HDU 2699 Five in a Row (搜索)
- hdu2699 Five in a Row 暴力穷举
- B.Five-In-a-Row【逻辑+模拟】
- CodeForce 825B Five-In-a-Row
- 第十八天,用dbutil实现一个简单的注册系统
- cocos2d-x3.2 在Windows下打包Android平台APK
- JavaWeb中如何导出Excel
- 正式开启CSDN博客之路
- Crazy Learning for Day 20
- Five-In-a-Row CodeForces
- Lingo基本使用方法
- 权限修饰符与抽象
- Java循环变量在哪里赋值
- Ubuntu16安装opencv
- Java从键盘录入并写入到文件中
- android_内存卡播放器
- pmm-server服务部署解读
- ConstraintLayout记录