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 ...
随机推荐
- iOS 注释
1) 参数的注释: UIButton *btnSend;/**< 发送按钮 */ 效果: 2) 方法的注释: type1(无参数): /** table 相关设置 */ -(void)confi ...
- L4170[CQOI2007]涂色
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i <= b; ...
- The philosophy of ranking
In the book Decision Quality, one will be trained to have three decision making system; one of them ...
- Java并发编程实战笔记—— 并发编程1
1.如何创建并运行java线程 创建一个线程可以继承java的Thread类,或者实现Runnabe接口. public class thread { static class MyThread1 e ...
- Opengl_入门学习分享和记录_02_渲染管线(一)顶点输入
现在前面的废话:最近好事不断!十分开心!生活真美好! 好了今天要梳理一下,顶点输入的具体过程,同样也是渲染管线中的第一个阶段的详细过程的介绍.之前介绍过,OpenGL操作的是一组3D坐标,所以我们的输 ...
- 想转行大数据,开始学习 Hadoop?
学习大数据首先要了解大数据的学习路线,首先搞清楚先学什么,再学什么,大的学习框架知道了,剩下的就是一步一个脚印踏踏实实从最基础的开始学起. 这里给大家普及一下学习路线:hadoop生态圈——Strom ...
- 鲜为人知的maven标签解说
目录 localRepository interactiveMode offline pluginGroups proxies servers mirrors profiles 使用场景 出现位置 激 ...
- windows server2012 nVME和网卡等驱动和不识别RAID10问题
安装2012---不识别M.2 nVME,下官方驱动,注入到系统里 缺多驱动---用ITSK万能驱动添加:|Win8012R2.x64(可解决不支持操作系统,win10与server2012R2通用) ...
- thinkPHP 获得当前请求的全部常量信息
tp框架提供了常量: http://网址/shop/index.php/分组/控制器/操作方法/名称1/值/名称2/值 __MODULE__: 路由地址分组信息 (/shop/index.php/分组 ...
- net core Webapi基础工程搭建(五)——缓存机制
目录 前言 Cache Session Cookie 小结 补充 前言 作为WebApi接口工程,性能效率是必不可少的,每次的访问请求,数据库读取,业务逻辑处理都或多或少耗费时间,偶尔再来个各种花式f ...