CodeForces - 754B

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 gets three 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 letter x), or 'o' (lowercase English letter o). 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.ox
ox..
x.o.
oo.x
Output
NO
Input
x..x
..oo
o...
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];
/*
.xox
o.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;
}
/*
xxox
x...
.o.o
o..o
*/





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值