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 ...
随机推荐
- 在 Windows 上使用 Python 进行 web 开发
本文由葡萄城技术团队于原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 上一篇我们介绍了在Windows 10下进行初学者入门开发Python的指 ...
- Where is the clone one and how to extract it?
One cannot be in two places at once. Do you know what's "Dual Apps"? Manufactures like Xia ...
- 刷脸即可解锁让iDevice取证不再难如登天
最近有则取证相关的消息,链接如下,光看标题便知道与Apple的Face ID有关. https://www.cnet.com/news/fbi-unlocked-an-iphone-x-by-forc ...
- 戴尔PowerEdge T110 Ⅱ服务器U盘安装Windows Server 2019 DataCenter
一. 下载准备 准备工作——下载Microsoft Windows Server 2019 官方简体中文激活版 (MSDN)原版iso镜像 准备工作——安装刻录软件UltraISO,单文件绿色版就够用 ...
- ZK安装、ZK配置、ZK集群部署
今天心血来潮,想搞一下zookeeper集群.具体步骤记录下吧~嘻嘻
- 记一次使用LR测试UDP和TCP的过程
背景 最近项目要做性能测试,要出要一份性能报告,让我出一个有关Tcp和Udp的功能模块的测试,流程大概是这样,先走TCP协议协商一下会话,协商成功后走Udp收发数据. 有点简单啊,自己写个功能模块测一 ...
- codeforces679A_Bear and Prime 100 交互题
传送门 第一道交互题 题意: 电脑事先想好了一个数[,] 你会每次问电脑一个数是否是它想的那个数的因数 电脑会告诉你yes或no 至多询问20次 最后要输出它想的数是质数还是合数 思路: 枚举< ...
- DDOS浅谈
一.DDOS攻击的来源 任何攻击都不会凭空产生,DDOS也有特定的来源.绝大多数的DDOS攻击都来自于僵尸网络.僵尸网络就是由数量庞大的可联网僵尸主机组成,而僵尸主机可以是任何电子设备(不仅是X86架 ...
- ZooKeeper实现生产-消费者队列
[欢迎关注公众号:程序猿讲故事 (codestory),及时接收最新文章] 生产-消费者队列,用于多节点的分布式数据结构,生产和消费数据.生产者创建一个数据对象,并放到队列中:消费者从队列中取出一个数 ...
- Django-内置用户系统
Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括 1.用户注册 2.用户登录 3.用户认证 4.注销 5.修改密码 Django作为一个 ...