zoukankan      html  css  js  c++  java
  • POJ 2361 Tic Tac Toe

    题目:给定一个3*3的矩阵,是一个井字过三关游戏。开始为X先走,问你这个是不是一个合法的游戏。也就是,现在这种情况,能不能出现。如果有人赢了,那应该立即停止。那么可以知道X的步数和O的步数应该满足x==o+1或者x==o这种情况是允许的,这种情况是O赢。(x==o)这种情况是X赢(x==o+1)。那么就可以知道

    如果

    ①、如果x>o+1(步数过多)||o>x(不符合x先行的规矩)  printf("no ");

    ②、如果是x赢了win(x)但是x!=o+1 不行

    ③、如果是O赢了win(o)但是x!=o    不行

    ④、两个同时赢了              不行, 但是这个和上面的重合了的,上面判断了要么O赢要么X赢,然而步数也只有x==o或者x==o+1两种,那么必然会进入其中一个判断那里(前提是你判断的win(x)和win(o)都是赢)。那么就可以不用判断啦

    这里我学习到别人的代码判断是否赢win(x)  思路非常不错。学习了。。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <algorithm>
    using namespace std;
    #define inf 1<<28
    #define MAX(x,y) ((x)>(y)?(x):(y))
    #define MIN(x,y) ((x)>(y)?(y):(x))
    #define MID(x,y) (MAX(x,y)-((MAX(x,y)-MIN(x,y))>>1))
    #define istwopow(x) ((x&(x-1))==0)
    #define LL __int64
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    int win (char ch,char str[][100])
    {
        for (int i=1;i<=3;i++)
        {
            for (int j=1;j<=3&&str[i][j]==ch;j++)
            {
                if (j==3) return 1;
            }//
            for (int j=1;j<=3&&str[j][i]==ch;j++)
            {
                if (j==3) return 1;//
            }
        }
        if (str[1][1]==ch&&str[2][2]==ch&&str[3][3]==ch)
        {
            return 1;
        }
        else if (str[1][3]==ch&&str[2][2]==ch&&str[3][1]==ch)
        {
            return 1;
        }
        return 0;
    }
    void work  ()
    {
        char str[100][100];
        for (int i=1;i<=3;i++)
        {
            scanf ("%s",str[i]+1);
        }
        int x=0,o=0;
        for (int i=1;i<=3;i++)
        {
            for (int j=1;j<=3;j++)
            {
                x += str[i][j]=='X';
                o += str[i][j]=='O';
            }
        }
        if (o>x||x>o+1)//步数不符合
        {
            printf ("no
    ");
            return ;
        }
        char ch1='X';
        char ch2='O';
        if (win(ch2,str)&&o!=x)//o赢应该步数相等
        {
            printf ("no
    ");
            return ;
        }
        if (win(ch1,str)&&x!=o+1)//x赢应该步数x==o+1
        {
            printf ("no
    ");
            return ;
        }
        printf ("yes
    ");
        return ;
    }
    int main ()
    {
        freopen("data.txt","r",stdin);
        int t;
        scanf ("%d",&t);
        while (t--)
        {
            work ();
        }
        return 0;
    }
    View Code

    Your job is to read a grid and to determine whether or not it could possibly be part of a valid Tic Tac Toe game. That is, is there a series of plays that can yield this grid somewhere between the start and end of the game?

    你的任务是读入棋盘状态,问可不可能是一个有效的三子棋棋盘,也就是说是否存在一系列走棋,能到达该棋盘状态。

  • 相关阅读:
    逻辑即理性
    关于股市与经济的一点思考
    Objective-C Automatic Reference Counting (ARC)
    错误与异常
    Programming with Objective-C -- 属性的缺省实现
    视频容器格式与编码格式简介
    视频基础知识-封装格式和编码格式
    各种音视频编解码学习详解
    你的技术护城河是什么?--技术前沿代表了一个人的眼力和价值
    技术前沿--很多技术不了解细节也应该了解大概---知道能用来干什么--了解技术的价值
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/5342573.html
Copyright © 2011-2022 走看看