一道4x4的井字棋——寒假训练week1 div3

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 gets three of his signs in a row next to each other (horizontal, vertical or diagonal).

Input
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 letter x), or ‘o’ (lowercase English letter o). 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.

Output
Print single line: “YES” in case Ilya could have won by making single turn, and “NO” otherwise.

Examples
Input
xx…
.oo.
x…
oox.
Output
YES
Input
x.ox
ox…
x.o.
oo.x
Output
NO
Input
x…x
…oo
o…
x.xo
Output
YES
Input
o.x.
o…
.x…
ooxx
Output
NO
Note
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.

题目链接 https://vjudge.net/contest/278632#problem/C

题目大意

在4x4的格子里下棋,当行、列、斜存在三个连续的棋子时,则胜利。现在轮到X下,问能否一步取得胜利,能就输出YES,不能输出NO。其中 .(点)代表未下的格子。

数据范围

4x4的数据,很明显可以暴力

解题思路

  1. 对所有三个相连的行、列、对角线的X和点进行计数,满足存在x==2&&点=1,为YES,否则为NO。
    (优点 复杂度小,但是感觉太难写了,代码很长,写一半放弃了)

  2. 读入数据后,直接两层for循环遍历非O点,满足条件输出结果,跳出循环。
    (优点 复杂度更小,但是满足的条件很多,很多if容易写漏)

  3. 写check函数,全局变量不用传值,判断三连子就行,bool型函数正确输出YES,否则输出NO。主函数里将每个点改为x再用check判断一下,记得还要改回来。
    (缺点 复杂度感觉太高了,但是思路简单,代码好写)
    在判断副对角线的时候错了几发,以后要留意

解决代码

 #include
char a[4][4];	
bool check(){
//判断列
for(int i = 0;i <= 1;i++){
	for(int j = 0;j <= 3;j++){
		if(a[i][j] =='x'&&a[i + 1][j] == 'x'&&a[i + 2][j] == 'x') {
			return 1;
			}
		}
	}
//判断行
for(int i = 0;i <= 3;i++){
	for(int j = 0;j <= 1;j++){
		if(a[i][j] =='x'&&a[i][j + 1] == 'x'&&a[i][j + 2] == 'x'){
			return 1;
			}
		}
	}
//判断3x3主对角线
for(int i = 0;i <= 1;i++){
	for(int j = 0;j <= 1;j++){
		if(a[i][j] == 'x'&&a[i + 1][j + 1] == 'x'&&a[i + 2][j + 2] == 'x'){
			return 1;
			}
		}
	}
//判断3x3副对角线
for(int i = 0; i <= 1;i++){
	for(int j = 2; j<=3;j++){
		if(a[i][j] == 'x'&&a[i + 1][j - 1] =='x'&&a[i + 2][j - 2] == 'x'){
			return 1;
			}
		}
	}
	return 0;
}
int main(){
int ans = 0;
for(int i = 0;i <= 3;i++){
	scanf("%s",a[i]);
	}
	for(int i = 0;i <= 3;i++)
	{
		for(int j = 0;j <= 3;j++){
			if(a[i][j] == '.'){
				a[i][j] = 'x';
				if(check()) {
					ans = 1;
					break;
					}
				a[i][j] = '.';
				}
			}
		}
		if(ans) printf("YES");
		else printf("NO");
	return 0;
}

你可能感兴趣的:(入门级别)