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. 1_HDFS理论及安装部署

    一.hadoop简介 1.hadoop的初衷是为了解决Nutch的海量数据爬取和存储的需要,HDFS来源于google的GFS,MapReduce来源于Google的MapReduce,HBase来源 ...

  2. C#:CodeSmith根据数据库中的表创建C#数据模型Model + 因为没有钱买正版,所以附加自己写的小代码

    对于C#面向对象的思想,我们习惯于将数据库中的表创建对应的数据模型: 但假如数据表很多时,我们手动增加模型类会显得很浪费时间: 这个时候有些人会用微软提供的EntityFrameWork,这个框架很强 ...

  3. spdlog&rapidjson 使用记录

    项目中需要记录log以及读写json,对比后选择了spdlog以及rapidjson. SPDLog 对于log只是要求能够记录到文件中以及能够过滤,选择spdlog是因为这个只需要包含头文件即可使用 ...

  4. DC课程目标

  5. 《零基础入门学习Python》【第一版】视频课后答案第001讲

    测试题答案: 0. Python 是什么类型的语言? Python是脚本语言 脚本语言(Scripting language)是电脑编程语言,因此也能让开发者藉以编写出让电脑听命行事的程序.以简单的方 ...

  6. python里字典的用法介绍

    一.什么是字典 字典是python里的一种数据类型,特点是元素的无序性,和键key的唯一性.字典的创建方法是{key:values},字典里的键key只能是不可变的数据类型(整型,字符串或者是元组), ...

  7. Gym - 101775L SOS 博弈 找规律

    题目:https://cn.vjudge.net/problem/Gym-101775L PS:训练赛中被这道题折磨的不轻,和队友反复推必胜态与必败态试图推导出公式或者规律,然后推的心态逐渐失控,,, ...

  8. 【原创】关于高版本poi autoSizeColumn方法异常的情况

    之前使用的3.9版本,autoSizeColumn方法一切正常,现在切换到了3.15版本这个方法就出先了问题,问题如下,无法自动追踪所有的列. Exception in thread "ma ...

  9. iOS启动图 LaunchImage LaunchScreen.xib

    1.Images.xcassets添加LaunchImage 2.具体大小和添加类别都是可以调的 640*960   (4/4s)                                 2X ...

  10. 在. net平台下,推送IOS,Android,Windows Phone消息

    1,新建项目,在项目中添加引用,dll文件已经上传在百度网盘,点击下载 2,引入命名空间 using PushSharp; using PushSharp.Android; using PushSha ...