Codeforces #390 (Div. 2) B. Ilya and tic-tac-toe game ( DFS

本文探讨了一个4x4的三子棋游戏,玩家Ilya使用X标记尝试在单次行动中赢得游戏。通过遍历所有可能的位置并采用深度优先搜索策略来判断Ilya是否有获胜的机会。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Ilya and tic-tac-toe game

Description

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.

Sample Input

xx..
.oo.
x...
oox.
x.ox
ox..
x.o.
oo.x
x..x
..oo
o...
x.xo
o.x.
o...
.x..
ooxx

Sample Output

YES
NO
YES
NO

Hint

In the first example Ilya had two winning moves: to the empty cell in the left column and to the leftmost empty cell in the first row.

In the second example it wasn’t possible to win by making single turn.

In the third example Ilya could have won by placing X in the last row between two existing Xs.

In the fourth example it wasn’t possible to win by making single turn.

题意

给你个4x4图 问你下一次在‘.’上填个‘x’是否可以连成三个?
三子棋(摔

题解:

每个点试试 跑dfs进行判断

AC代码

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define CLR(a,b) memset(a,b,sizeof(a))

const int N = 1000;
char mps[N][N];
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, -1, 1, 1, -1, -1, 1};

bool check(int x,int y)
{
    return x>=0 && x<4 && y>=0 && y<4;
}
int dfs()
{
    bool flag = false;
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 4; j++) {
            if(mps[i][j] == 'x') {
                for(int k = 0; k < 8; k++) {
                    int xx = i + dx[k];
                    int yy = j + dy[k];
                    if(mps[xx][yy]=='x' && check(xx,yy)) {
                        int xxx = i + dx[k]*2;
                        int yyy = j + dy[k]*2;
                        if(mps[xxx][yyy]=='x' && check(xxx,yyy)) {
                            flag = true;
                        }

                    }
                }
            }
        }
    }
    return flag;
}
int  main()
{
    for(int i = 0; i < 4; i++) scanf("%s",mps[i]);
    bool flag = false;
    for(int i = 0;i < 4; i++) {
        for(int j = 0;j < 4; j++) {
            if(mps[i][j] == '.') {
                mps[i][j] = 'x';
                if(dfs()) {
                    flag = true; break;
                }
                else {
                    mps[i][j] = '.';
                }
            }
        }
        if(flag) break;
    }
    if(flag)
        puts("YES");
    else
        puts("NO");
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值