Codeforces Round #390 (Div. 2) B. Ilya and tic-tac-toe game(dfs)
来源:互联网 发布:2009年nba总决赛数据 编辑:程序博客网 时间:2024/06/05 11:06
Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. He played a lot of games today with his friend Arseny. The friends became tired and didn't finish the last game. It was Ilya's turn in the game when they left it. Determine whether Ilya could have won the game by making single turn or not.
The rules of tic-tac-toe on the 4 × 4 field are as follows. Before the first turn all the field cells are empty. The two players take turns placing their signs into empty cells (the first player places Xs, the second player places Os). The player who places Xs goes first, the another one goes second. The winner is the player who first getsthree of his signs in a row next to each other (horizontal, vertical or diagonal).
The tic-tac-toe position is given in four lines.
Each of these lines contains four characters. Each character is '.' (empty cell), 'x' (lowercase English letterx), or 'o' (lowercase English lettero). It is guaranteed that the position is reachable playing tic-tac-toe, and it is Ilya's turn now (in particular, it means that the game is not finished). It is possible that all the cells are empty, it means that the friends left without making single turn.
Print single line: "YES" in case Ilya could have won by making single turn, and "NO" otherwise.
xx...oo.x...oox.
YES
x.oxox..x.o.oo.x
NO
x..x..ooo...x.xo
YES
o.x.o....x..ooxx
NO
In the first example Ilya had two winning moves: to the empty cell in the left column and to the leftmost empty cell in the first row.
In the second example it wasn't possible to win by making single turn.
In the third example Ilya could have won by placing X in the last row between two existing Xs.
In the fourth example it wasn't possible to win by making single turn.
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fd(i,a,b) for(int i=a;i>=b;i--)#define inf 0x3f3f3f3f#define ll long long#define mem(a,b) memset(a,b,sizeof(a))using namespace std;int fx[8]= {1,-1,0,0,1,-1,1,-1};int fy[8]= {0,0,1,-1,1,-1,-1,1};//八个方向char map[101][101];int flag;int dfs(){ for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { if(map[i][j]=='x')//如果遇见了x就看八个方向上有没有满足条件的 { for(int k=0; k<8; k++) { flag=0; for(int z=1; z<=2; z++)//同一个方向的两个坐标的位置,如果都为x的话,则证明满足条件 { int fxx=i+fx[k]*z;//fxx,fyy分别表示与x在同一条直线的坐标, int fyy=j+fy[k]*z; if(fxx>=1&&fyy<=4&&fyy>=1&&fxx<=4) { if(map[fxx][fyy]=='x') continue; else flag=1; } else flag=1; } if(flag==0)//满足条件返回1 return 1; } } } } return 0;}int main(){ //mem(map,'.'); for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { scanf("%c",&map[i][j]); } getchar(); } int ok=0; for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { //printf("%c",map[i][j]); if(map[i][j]=='.') { map[i][j]='x';//首先将空白变成x,进行搜索 int temp=dfs(); map[i][j]='.';//变回原先的数,以免影响下面的数据 if(temp==1)//如果等一则证明存在这种情况。 { ok=1; } } } //printf("\n"); if(ok==1) break; } if(ok==1)//输出结果即可 printf("YES\n"); else printf("NO\n");}
- Codeforces Round #390 (Div. 2) B. Ilya and tic-tac-toe game(dfs)
- Codeforces #390 (Div. 2) B. Ilya and tic-tac-toe game ( DFS
- Codeforces Round #390(Div. 2)B. Ilya and tic-tac-toe game【模拟】
- Codeforces Round #390 (Div. 2)-B Ilya and tic-tac-toe game(模拟)
- Codeforces Round #390 (Div. 2)B Ilya and tic-tac-toe game
- 【Codeforces 754 B Ilya and tic-tac-toe game】
- 【codeforces 754B】 Ilya and tic-tac-toe game
- codeforces 754 B Ilya and tic-tac-toe game
- CodeForces 754B Ilya and tic-tac-toe game
- B. Ilya and tic-tac-toe game
- Codeforces-754B-Ilya and tic-tac-toe game(简单模拟判断)
- Cdoeforces 754 B - Ilya and tic-tac-toe game
- 754B Ilya and tic-tac-toe game
- 754B. Ilya and tic-tac-toe game
- 2017寒假集训-小题训练1:F - Ilya and tic-tac-toe game
- 001 tic-tac-toe game
- [Codeforces]C. Tic-tac-toe
- Codeforces Beta Round #3C. Tic-tac-toe
- 用dos命令轻松删除Windows7系统中的垃圾文件
- App内存占用优化
- JZOJ 4936【NOIP2017GDKOI模拟1.12】c
- MairDB更新/删除数据(三)
- Flume学习笔记 --- 模拟流式数据产生数据
- Codeforces Round #390 (Div. 2) B. Ilya and tic-tac-toe game(dfs)
- rabbitmq的模型
- Ubuntu安装MYSQL数据库以及数据知识点
- 数据库操作命令
- 面试
- amp.cmd
- leetcode9. Palindrome Number
- vsftpd 设置
- Centos7+Nginx通过windows CA颁发及配置SSL服务