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 = ;
char field[N_MAX][N_MAX];
char p;
bool judge() {
bool flag = ;
if (field[][] == p && field[][] == p && field[][] == p)return true;
if (field[][] == p && field[][] == p && field[][] == p)return true;
for (int i = ; i < N_MAX;i++) {
for (int j = ; j < N_MAX;j++) {
if (field[i][j] != p) { flag = ; break; }
}
if (flag)return true;
else flag = ;
} for (int i = ; i < N_MAX; i++) {
for (int j = ; j < N_MAX; j++) {
if (field[j][i] != p) { flag = ; break; }
}
if (flag)return true;
else flag = ;
}
return false;
} int main() {
int t;
scanf("%d",&t);
while(t--){
bool flag1 = ,flag2=;
for (int i = ; i < N_MAX;i++) {
for (int j = ; j < N_MAX;j++) {
scanf(" %c",&field[i][j]);
}
}
scanf(" %c",&p);
for (int i = ; i < N_MAX; i++) {
for (int j = ; j < N_MAX;j++) {
if(field[i][j]=='.'){
field[i][j] = p;
if (judge()) { flag1 = ; break; }//直接下一步就搞定了
int num = ;
for (int k = ; k < N_MAX;k++) {//否则下两步,看看能否赢
for (int l = ; l < N_MAX;l++) {
if (field[k][l] == '.') {
field[k][l] = p;
if (judge())num++;//找到一种可行的赢法,赢的可能数加1
if (num >= ) { flag2 = ; 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 ;
}

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

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

    题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...

  2. 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/principlescomputin ...

  3. POJ 2361 Tic Tac Toe

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

  4. 【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-To ...

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

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  6. Epic - Tic Tac Toe

    N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...

  7. python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  8. ACM-Team Tic Tac Toe

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...

  9. LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

    地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...

随机推荐

  1. Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?

    什么是面向对象程序设计? 我们称为OOP(Object  Oriented  Programming) 就是非结构化的程序设计 要使用类和对象的方法来进行编程 什么是类,什么是对象 类就是封装了属性和 ...

  2. 协议(Protocol)与委托代理(Delegate)

    协议(Protocol)的作用: 1. 规范接口,用来定义一套公用的接口: 2. 约束或筛选对象. 代理(Delegate): 它本身是一种设计模式,委托一个对象<遵守协议>去做某件事情, ...

  3. 哈希表(Hash Table)/散列表(Key-Value)

    目录 1. 哈希表的基本思想 2. 哈希表的相关基本概念 1.概念: 2.哈希表和哈希函数的标准定义: 1)冲突: 2)安全避免冲突的条件: 3)冲突不可能完全避免 4)影响冲突的因素 3. 哈希表的 ...

  4. 【dp】守望者的逃离

    妙 题目描述 恶魔猎手尤迪安野心勃勃,他背着了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守望者,尤迪安开始对这个荒岛施咒,这座岛很快 ...

  5. C#基础-数组-冒泡排序

    冒泡排序基础 冒泡排序原理图分析 tmp在算法中起到数据交换的作用 int[] intNums = { 12,6,9,3,8,7 }; int tmp = intNums[0]; // 一共5次冒泡, ...

  6. python入门:模拟简单用户登录(自写)

    #!/usr/bin/env python # -*- coding: utf-8 -*- #模拟简单用户登录(自写) import getpass a = raw_input("Pleas ...

  7. PHP四种序列化方案

    原文地址:https://t.ti-node.com/thread/... 数据的序列化是一个非常有用的功能,然而目测很多人跟我一样,在刚接触这玩意的时候压根就不理解这货色到底是干啥用的,反正老师说了 ...

  8. redis的字符串操作以及在django中的使用

    redis ----redis.MongoDB : 非关系型数据库 redis   存储在内存中 MongoDB 存储在硬盘中 l  简介 redis是一个key-value存储系统 , 支持持久化 ...

  9. linux下安装mysql并设置远程连接

    腾讯云环境为Centos7.4   mysql版本为5.6 本次安装使用yum安装 检查是否已有mysql: yum list installed | grep mysql 下载yum源文件: wge ...

  10. init_bootmem_node

    初始化pg_data_t->bdtat结构体, /* * node_bootmem_map is a map pointer - the bits represent all physical ...