CodeForces 825B(DFS)

本文介绍了一种五子棋游戏的AI实现方法,通过遍历棋盘上的空位并尝试放置棋子,利用深度优先搜索判断是否能形成五连珠取胜。

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

问题描述:

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.

Input

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.

Output

Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'.

Example

Input
XX.XX.....
.....OOOO.
..........
..........
..........
..........
..........
..........
..........
..........
Output
YES
Input
XXOXX.....
OO.O......
..........
..........
..........
..........
..........
..........
..........
..........
Output
NO

题目题意:给了我们一个10*10的图,问我们能否在加上一个X满足五子棋的成立条件.

题目分析:我们遍历每一个'.'点,把它变成'X‘,然后去沿四个方向去dfs(),

题目代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
using namespace std;

char mmap[15][15];
bool vis[15][15],ans;
int Next1[2][2]={{-1,0},{1,0}};
int Next2[2][2]={{0,-1},{0,1}};
int Next3[2][2]={{-1,-1},{1,1}};
int Next4[2][2]={{1,-1},{-1,1}};
int cnt;

void dfs1(int x,int y)
{
    if (ans) return ;
    if (cnt==5) {
        ans=true;
        return ;
    }
    for (int i=0;i<2;i++) {
        int dx=x+Next1[i][0];
        int dy=y+Next1[i][1];
        if (dx<1||dx>10||dy<1||dy>10) continue;
        if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
        if (mmap[dx][dy]=='X') {
            vis[dx][dy]=true;
            cnt++;
            dfs1(dx,dy);
        }
    }

}
void dfs2(int x,int y)
{
    if (ans) return ;
    if (cnt==5) {
        ans=true;
        return ;
    }
    for (int i=0;i<2;i++) {
        int dx=x+Next2[i][0];
        int dy=y+Next2[i][1];
        if (dx<1||dx>10||dy<1||dy>10) continue;
        if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
        if (mmap[dx][dy]=='X') {
            vis[dx][dy]=true;
            cnt++;
            dfs2(dx,dy);
        }
    }

}
void dfs3(int x,int y)
{
    if (ans) return ;
    if (cnt==5) {
        ans=true;
        return ;
    }
    for (int i=0;i<2;i++) {
        int dx=x+Next3[i][0];
        int dy=y+Next3[i][1];
        if (dx<1||dx>10||dy<1||dy>10) continue;
        if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
        if (mmap[dx][dy]=='X') {
            vis[dx][dy]=true;
            cnt++;
            dfs3(dx,dy);
        }
    }

}
void dfs4(int x,int y)
{
    if (ans) return ;
    if (cnt==5) {
        ans=true;
        return ;
    }
    for (int i=0;i<2;i++) {
        int dx=x+Next4[i][0];
        int dy=y+Next4[i][1];
        if (dx<1||dx>10||dy<1||dy>10) continue;
        if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
        if (mmap[dx][dy]=='X') {
            vis[dx][dy]=true;
            cnt++;
            dfs4(dx,dy);
        }
    }

}
int main()
{
    for (int i=1;i<=10;i++) {
        scanf("%s",mmap[i]+1);
    }
    ans=false;
    for (int i=1;i<=10;i++) {
        for (int j=1;j<=10;j++) {
            if (mmap[i][j]=='.'&&!ans) {
                mmap[i][j]='X';
                memset (vis,false,sizeof (vis));
                vis[i][j]=true;
                cnt=1;
                dfs1(i,j);
                memset (vis,false,sizeof (vis));
                vis[i][j]=true;
                cnt=1;
                dfs2(i,j);
                memset (vis,false,sizeof (vis));
                vis[i][j]=true;
                cnt=1;
                dfs3(i,j);
                memset (vis,false,sizeof (vis));
                vis[i][j]=true;
                cnt=1;
                dfs4(i,j);
                mmap[i][j]='.';
            }
            if (ans) break;
        }
        if (ans) break;
    }
    if (ans) puts("YES");
    else puts("NO");
}











Codeforces比赛中,编写高效、简洁的代码是取得好成绩的关键之一。选手通常会依赖一些常用的代码模板或框架,以节省时间并减少出错的可能性。以下是一些在Codeforces比赛中常见的代码模板及其用途: ### 输入输出模板 在竞赛中,标准输入输出的速度对性能有一定影响,尤其是当数据量较大时。Java中通常使用`BufferedReader`和`PrintWriter`来提高输入输出效率。 ```java import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); String line; while ((line = br.readLine()) != null) { pw.println(line); } pw.flush(); } } ``` ### 快速读取整数数组 对于需要频繁读取多个整数的情况,可以使用以下方法将一行输入拆分为整数数组。 ```java StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(st.nextToken()); } ``` ### 数学函数模板 一些常见的数学运算可以预先定义为函数,例如最大公约数(GCD)、快速幂等。 ```java public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static long fastPower(long base, long exponent, long mod) { long result = 1; while (exponent > 0) { if ((exponent & 1) == 1) { result = (result * base) % mod; } base = (base * base) % mod; exponent >>= 1; } return result; } ``` ### 数据结构模板 使用Java集合框架时,可以预先准备好一些常用的数据结构操作,例如优先队列、双端队列等。 ```java PriorityQueue<Integer> minHeap = new PriorityQueue<>(); Deque<Integer> deque = new ArrayDeque<>(); ``` ### 图论算法模板 图论问中,常见的算法如广度优先搜索(BFS)、深度优先搜索(DFS)、最短路径算法(Dijkstra)等可以预先准备好模板。 ```java // BFS模板 public static void bfs(int start, List<List<Integer>> adj, boolean[] visited) { Queue<Integer> queue = new LinkedList<>(); queue.add(start); visited[start] = true; while (!queue.isEmpty()) { int node = queue.poll(); for (int neighbor : adj.get(node)) { if (!visited[neighbor]) { visited[neighbor] = true; queue.add(neighbor); } } } } ``` ### 动态规划模板 动态规划是竞赛中常见的解方法。可以根据问的特性设计状态转移方程。 ```java int n = 100; int[] dp = new int[n + 1]; dp[0] = 0; for (int i = 1; i <= n; i++) { dp[i] = Integer.MAX_VALUE; for (int j = 1; j * j <= i; j++) { dp[i] = Math.min(dp[i], dp[i - j * j] + 1); } } ``` ### 字符串处理模板 字符串处理中,常见的操作如字符串匹配、回文检查等也可以预先准备好。 ```java public static boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while (left < right) { if (s.charAt(left) != s.charAt(right)) { return false; } left++; right--; } return true; } ``` 以上模板可以作为Codeforces比赛中的常用代码框架,选手可以根据具体目要求进行调整和优化。此外,建议平时刷时将这些模板保存起来,以便比赛时快速调用[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值