๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

3 X 3 ๋งต์—์„œ ๋จผ์ € ๊ฐ€๋กœ ํ˜น์€ ์„ธ๋กœ ํ˜น์€ ๋Œ€๊ฐ์„  3๊ฐœ์˜ ๋ผ์ธ์„ ๋งŒ๋“œ๋Š” ์‚ฌ๋žŒ์ด ์ด๊ธฐ๋Š” ๊ฒŒ์ž„์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ด๊ฒผ๋Š”๊ฐ€ ์กŒ๋Š”๊ฐ€๋ฅผ ๋ณด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ฒŒ์ž„์„ ์ง„ํ–‰ํ–ˆ๋Š”๊ฐ€๋ฅผ ๋ณด๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‚ฌํ•ญ๋“ค์„ ์ฒดํฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

  • ๋ฒˆ๊ฐˆ์•„ ๊ฐ€๋ฉด์„œ ์ง„ํ–‰ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์–ด๋Š ํ•œ ์ชฝ์ด 2๋ฒˆ ์—ฐ์† ์ง„ํ–‰ํ•œ ๊ฒฝ์šฐ ( | 'O'์˜ ๊ฐœ์ˆ˜ - 'X'์˜ ๊ฐœ์ˆ˜| > 1)
  • ์„ ๊ณต์ด ๋จผ์ € ๋†”์•ผ ํ•˜๋Š”๋ฐ, ํ›„๊ณต์ด ๋จผ์ € ๋†“์€ ๊ฒฝ์šฐ ( 'O'์˜ ๊ฐœ์ˆ˜ < 'X'์˜ ๊ฐœ์ˆ˜ )
  • ๋‘˜ ๋‹ค ์ด๊ฒผ๋‹ค๊ณ  ํŒ์ •ํ•  ๊ฒฝ์šฐ ( 'O' 3๊ฐœ๋กœ ํ•œ ๋ผ์ธ์„ ๋งŒ๋“ค๊ณ , 'X' 3๊ฐœ๋กœ๋„ ํ•œ ๋ผ์ธ์„ ๋งŒ๋“  ๊ฒฝ์šฐ)
  • ์„ ๊ณต์ด ์ด๊ฒผ์ง€๋งŒ, ํ›„๊ณต์ด ๊ณ„์† ์ด์–ด ๋‚˜๊ฐ€๋Š” ๊ฒฝ์šฐ ( 'O'์˜ ๊ฐœ์ˆ˜ - 'X'์˜ ๊ฐœ์ˆ˜ <= 0)
    • ์ •์ƒ์ ์ธ ๊ฒŒ์ž„์—์„œ ์„ ๊ณต์ด ์ด๊ฒผ์„ ๊ฒฝ์šฐ์—๋Š” 'O'๊ฐ€ 'X'๋ณด๋‹ค 1๊ฐœ๊ฐ€ ๋” ๋งŽ๋‹ค.
  • ํ›„๊ณต์ด ์ด๊ฒผ์ง€๋งŒ, ์„ ๊ณต์ด ๊ณ„์† ์ด์–ด๋‚˜๊ฐ€๋Š” ๊ฒฝ์šฐ ( 'O'์˜ ๊ฐœ์ˆ˜ == 'X'์˜ ๊ฐœ์ˆ˜)
    • ์ •์ƒ์ ์ธ ๊ฒŒ์ž„์—์„œ ํ›„๊ณต์ด ์ด๊ฒผ์„ ๊ฒฝ์šฐ์—๋Š” 'O'์™€ 'X'์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค.

 

์œ„์™€ ๊ฐ™์€ ์‚ฌํ•ญ๋“ค ์ค‘ ํ•˜๋‚˜๋ผ๋„ ํ•ด๋‹น๋œ๋‹ค๋ฉด ๊ทธ ๊ฒŒ์ž„์€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒŒ์ž„์ด๋ฏ€๋กœ 0์„ ๋ฆฌํ„ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ์‚ฌํ•ญ๋“ค์„ ๋ณด๋ฉด 'O'์™€ 'X'์˜ ๊ฐœ์ˆ˜๋กœ๋งŒ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋Š” ํ•ญ๋ชฉ๋“ค๋„ ์žˆ์ง€๋งŒ, ์„ ๊ณต์ด ์ด๊ฒผ๋Š”๊ฐ€์™€ ํ›„๊ณต์ด ์ด๊ฒผ๋Š”๊ฐ€์— ๋Œ€ํ•ด์„œ๋Š” 3๊ฐœ์˜ ์ ์ด ์ด์–ด์ง„ ๋ผ์ธ์ธ์ง€๋ฅผ ์ฒดํฌํ•˜๋Š” ๋กœ์ง์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๊ทธ ๋ถ€๋ถ„์„ ์ž‘์„ฑํ•œ ํ›„ ์œ„ ์กฐ๊ฑด๋“ค์„ ์ž˜ ์ฒดํฌํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <string>
#include <vector>
using namespace std;
using Position = pair<int, int>;
const int BOARD_LENGTH = 3;
// ์ขŒ์šฐ, ์ƒํ•˜, y=-x ๋Œ€๊ฐ๋ผ์ธ, y=x ๋Œ€๊ฐ๋ผ์ธ
vector<vector<Position>> positionDirections { {{0, -1}, { 0, 1 }}, { {-1, 0}, { 1, 0} }, { {-1, -1}, { 1, 1} }, { {1, -1}, {-1, 1} } };

pair<vector<Position>, vector<Position>> GetPositions(const vector<string>& board) {
	vector<Position> firster;
	vector<Position> seconder;

	for (int row = 0; row < BOARD_LENGTH; row++) {
		for (int col = 0; col < BOARD_LENGTH; col++) {
			if (board[row][col] == 'O')
				firster.emplace_back(row, col);
			else if (board[row][col] == 'X')
				seconder.emplace_back(row, col);
		}
	}
	return make_pair(firster, seconder);
}

bool IsWin(const vector<string>& board, const vector<Position>& positions, char target) {
	for (const auto position : positions) {
		int currentRow = position.first;
		int currentCol = position.second;

		for (const auto& directions : positionDirections) {
			bool isWin = true;

			for (const auto& direction : directions) {
				int nextRow = currentRow + direction.first;
				int nextCol = currentCol + direction.second;

				if (nextRow < 0 or nextRow >= BOARD_LENGTH or nextCol < 0 or nextCol >= BOARD_LENGTH or board[nextRow][nextCol] != target)
					isWin = false;
			}

			if (isWin)
				return true;
		}
	}

	return false;
}

int solution(vector<string> board) {
	auto positions = GetPositions(board);
	vector<Position> firster = positions.first;
	vector<Position> seconder = positions.second;

	if (abs((int)firster.size() - (int)seconder.size()) > 1)
		return false;

	if (firster.size() < seconder.size())
		return false;

	bool isFirsterWin = IsWin(board, firster, 'O');
	bool isSeconderWin = IsWin(board, seconder, 'X');

	if (isFirsterWin and isSeconderWin)
		return false;

	if (isFirsterWin and firster.size() - seconder.size() <= 0)
		return false;

	if (isSeconderWin and firster.size() != seconder.size())
		return false;

	return true;
}

 

 

728x90
๋ฐ˜์‘ํ˜•