foj Problem 2283 Tic-Tac-Toe

                                                                                                Problem 2283 Tic-Tac-Toe

Accept: 60    Submit: 92
Time Limit: 1000 mSec    Memory Limit : 262144
KB

Problem Description

Kim likes to play Tic-Tac-Toe.

Given a current state, and now Kim is going to take his next move. Please
tell Kim if he can win the game in next 2 moves if both player are clever
enough.

Here “next 2 moves” means Kim’s 2 move. (Kim move,opponent move, Kim move,
stop).

Game rules:

Tic-tac-toe (also known as noughts and crosses or Xs and Os) is a
paper-and-pencil game for two players, X and O, who take turns marking the
spaces in a 3×3 grid. The player who succeeds in placing three of their marks in
a horizontal, vertical, or diagonal row wins the game.

Input

First line contains an integer T (1 ≤ T ≤ 10), represents there are T test
cases.

For each test case: Each test case contains three lines, each line three
string(“o” or “x” or “.”)(All lower case letters.)

x means here is a x

o means here is a o

. means here is a blank place.

Next line a string (“o” or “x”) means Kim is (“o” or “x”) and he is going to
take his next move.

Output

For each test case:

If Kim can win in 2 steps, output “Kim win!”

Otherwise output “Cannot win!”

Sample Input

3
. . .
. . .
. . .
o
o x o
o . x
x x o
x
o x .
. o .
. . x
o

Sample Output

Cannot win!
Kim win!
Kim win!

题意:kim两手棋之内能否胜利。

思路:直接暴力搜索即可:

讨论这两手棋,穷搜每一个还未下任何棋的点,若下第一手棋就能形成三子一线,就直接赢了,否则,判断第二手棋能否赢。

这时换个想法,因为两方都足够聪明,如果kim下的第二手棋有不止一种赢的下法,那么kim一定能赢,因为对手再厉害也只能挡住一种能赢的下法而已(可以忽略对手的下的那手棋,直接讨论kim赢的可能数即可)

AC代码:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<bitset>
using namespace std;
#define INF 0x3f3f3f3f
const int N_MAX = 3;
char field[N_MAX][N_MAX];
char p;
bool judge() {
    bool flag = 1;
    if (field[0][0] == p && field[1][1] == p && field[2][2] == p)return true;
    if (field[2][0] == p && field[1][1] == p && field[0][2] == p)return true;
    for (int i = 0; i < N_MAX;i++) {
        for (int j = 0; j < N_MAX;j++) {
            if (field[i][j] != p) { flag = 0; break; }
        }
        if (flag)return true;
        else flag = 1;
    }

    for (int i = 0; i < N_MAX; i++) {
        for (int j = 0; j < N_MAX; j++) {
            if (field[j][i] != p) { flag = 0; break; }
        }
        if (flag)return true;
        else flag = 1;
    }
    return false;
}

int main() {
    int t;
    scanf("%d",&t);
    while(t--){
        bool flag1 = 0,flag2=0;
        for (int i = 0; i < N_MAX;i++) {
            for (int j = 0; j < N_MAX;j++) {
                scanf(" %c",&field[i][j]);
            }
        }
        scanf(" %c",&p);
        for (int i = 0; i < N_MAX; i++) {
            for (int j = 0; j < N_MAX;j++) {
              if(field[i][j]==‘.‘){
                  field[i][j] = p;
                  if (judge()) { flag1 = 1; break; }//直接下一步就搞定了
                  int num = 0;
                  for (int k = 0; k < N_MAX;k++) {//否则下两步,看看能否赢
                      for (int l = 0; l < N_MAX;l++) {
                          if (field[k][l] == ‘.‘) {
                              field[k][l] = p;
                              if (judge())num++;//找到一种可行的赢法,赢的可能数加1
                              if (num >= 2) { flag2 = 1; break; }
                              field[k][l] = ‘.‘;
                          }
                      }
                      if (flag2)break;
                  }
                  field[i][j] = ‘.‘;
                  if (flag2)break;
              }
            }
            if (flag1||flag2)break;
        }
        if (flag1 || flag2)printf("Kim win!\n");
        else printf("Cannot win!\n");
    }
    return 0;
}
时间: 2024-08-04 04:27:24

foj Problem 2283 Tic-Tac-Toe的相关文章

2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

题面: C. Team Tic Tac Toe Input ?le: standard input Output ?le: standard output Time limit: 1 second Memory limit: 256 megabytes Farmer John owns 26 cows, which by happenstance all have names starting with different letters of the alphabet, so Farmer J

tic tac toe

井字棋 tic tac toe,布布扣,bubuko.com

amazon.设计1. tic tac toe

//不觉中 已经全力找工作好久好久了.大概有1年半了.身心疲惫,不要放弃.曙光快来了. 1.tic tac toe //http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe.html 类似相关棋牌坐标对战游戏 一通百通 /** * Enumerations for the various states of the game */ public enum GameState { // to save as "G

Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputing-001/wiki/view? page=trees 1.2 CODE无parent域的树 http://www.codeskulptor.org/#poc_tree.py class Tree: """ Recursive definition for trees plus

【leetcode】1275. Find Winner on a Tic Tac Toe Game

题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-Toe: Players take turns placing characters into empty squares (" "). The first player A always places "X" characters, while the second pl

Epic - Tic Tac Toe

N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If 3 consecutive samecolor found, that color will get 1 point. So if 4 red are vertically then pointis 2. Find the winner. def tic_tac_toe(board,n) red,

Learn Python 015: Tic Tac Toe Game

board = [' ' for i in range(9)] def print_board(): row_1 = '|{}|{}|{}|'.format(board[0], board[1], board[2]) row_2 = '|{}|{}|{}|'.format(board[3], board[4], board[5]) row_3 = '|{}|{}|{}|'.format(board[6], board[7], board[8]) print('\n') print(row_1)

UVa 10363 - Tic Tac Toe

题目:给你一个井字棋的状态,判断是否合法. 分析:枚举.直接枚举多有情况判断即可. 合法状态有三种情况:(X先下子) 1.X赢,则O不能赢,且X比O多一子: 2.O赢,则X不能赢,且O和X子一样多: 3.没人赢,此时O的子和可能和X一样多,也可能少一个. 说明:简单题(⊙_⊙). #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include &l

[CareerCup] 17.2 Tic Tac Toe 井字棋游戏

17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏,有下面几点需要考虑: 1. 判断是否能赢hasWon函数是调用一次还是多次,如果是多次,我们可能为了优化而需要加入一些预处理. 2. 井字棋游戏通常是3x3的大小,我们是否想要实现NxN的大小? 3. 我们需要在代码紧凑,执行速度和代码清晰之间做出选择. #include <iostream> #