FZU2283-Tic-Tac-Toe
来源:互联网 发布:数据分析师职业规划 编辑:程序博客网 时间:2024/05/16 10:14
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先下,问三步之内能否获胜。
解题思路: dfs搜索,参考了斌神的代码,简洁清晰~
#include<iostream>#include<cstdio>#include<cmath>#include<vector>#include<map>#include<algorithm>using namespace std;const int MAXN=1e5+5;int mat[5][5];int check(){ for(int i=1;i<=3;i++) if(mat[i][1]&&mat[i][1]==mat[i][2]&&mat[i][2]==mat[i][3]) return mat[i][1]; for(int j=1;j<=3;j++) if(mat[1][j]&&mat[1][j]==mat[2][j]&&mat[2][j]==mat[3][j]) return mat[1][j]; if(mat[1][1]&&mat[1][1]==mat[2][2]&&mat[2][2]==mat[3][3]) return mat[1][1]; if(mat[1][3]&&mat[1][3]==mat[2][2]&&mat[2][2]==mat[3][1]) return mat[1][3]; return 0;}int dfs(int er,int step){ int tmp=check();if(tmp!=0) return tmp; if(step==4) return 0; bool tie=false; for(int i=1;i<=3;i++) { for(int j=1;j<=3;j++) { if(mat[i][j]==0) { mat[i][j]=er; int ans=dfs(3-er,step+1); mat[i][j]=0; if(ans==er) return er; if(ans==0) tie=true; } } } if(tie) return 0; return 3-er;}int main(){ int T; scanf("%d",&T); while(T--) { char ch[3]; for(int i=1;i<=3;i++) { for(int j=1;j<=3;j++) { scanf("%s",&ch); if(ch[0]=='.') mat[i][j]=0; else if(ch[0]=='o') mat[i][j]=1; else mat[i][j]=2; } } char op[3]; scanf("%s",op); int er; if(op[0]=='o') er=1; else er=2; if(dfs(er,1)==er) printf("Kim win!\n"); else printf("Cannot win!\n"); } return 0;}/*3. . .. . .. . .oo x oo . xx x oxo x .. o .. . xo*/
- FZU2283-Tic-Tac-Toe
- 2017ACM福建省赛FZU2283 Tic-Tac-Toe
- FZU2283+Tic-Tac-Toe+【思维+暴力枚举】+ 第八届福建省大学生程序设计竞赛
- Tic-Tac-Toe
- [cf]Tic-tac-toe
- C - Tic-tac-toe
- Minimax-Tic Tac Toe
- Design Tic-Tac-Toe
- Design Tic-Tac-Toe
- Design Tic-Tac-Toe
- FZU Tic-Tac-Toe
- Tic-Tac-Toe FZU
- tic-tac-toe游戏
- Analysis On Tic-Tac-Toe
- poj 3075 Tic-Tac-Toe
- checkio (tic-tac-toe)
- AOJ 0066 Tic Tac Toe
- UVa 10363 - Tic Tac Toe
- 传递函数依赖
- Codeforces Round #411 D. Minimum number of steps (贪心。)
- Android(root)设备HTTPS请求时间校准
- Eclipse中安装Hibernate的插件
- kafka + spark streaming 实时读取计算 nginx 日志,存储结果到 mongodb/mysql
- FZU2283-Tic-Tac-Toe
- Gradle 中 buildConfigField的巧妙应用
- 2016-2017年终总结-特遣人生
- 浅谈memmove 和 memcpy的区别
- 原码反码补码的详解以及java中>>和>>>的区别
- 存储过程学习笔记(SQL数据库)
- Guess UVA
- 动归————奶牛浴场
- hpuoj 1187: 영어