CodeForces

来源:互联网 发布:如家精选 知乎 编辑:程序博客网 时间:2024/06/02 07:11

B. Ilya and tic-tac-toe game

time limit per test

2 seconds

memory limit per test

256 megabytes

input
standard input
output
standard output

Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. He played a lot of games today with his friend Arseny. The friends became tired and didn't finish the last game. It was Ilya's turn in the game when they left it. Determine whether Ilya could have won the game by making single turn or not.

The rules of tic-tac-toe on the 4 × 4 field are as follows. Before the first turn all the field cells are empty. The two players take turns placing their signs into empty cells (the first player places Xs, the second player places Os). The player who places Xs goes first, the another one goes second. The winner is the player who first getsthree of his signs in a row next to each other (horizontal, vertical or diagonal).

Input

The tic-tac-toe position is given in four lines.

Each of these lines contains four characters. Each character is '.' (empty cell), 'x' (lowercase English letterx), or 'o' (lowercase English lettero). It is guaranteed that the position is reachable playing tic-tac-toe, and it is Ilya's turn now (in particular, it means that the game is not finished). It is possible that all the cells are empty, it means that the friends left without making single turn.

Output

Print single line: "YES" in case Ilya could have won by making single turn, and "NO" otherwise.

Examples
Input
xx...oo.x...oox.
Output
YES
Input
x.oxox..x.o.oo.x
Output
NO
Input
x..x..ooo...x.xo
Output
YES
Input
o.x.o....x..ooxx
Output
NO


题意:

题目意思很简单   这是一个井字棋游戏  问你能不能通过再走一步x的方式使x获胜 


看起来好像非常简单的样子  但是实际上还是比较复杂的   因为如果你想用搜索写的话 还要传参数来固定方向 很麻烦 不如模拟 

虽然题意很简单  但是我写的还是比较复杂的 我好像也没看到有大神有特别高明的写法


思路:

枚举所有点  把点换成x  判断是否胜利  即可~~

附上几个样例


#include <algorithm>#include <cstring>#include <iostream>#include <queue>#include <cstdio>using namespace std;char str[5][5];/*.xoxo.x.x.o...o.*/bool check(int x,int y, char Q)//来判断斜线的组成情况 {  //  for (int i = 0; i < 4; i ++) {   //     cout << str[i] << endl; //   }cout << endl;   // if(x - 2 < 0 || y + 2 >= 4 || y - 2 < 0)return false;    if(str[x][y] == Q && str[x - 1][y + 1] == Q && str[x - 2][y + 2] == Q)return true;    if(str[x][y] == Q && str[x - 1][y - 1] == Q && str[x - 2][y - 2] == Q)return true;    return false;}bool judge(char Q){    int res = 0;    for(int i = 0; i < 4; i ++) {//判断行            res = 0;        for (int j = 0; j < 4; j ++) {            if(str[i][j] ==  Q) {                res ++;if(res == 3)return true;            } else  {                res = 0;            }        }    }    res = 0;    for(int i = 0; i < 4; i ++) {//判断列            res = 0;        for (int j = 0; j < 4; j ++) {            if(str[j][i] == Q) {                res ++;if(res == 3)return true;            } else  {                res = 0;            }        }    }    res = 0;    for(int i = 0; i < 4; i ++) {//判断斜线        for (int j = 0; j < 4; j ++) {            if(str[i][j] == Q && check(i , j, Q)) {                return true;            }        }    }    return false;}int main(){    for (int i = 0; i < 4; i ++)        cin >> str[i];    bool flag = false;    char op = 'o';    if(judge(op)) {        cout << "NO" << endl;        return 0;    }    op = 'x';    if(judge(op)) {        cout << "NO" << endl;        return 0;    }//这两个部分是因为我没有读懂题目  然后迟迟不过 然后自己脑补的条件~~~  去掉也行    op = 'x';    for (int i = 0; i < 4; i ++) {        for(int j = 0; j < 4; j ++) {           if(str[i][j] == '.') {                str[i][j] = 'x';                if(judge(op)) {                    flag = true;                    break;                }                str[i][j] = '.';            }if(flag)break;        }    }    if(flag)cout << "YES" << endl;    else cout << "NO" << endl;}/*xxoxx....o.oo..o*/