foj 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
. . .
. . .
. . .
o
o x o
o . x
x x o
x
o x .
. o .
. . x
o
Sample Output
Kim win!
Kim win!
#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的更多相关文章
- 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 ...
- 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 ...
- POJ 2361 Tic Tac Toe
题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...
- 【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 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- Epic - Tic Tac Toe
N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...
- python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- ACM-Team Tic Tac Toe
我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
随机推荐
- mysql中的空值问题
MySQL的查询如果需要用到空值的情况下,where后面的条件就需要注意了 MySQL中的表示空值的方法:is null 和 is not null 比如:select * from user whe ...
- JDK安装及环境变量配置详解
一.下载(Jdk 1.8 ) 1.下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 ...
- 关于removeFromSuperview
关于 - (void)removeFromSuperview 苹果官网API中是这么描述的: Unlinks the view from its superview and its window, ...
- JavaScript Dom编程艺术(1)
Dom是一种可以供多种环境和多种程序设计语言使用的API: 一份文档就是一个节点树: 节电分为不同的类型:元素节点,属性节点,文档节点,元素节点分为属性节点和文档节点: getelementbyid( ...
- 洛谷P2347 砝码称重
题目 貌似是某年提高组签到题,六重循环零压力AC,差点怒踩std 但本蒟蒻决定写正解——多重背包,果断20分 原因是写错了状态转移方程...神才知道我咋过的样例和两个测试点 扯远了 多重背包 简单说一 ...
- Python 建模步骤
#%% #载入数据 .查看相关信息 import pandas as pd import numpy as np from sklearn.preprocessing import LabelEnco ...
- octave-basic
在coursera上斯坦福的machine learning,lecturer极力推荐开源的编程环境Octave入手,所以我也下载了来试一试吧== 参考链接:http://www.linuxdiyf. ...
- Letters CodeForces - 978C (二分)
Time limit4000 ms Memory limit262144 kB There are nn dormitories in Berland State University, they a ...
- Linux学习-延伸正则表达式
grep 默认仅支持基础正则表达式,如果要使用延伸型正则 表达式,你可以使用 grep -E , 不过更建议直接使用 egrep !直接区分指令比较好记忆!其 实 egrep 与 grep -E 是类 ...
- 控制台窗口和powershell运行服务会卡住的解决办法
之前使用nodejs做了一个简单的web服务,通过控制台窗口运行,通过浏览器访问发现有时候浏览器等很久数据都加载不出来,以为是代码有问题,后来发现是控制台卡住了,按一下enter键就好了,当时百度了一 ...