Codeforces Round #390 (Div. 2)B Ilya and tic-tac-toe game

題目大意:

      兩個人走三子棋問下一步是否能贏。

題目解法:

      枚舉所有情況就行了,反正在邊上的就8種,在中間的4種。

代碼:

#include "iostream"
#include "algorithm"
#include "cstdio"
#include "math.h"
#include "string"
#include "string.h"
using namespace std;
int go[2][4] = { {-1,0,1,0},{0,1,0,-1} };
char mp[10][10];
bool judge(int x, int y) {
	if (mp[x - 1][y] == 'x'&&mp[x - 2][y] == 'x') {
		return true;
	}
	if (mp[x + 1][y] == 'x'&&mp[x + 2][y] == 'x') {
		return true;
	}
	if (mp[x][y - 1] == 'x'&&mp[x][y - 2] == 'x') {
		return true;
	}
	if (mp[x][y + 1] == 'x'&&mp[x][y + 2] == 'x') {
		return true;
	}
	if (mp[x + 1][y + 1] == 'x'&&mp[x + 2][y + 2] == 'x') {
		return true;
	}
	if (mp[x - 1][y - 1] == 'x'&&mp[x - 2][y - 2] == 'x') {
		return true;
	}
	if (mp[x + 1][y - 1] == 'x'&&mp[x + 2][y - 2] == 'x') {
		return true;
	}
	if (mp[x - 1][y + 1] == 'x'&&mp[x - 2][y + 2] == 'x') {
		return true;
	}
	return false;
}
bool judge2(int x, int y) {
	if (mp[x - 1][y] == 'x'&&mp[x + 1][y] == 'x') {
		return true;
	}
	if (mp[x][y - 1] == 'x'&&mp[x][y + 1] == 'x') {
		return true;
	}
	if (mp[x + 1][y + 1] == 'x'&&mp[x - 1][y - 1] == 'x') {
		return true;
	}
	if (mp[x + 1][y - 1] == 'x'&&mp[x - 1][y + 1] == 'x') {
		return true;
	}
	return false;
}
int main()
{
	for (int i = 2;i <= 5;i++) {
		for (int j = 2;j <= 5;j++) {
			scanf(" %c", &mp[i][j]);
		}
	}
	bool flag = false;
	for (int i = 2;i <= 5;i++) {
		for (int j = 2;j <= 5;j++) {
			if (mp[i][j] == 'x') {
				if (judge(i, j)) {
					flag = true;
					break;
				}
			}
		}
		if (flag)
			break;
	}
	if (!flag) {
		for (int i = 2;i <= 5;i++) {
			for (int j = 2;j <= 5;j++) {
				if (mp[i][j] == '.') {
					if (judge2(i, j)||judge(i,j)) {
						flag = true;
						break;
					}
				}
			}
			if (flag)
				break;
		}
	}
	if (flag)
		puts("YES");
	else
		puts("NO");
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章