Tic-Tac-Toe (第八届福建省赛)
来源:互联网 发布:dnf辅助官网源码 编辑:程序博客网 时间:2024/04/29 17:38
Accept: 2 Submit: 3
Time Limit: 1000 mSec Memory Limit : 262144 KB
Problem Description
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
Sample Output
Source
第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)
一开始觉得太麻烦了,所以没去写这道题,结果比赛完后,回顾了一下,发现就是一道水题,暴力搜索就好了,只有3*3的格子;
发现还是缺乏经验,水题都发现不了。
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>using namespace std;char map[4][4];int sum;int flag;int ans;char a,b;int isok(char c) { for(int i=0; i<3; i++) { if(map[0][i]==c&&map[1][i]==c&&map[2][i]==c) return 1; //行列 if(map[i][0]==c&&map[i][1]==c&&map[i][2]==c) return 1; } if(map[0][0]==c&&map[1][1]==c&&map[2][2]==c) return 1; if(map[2][0]==c&&map[1][1]==c&&map[0][2]==c) return 1; //对角线 return 0;}void dfs() { ans=0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(map[i][j]!='.') continue; map[i][j]=a; if(isok(a)) ans++; map[i][j]='.'; if(ans>=2) return; //有超过两个地方可以让A赢,则A必赢 } }}int main() { int T; scanf("%d",&T); while(T--) { flag=0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { scanf("%s",&map[i][j]); } getchar(); } scanf("%c",&a); if(a=='o') b='x'; else b='o'; //情况一;一开始便已经可以赢了; if(isok(a)) { printf("Kim win!\n"); continue; } //情况二:只差一位就可以赢 for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(map[i][j]!='.') continue; map[i][j]=a; if(isok(a)) { flag=1; break; } map[i][j]='.'; } if(flag) break; } if(flag) { printf("Kim win!\n"); continue; } // 情况三;b只差一步就可以赢,所以要封锁这个位置,然后判断之后是否有超过两个位置可以赢,有则可以赢; ans=0; for(int i=0; i<3; i++) for(int j=0; j<3; j++) { if(map[i][j]!='.') continue; map[i][j]=b; if(isok(b)) { flag=1; map[i][j]=a; dfs(); if(ans>=2) flag=2; map[i][j]='.'; } map[i][j]='.'; } if(flag) { if(flag==2) { printf("Kim win!\n"); } else printf("Cannot win!\n"); continue; } //不是以上特殊情况,则直接搜索,看有没有能练成线的; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { ans=0; if(map[i][j]!='.') continue; else { map[i][j]=a; dfs(); map[i][j]='.'; } if(ans>=2) break; } if(ans>=2) break; } if(ans>=2) printf("Kim win!\n"); else printf("Cannot win!\n"); } return 0;}
- Tic-Tac-Toe (第八届福建省赛)
- 第八届福建省大学生程序设计竞赛-重现赛 L Tic-Tac-Toe
- FOJ Problem 2283 Tic-Tac-Toe(暴力枚举)——第八届福建省大学生程序设计竞赛-重现赛
- 第八届福建省大学生程序设计竞赛 L.Tic-Tac-Toe【思维+暴力枚举】
- FZU2283+Tic-Tac-Toe+【思维+暴力枚举】+ 第八届福建省大学生程序设计竞赛
- 2017ACM福建省赛FZU2283 Tic-Tac-Toe
- 第八届福建大学生程序设计竞赛-L 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
- FZU2283-Tic-Tac-Toe
- tic-tac-toe游戏
- wait 和notify的使用
- VS2013 MFC 如何浏览文件并将路径显示到编辑框?
- G
- Scala学习—函数式编程之集合操作
- lintcode -- 罗马数字转整数
- Tic-Tac-Toe (第八届福建省赛)
- TensorFlow与中文手写汉字识别
- Servlet的生命周期
- PTA基础题目5-1 厘米换算英尺英寸 (15分)
- 多线程——创建线程方法之一(继承Thread类)
- 图像处理(2)空间滤波
- Java final关键字
- cnn:深入理解卷积网络
- Android零基础入门第12节:熟悉Android Studio界面,开始装逼卖萌