• cf-754B Ilya and tic-tac-toe game(搜索查找)


    https://codeforces.com/problemset/problem/754/B

    题意:

    一个4*4的棋盘,每个单元可能是‘x’,‘o’,‘.’,的一种。其中“.”,代表当前单元没有棋子,“o”代表当前单元有乙的棋子,“x”代表当前单元有甲的棋子。现在需要判断:如果下一步轮到甲下棋,他能否获胜?(胜利的规则是棋盘上有自己方的3个子在一条线上,这条线可以是水平,竖直或倾斜45度)

    思路:

    因为棋盘只有4*4,所以暴力搜索查找即可,判断一下如果是x,周围八个方向是否满足胜利条件,flag标记即可。

    代码:

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string>
    #include<iomanip>
    #include<algorithm>
    #include<string.h>
    #include<queue>
    #include<cmath>
    #include<stack>
    
    using namespace std;
    const int maxn=1e5+10;
    const int inf=1e10;
    typedef long long ll;
    
    char a[4][4];
    int dx[8]={0,0,1,-1,1,-1,1,-1};
    int dy[8]={1,-1,0,0,1,-1,-1,1};
    
    int main()
    {
        for(int i=0; i<4; i++) cin>>a[i];
        int flag=0;
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
            {
                if(a[i][j]=='x')
                {
                    for(int k=0; k<8; k++)
                    {
                        if(i+2*dx[k]>=0&&i+2*dx[k]<4&&j+2*dy[k]>=0&&j+2*dy[k]<4)
                        {
                            int temp=0;
                            for(int m=1; m<=2; m++)
                            {
                                if(a[i+dx[k]*m][j+dy[k]*m]=='x')
                                 temp++;
                                else if(a[i+dx[k]*m][j+dy[k]*m]=='.')
                                 continue;
                                else temp--;
                            }
                            if(temp>=1) {
                                flag=1;
                                break;
                            }
                        }
                    }
                }
            }
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
        //system("pause");
        return 0;
    }
  • 相关阅读:
    Guava入门第四章(Objects)
    Guava入门第三章(Preconditions)
    Guava入门第二章(Splitter)
    Guava入门第一章(Joiner)
    Docker入门第六章
    Docker遇到的问题
    Docker命令图
    2016-08-26-Java比较器的使用
    2017-10-6-MyBatis配置简述
    2017-9-17-Java Exception小结
  • 原文地址:https://www.cnblogs.com/sweetlittlebaby/p/14355240.html
Copyright © 2020-2023  润新知