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. 实验十三 团队作业9:Beta冲刺与团队项目验收

    实验十三 团队作业9:Beta冲刺与团队项目验收 实验时间 2019-6-20(21) Deadline: [6.20-6.26]之间任选连续3天的23:00,以团队随笔博文提交时间为准. 评分标准: ...

  2. 在ubuntu中用命令安装iperf

    Iperf的安装要换成阿里的源 sudo add-apt-repository "ppa:patrickdk/general-lucid" sudo apt-get update ...

  3. vector 下标操作

    比如:vector<int> ivec(3).. 当采用下标操作ivec[10]的时候,该操作是未定义的,在自己的机器上输出的值是零.建议使用迭代器进行操作.

  4. Silverlight日记:动态生成DataGrid、行列装换、动态加载控件

    本文主要针对使用DataGrid动态绑定数据对象,并实现行列转换效果. 一,前台绑定 <sdk:DataGrid x:Name="dataGrid2" Style=" ...

  5. 2d游戏中求出一个向量的两个垂直向量

    function cc.exports.VerticalVector(vec)--求出两个垂直向量 local result = {} result[1] = cc.p(vec.y/vec.x,-1) ...

  6. 修改broadcom 4322无线网卡ID教程,不再显示第三方无线网卡

    本帖最后由 hellokingabc 于 2016-1-11 03:07 编辑 黑苹果已经基本完美,但是无线网卡总是出现问题,经常断网,经过搜索,原因在于无线网卡在OSX系统下显示为第三方无线网卡,只 ...

  7. Confirm the Ending--freecodecamp算法题目

    Confirm the Ending(检查字符串结尾) 要求 判断一个字符串(str)是否以指定的字符串(target)结尾. 如果是,返回true;如果不是,返回false. 思路 利用.repla ...

  8. 【离线 线段树分治】bzoj4025: 二分图

    昨天mac的gdb挂了,今天怎么笔记本的gdb也挂了…… Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这 ...

  9. 【wqs二分 决策单调性】HHHOJ#261. Brew

    第一道决策单调性…… 题目描述 HHHOJ#261. Brew 题目分析 挺好的……模板题? 寄存了先. #include<bits/stdc++.h> typedef long long ...

  10. 转:模式窗口showModalDialog的用法总结

    模式窗口showModalDialog的用法总结   最近几天一直在处理模式窗口的问题,索性写了这篇总结,以供参考: 1.打开窗口:var handle = window.showModalDialo ...