2017福建省赛 L Tic-Tac-Toe 模拟
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两步之内是否可以获胜
分析:1.枚举每个Kim可以下棋的地方
2.首先看Kim下了这部棋后是否阻住了已经有两颗棋的对方
3.然后再看Kim下了这部棋后是否可以获胜,获胜的状态有两种,一种是三棋相连直接获胜,一种是这部棋后我有两个地方可以下棋子构成三子相连
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 10;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const ll inf = 1e9;
const double pi = acos(-1.0);
char mp[maxn][maxn];
bool check( char c ) {
if(mp[1][1]==c&&mp[1][1]==mp[1][2]&&mp[1][1]==mp[1][3]) return true;
if(mp[2][1]==c&&mp[2][1]==mp[2][2]&&mp[2][1]==mp[2][3]) return true;
if(mp[3][1]==c&&mp[3][1]==mp[3][2]&&mp[3][1]==mp[3][3]) return true;
if(mp[1][1]==c&&mp[1][1]==mp[2][1]&&mp[1][1]==mp[3][1]) return true;
if(mp[1][2]==c&&mp[1][2]==mp[2][2]&&mp[1][2]==mp[3][2]) return true;
if(mp[1][3]==c&&mp[1][3]==mp[2][3]&&mp[1][3]==mp[3][3]) return true;
if(mp[1][1]==c&&mp[1][1]==mp[2][2]&&mp[1][1]==mp[3][3]) return true;
if(mp[1][3]==c&&mp[1][3]==mp[2][2]&&mp[1][3]==mp[3][1]) return true;
return false;
}
bool ok( char c ) {
if( check(c) ) { //是否构成三子相连
return true;
}
ll cnt = 0;
//是否有两个地方可以再下一颗棋子构成三子相连
for( ll i = 1; i <= 3; i ++ ) {
for( ll j = 1; j <= 3; j ++ ) {
if( mp[i][j] == '.' ) {
mp[i][j] = c;
if( check(c) ) {
cnt ++;
}
mp[i][j] = '.';
}
}
}
if( cnt >= 2 ) {
return true;
}
return false;
}
int main() {
ll T;
cin >> T;
while( T -- ) {
for( ll i = 1; i <= 3; i ++ ) {
for( ll j = 1; j <= 3; j ++ ) {
cin >> mp[i][j];
}
}
char c1, c2;
cin >> c1;
if( c1 == 'x' ) {
c2 = 'o';
} else {
c2 = 'x';
}
bool flag = false;
for( ll i = 1; i <= 3; i ++ ) {
for( ll j = 1; j <= 3; j ++ ) {
if( mp[i][j] == '.' ) {
mp[i][j] = c1;
if( !ok(c2) && ok(c1) ) {
flag = true;
}
mp[i][j] = '.';
}
}
}
if( flag ) {
cout << "Kim win!" << endl;
} else {
cout << "Cannot win!" << endl;
}
}
return 0;
}
2017福建省赛 L Tic-Tac-Toe 模拟的更多相关文章
- 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 ...
- 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 ...
- python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- 2017福建省赛 FZU2272~2283
1.FZU2272 Frog 传送门:http://acm.fzu.edu.cn/problem.php?pid=2272 题意:鸡兔同笼通解 题解:解一个方程组直接输出就行 代码如下: #inclu ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
- [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 ...
随机推荐
- 【Intellij IDEA】设置 jdk 版本
File -> Project Structure... -> Project,如图所示:
- Java基础之二十 并发
20.1 并发得多面性 并发编程令人困惑的一个主要原因:使用并发时需要解决的问题有多个,而实现并发的方法也有多种,并且在这两者之间没有明显的映射关系. 20.1.1 更快的执行 速度问题初听起来很简单 ...
- JS节流和防抖函数
一. 实现一个节流函数 // 思路:在规定时间内只触发一次function throttle (fn, delay) { // 利用闭包保存时间 let prev = Date.now() re ...
- webupload项目中使用
目前项目需要一个多图上传的功能,使用LayUI并也是可以实现多图上传的,但是没有图片删除功能,参考了一下网上多图上传的插件,选择了WebUpload进行功能开发. 然而不幸的是,官方的插件并不带UI界 ...
- python自动化测试框架unittest
对于刚学习python自动化测试的小伙伴来说,unittest是一个非常适合的框架: 通过unittest,可以管理测试用例的执行,自动生成简单的自动化测试报告: 首先我们尝试编写编写一个最简单的un ...
- 初识JavaScript和面向对象
1.javascript基本数据类型: number: 数值类型 string: 字符串类型 boolean: 布尔类型 null: 空类型 undefault:未定义类型 object: 基本数据类 ...
- struts与springmvc有何区别
Struts2与SpringMVC有何区别? (1)SpringMVC的核心控制器是基于servlet技术,而Struts2是基于filter. (2)Struts2是类级别的拦截, 一个类对应一个r ...
- Java一个简单的文件工具集
class FileUtils { //文件目录下文件总数目 public static int fileNumber(File dir) { int filenumber = 0; if(dir.e ...
- ArcGIS数据格式详解
- json操作与使用 小白
json使用广可以和很多语言进行互换,把json序列化成字符串,可以反序列化回去 dumps(传入的类型,'ensure_ascii=False') loads网络传输 dump load文件写读 p ...