【FZU
来源:互联网 发布:centos 7服务器版安装 编辑:程序博客网 时间:2024/06/02 03:06
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.
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.
For each test case:
If Kim can win in 2 steps, output “Kim win!”
Otherwise output “Cannot win!”
3. . .. . .. . .oo x oo . xx x oxo x .. o .. . xo
Cannot win!Kim win!Kim win!
题意:给出一个3x3的棋盘,两人用'x'和'o'下棋,三个棋子连成一条线就算赢,现在给出Kim的棋子样式,问Kim能否在两步之内取得胜利(若没有一步取胜,对手也会再下一步)。
分析:我们可以遍历棋盘上的点,若点为'.'则将其变为Kim的棋子进行一次判断,是否存在三子连珠,若存在则说明Kim可以赢得比赛。如果改变点后未形成三子连珠,则再次进行遍历,将'.'变为Kim的棋子,判断是否有两个以上的点符合三子连珠,若存在则Kim可以赢得比赛。
因为一步之后对手会下一步棋,如果只存在一种满足的情况,那么对手会将路堵死,所以要有两点以上满足条件。
代码如下:
#include <map>#include <cmath>#include <queue>#include <stack>#include <vector>#include <cstdio>#include <string>#include <cstring>#include <iostream>#include <algorithm>#define LL long long#define INF 0x3f3f3f3fusing namespace std;const int MX = 5;char mp[MX][MX];char op[5];char read(){ char c = getchar(); if(c == ' ' || c == '\n'){ c = getchar(); } return c;}int check(){ for(int i = 1; i <= 3; i++){ for(int j = 1; j <= 3; j++){ if(mp[i][j] == op[0]){ if(i+2 <= 3 && mp[i+1][j] == op[0] && mp[i+2][j] == op[0]) return 1; if(j+2 <= 3 && mp[i][j+1] == op[0] && mp[i][j+2] == op[0]) return 1; if(i+2 <= 3 && j+2 <= 3 && mp[i+1][j+1] == op[0] && mp[i+2][j+2] == op[0]) return 1; if(i+2 <= 3 && j-3 >= 0 && mp[i+1][j-i] == op[0] && mp[i+2][j-2] == op[0]) return 1; } } } return 0;}int main(){ int t; scanf("%d", &t); while(t--){ for(int i = 1; i <= 3; i++){ for(int j = 1; j <= 3; j++){ mp[i][j] = read(); } } scanf("%s", op); int flag = 0; for(int i = 1; i <= 3; i++){ for(int j = 1; j <= 3; j++){ if(mp[i][j] == '.'){ int cnt = 0; mp[i][j] = op[0]; if(check()){ flag = 1; break; } else{ for(int p = 1; p <= 3; p++){ for(int q = 1; q <= 3; q++){ if(mp[p][q] == '.'){ mp[p][q] = op[0]; if(check()){ cnt++; } mp[p][q] = '.'; } } } if(cnt >= 2){ flag = 1; break; } } mp[i][j] = '.'; } } } if(flag) puts("Kim win!"); else puts("Cannot win!"); } return 0;}
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- liunx 环境下 python模块numpy,scipy,matplotlib 安装
- iOS中多语言本地化流程的优化
- 文件内容的查阅
- jeecg如何在一个页面使用按钮选择弹出选择框
- 贪心算法题集总结
- 【FZU
- android在java代码中绘制矩形框
- 中缀式变后缀式
- Android Studio 如何导入aar包
- 安装latexdiff
- tf.nn.softmax_cross_entropy_with_logits()
- 如何用eclipse创建你的第一个servlet小程序
- JS--Array的常用方法map、reduce、filter、forEach、indexOf
- oracle biee 12c windows install