CodeForces - 754B Ilya and tic-tac-toe game

时间:2025-02-27 19:07:16

简单搜索

判断是否能在最后一步下棋得到胜利

问题转化为 是否有可以胜利的x的摆法

那么就只有两种情况

1、有两个x相连 并且 在端点还有.可以落子 那么就可以在最后一步 胜利

2、两个x中间恰好有一个.空着 那么可以再这里落子胜利

搜索这两种情况 存在则胜利 不存在 则无法再最后一步胜利

 #include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <map>
#include <queue>
#include <fstream>
#define READ() freopen("in.txt", "r", stdin);
using namespace std; typedef pair<int,int> P; char maze[][];
struct Point
{
int x, y;
}point[];
int num = ;
int d[][] = {-, , -, , , , , , , , , -, , -, -, -};
bool vis[][];
bool OK(int x, int y)
{
if (x < || x >= || y < || y >= ) return false;
return true;
}
bool Search()
{ queue<P> que;
for (int i = ; i < num; i++)
{
que.push(P(point[i].x, point[i].y));
}
// memset(vis, 0, sizeof(vis));
while (!que.empty())
{
P p = que.front();
que.pop();
// if(vis[p.first][p.second]) continue;
// vis[p.first][p.second] = true;
//假设是作为端点 的点
for (int i = ; i < ; i++)
{
int nx = p.first+d[i][], ny = p.second+d[i][];
if (!OK(nx, ny)) continue;
if (maze[nx][ny] == 'x')
{
nx += d[i][];
ny += d[i][];
if (!OK(nx, ny)) continue;
if (maze[nx][ny] == 'x')
{
return true;
}
}
}
//假设是作为中间的点
for (int i = ; i < ; i++)
{
int nx = p.first+d[i][], ny = p.second+d[i][];
if (!OK(nx, ny)) continue;
if (maze[nx][ny] == 'x')
{
nx = p.first-d[i][], ny = p.second-d[i][];
if (!OK[nx][ny]) continue;
if (maze[nx][ny] == 'x')
{
return true;
}
}
}
}
return false; }
int main()
{
READ()
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
scanf("%c", &maze[i][j]);
if (maze[i][j] == '.')
{
point[num].x = i;
point[num].y = j;
num++;
}
}
getchar();
}
if (Search()) cout << "YES" << endl;
else cout << "NO" << endl;
}