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 的网格上玩井字棋. 井字棋游戏的规则如下: ...
随机推荐
- Maven搭建Struts2+Spring3+Hibernate4框架
做了三年多的JavaEE开发了,在平时的JavaEE开发中,为了能够用最快的速度开发项目,一般都会选择使用Struts2,SpringMVC,Spring,Hibernate,MyBatis这些开源框 ...
- Luogu [P3367] 模板 并查集
[模板]并查集 题目详见:[[P3367][模板]并查集] (https://www.luogu.org/problemnew/show/P3367) 这是一道裸的并查集题目(要不然叫模板呢) 废话不 ...
- [vijos]P1514 天才的记忆
背景 神仙飞啊飞 描述 从前有个人名叫W and N and B,他有着天才般的记忆力,他珍藏了许多许多的宝藏.在他离世之后留给后人一个难题(专门考验记忆力的啊!),如果谁能轻松回答出这个问题,便可以 ...
- 二十一、C++中的临时对象
思考: 构造函数是一个特殊的函数 是否可以直接调用? 是否可以在构造函数中调用构造函数? 直接调用构造函数的行为是什么? 答: 直接调用构造函数将产生一个临时对象 临时对象的生命周期只有一条语句的时间 ...
- PHP计算两个日期相差的年月日时分秒
$start_time = '2017-09-06 15:12:20'; $end_time = '2018-09-08 10:20:45'; get_time($start_time,$end_ti ...
- 16Shell脚本—计划任务服务程序
计划任务服务程序 尽管我们现在已近有了功能彪悍的脚本程序来执行一些批处理工作,但是,如果仍然需要每天凌晨两点敲击键盘回车键来执行这个脚本程序,这简直太痛苦了.为此,我们需要学习如何设置服务器的计划任务 ...
- python计算机基础(三)
简述Python垃圾回收机制: 当x=10,赋值x=11,的代码,也就是10没有对应的变量名, 10在python眼中相当于垃圾,就会被清理掉,释放内存. 对于下述代码: x = 10 y = 10 ...
- 20个必不可少的Python库也是基本的第三方库
个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都应该有它. Scrapy. ...
- BZOJ 5064: B-number
数位DP #include<cstdio> #include<cstring> using namespace std; int A[16]; long long F[16][ ...
- Python并发(一)
假设我们要从一个网站用Python程序下载5张图片,最传统的思路就是写个for循环挨个挨个下载,但是这样做有个缺点,就是除了第一张,每张图片都必须等待前一张图片下载完毕后,才可以开始下载.由于网络有很 ...