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;
}
最新文章
- 【翻译二十】-java线程池
- 【项目经验】——JSON.parse() &;&; JSON.stringify()
- atitit.提升开发效率---使用服务器控件生命周期 asp.net 11个阶段 java jsf 的6个阶段比较
- javaScript一些函数--Math()
- REUSE_ALV_POPUP_TO_SELECT的使用技巧
- 小扩展大用处,自己扩展一个ForeachRead吧
- ASP.NET MVC 5改进了基于过滤器的身份验证
- [backbone] Getting Started with Backbone.js
- [转]Ubuntu 常用快捷键10个
- Spring的配置文件
- HttpWebRequest提高效率之连接数,代理,自动跳转,gzip请求等设置问题
- 读改善c#代码157个建议:建议4~6
- 使用ProgressDialog创建进度对话框
- sessionStorage 、localStorage 和 cookie
- 201671010142 2017-2 《java第十二十三章学习感悟》
- Spring详解(六)------AspectJ 实现AOP
- wamp 中安装cakephp Fatal error: You must enable the intl extension to use CakePHP. in XXX
- 基于django的自定义简单session功能
- Spring 学习——Spring AOP——AOP配置篇Aspect、Pointcut
- Vue + Element UI 实现权限管理系统(更换皮肤主题)
热门文章
- 【iOS】获取项目名和版本号
- Mac 使用小结
- Unity经典游戏教程之:雪人兄弟
- 已知词频生成词云图(数据库到生成词云)--generate_from_frequencies(WordCloud)
- Spring Cloud 相关资料链接
- Spring AOP JDK动态代理与CGLib动态代理区别
- [JavaScript] 节流(throttle)-防抖(debounce) 不懵圈指北
- python学习笔记(3)--函数、参数、变量、递归
- Java 从入门到进阶之路(二)
- idea快速生成实体类