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的棋盘,规则是横竖或者斜着连成5个棋子以上获得胜利。现在Alice持'X'棋子,判断他是否能在一步之内取得胜利。
分析:因为棋盘不是很大,所以直接遍历每个点,遇到'.'则进入搜索,将'.'当做下一步要下的点,判断四个方向是否满足cnt>=4.满足则输出YES,否则继续判断直到结束。
代码如下:
#include <map> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> #define LL long long using namespace std; const int MX = 1e5 + 5; const int mod = 1e9 + 7; const int INF = 2e9 + 5; char mp[15][15]; int go[][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, -1, -1, 1, -1, -1, 1}; int dfs(int x, int y){ for(int i = 0; i < 8; i += 2){ int cnt = 0; int tx = x, ty = y; tx += go[i][0]; ty += go[i][1]; while(mp[tx][ty] == 'X'){ cnt++; tx += go[i][0]; ty += go[i][1]; } tx = x, ty = y; tx += go[i+1][0]; ty += go[i+1][1]; while(mp[tx][ty] == 'X'){ cnt++; tx += go[i+1][0]; ty += go[i+1][1]; } if(cnt >= 4) return 1; } return 0; } int main(){ for(int i = 1; i <= 10; i++){ scanf("%s", mp[i]+1); } for(int i = 1; i <= 10; i++){ for(int j = 1; j <= 10; j++){ if(mp[i][j] == '.'){ if(dfs(i, j)){ printf("YES\n"); return 0; } } } } printf("NO\n"); return 0; }