๐Ÿ™‡โ€โ™€๏ธ[Gold I] ๋น„์ˆ - 1799

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2020 KB, ์‹œ๊ฐ„: 44 ms

๋ถ„๋ฅ˜

๋ฐฑํŠธ๋ž˜ํ‚น

์ œ์ถœ ์ผ์ž

2024๋…„ 2์›” 24์ผ 19:41:50

๋ฌธ์ œ ์„ค๋ช…

์„œ์–‘ ์žฅ๊ธฐ์ธ ์ฒด์Šค์—๋Š” ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๋น„์ˆ(bishop)์ด ์žˆ๋‹ค. < ๊ทธ๋ฆผ 1 >๊ณผ ๊ฐ™์€ ์ •์‚ฌ๊ฐํ˜• ์ฒด์ŠคํŒ ์œ„์— B๋ผ๊ณ  ํ‘œ์‹œ๋œ ๊ณณ์— ๋น„์ˆ์ด ์žˆ์„ ๋•Œ ๋น„์ˆ์€ ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์—ฌ O๋กœ ํ‘œ์‹œ๋œ ์นธ์— ์žˆ๋Š” ๋‹ค๋ฅธ ๋ง์„ ์žก์„ ์ˆ˜ ์žˆ๋‹ค.

< ๊ทธ๋ฆผ 1 >

๊ทธ๋Ÿฐ๋ฐ ์ฒด์ŠคํŒ ์œ„์—๋Š” ๋น„์ˆ์ด ๋†“์ผ ์ˆ˜ ์—†๋Š” ๊ณณ์ด ์žˆ๋‹ค. < ๊ทธ๋ฆผ 2 >์—์„œ ์ฒด์ŠคํŒ์— ์ƒ‰์น ๋œ ๋ถ€๋ถ„์€ ๋น„์ˆ์ด ๋†“์ผ ์ˆ˜ ์—†๋‹ค๊ณ  ํ•˜์ž. ์ด์™€ ๊ฐ™์€ ์ฒด์ŠคํŒ์— ์„œ๋กœ๊ฐ€ ์„œ๋กœ๋ฅผ ์žก์„ ์ˆ˜ ์—†๋„๋ก ํ•˜๋ฉด์„œ ๋น„์ˆ์„ ๋†“๋Š”๋‹ค๋ฉด < ๊ทธ๋ฆผ 3 >๊ณผ ๊ฐ™์ด ์ตœ๋Œ€ 7๊ฐœ์˜ ๋น„์ˆ์„ ๋†“์„ ์ˆ˜ ์žˆ๋‹ค. ์ƒ‰์น ๋œ ๋ถ€๋ถ„์—๋Š” ๋น„์ˆ์ด ๋†“์ผ ์ˆ˜ ์—†์ง€๋งŒ ์ง€๋‚˜๊ฐˆ ์ˆ˜๋Š” ์žˆ๋‹ค.

< ๊ทธ๋ฆผ 2 >

< ๊ทธ๋ฆผ 3 >

์ •์‚ฌ๊ฐํ˜• ์ฒด์ŠคํŒ์˜ ํ•œ ๋ณ€์— ๋†“์ธ ์นธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฒด์ŠคํŒ์˜ ํฌ๊ธฐ๋ผ๊ณ  ํ•œ๋‹ค. ์ฒด์ŠคํŒ์˜ ํฌ๊ธฐ์™€ ์ฒด์ŠคํŒ ๊ฐ ์นธ์— ๋น„์ˆ์„ ๋†“์„ ์ˆ˜ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์„œ๋กœ๊ฐ€ ์„œ๋กœ๋ฅผ ์žก์„ ์ˆ˜ ์—†๋Š” ์œ„์น˜์— ๋†“์„ ์ˆ˜ ์žˆ๋Š” ๋น„์ˆ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ฒด์ŠคํŒ์˜ ํฌ๊ธฐ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฒด์ŠคํŒ์˜ ํฌ๊ธฐ๋Š” 10์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ์•„๋ž˜์˜ ์˜ˆ์™€ ๊ฐ™์ด ์ฒด์ŠคํŒ์˜ ๊ฐ ์นธ์— ๋น„์ˆ์„ ๋†“์„ ์ˆ˜ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฒด์ŠคํŒ ํ•œ ์ค„ ๋‹จ์œ„๋กœ ํ•œ ์ค„์”ฉ ์ฃผ์–ด์ง„๋‹ค. ๋น„์ˆ์„ ๋†“์„ ์ˆ˜ ์žˆ๋Š” ๊ณณ์—๋Š” 1, ๋น„์ˆ์„ ๋†“์„ ์ˆ˜ ์—†๋Š” ๊ณณ์—๋Š” 0์ด ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง„ ์ฒด์ŠคํŒ ์œ„์— ๋†“์„ ์ˆ˜ ์žˆ๋Š” ๋น„์ˆ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.

๋‚ด๊ฐ€ ํ•ด๊ฒฐํ•˜๋ คํ•œ ๋ฐฉ๋ฒ•์€ ๋น„์ˆ์„ ๋†“๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ  ํ•ด๋‹น ๊ฒฝ์šฐ๊ฐ€ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š”์ง€ ํŒ๋ณ„ํ•˜๊ณ  ์ตœ๋Œ€ ๋น„์ˆ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋ ค๊ณ  ํ–ˆ๋‹ค.

DFS๋ฅผ ๋‘ ๊ฐœ ๋งŒ๋“ค์–ด์„œ ๊ฐ๊ฐ ์‹œ๋„ํ•ด๋ดค๋Š”๋ฐ ๋‘˜ ๋‹ค ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์„œ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.

๋‚ด๊ฐ€ ์ฒ˜์Œ์— ์ƒ๊ฐํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

// 100๊ฐœ์˜ ์นธ์ด ์ตœ๋Œ€ ๊ฐ’
// ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž.
// ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•  ๊ฒƒ์ด๋‹ค.
// ํ•œ ์œ„์น˜์— ๋น„์ˆ์„ ๋†“๊ฑฐ๋‚˜ ์•ˆ๋†“๊ฑฐ๋‚˜์ด๋‹ˆ๊นŒ
// ์นธ๋‹น 2๊ฐœ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜
// ์ตœ๋Œ€ 2^100์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‚˜์˜จ๋‹ค -> ์ด๋ ‡๊ฒŒ ํƒ์ƒ‰ํ•˜๋ฉด ์‹œ๊ฐ„๋‚ด์— ๋ชปํ‘ผ๋‹ค

void setting();
bool isOkay(pair<int, int> p);
void DFS(int L);
void printBoard();
void solve();

int n, cache[100], res = 0, cnt = 0;
vector<vector<int>> board;
vector<pair<int, int>> goodPos;
int dy[] = { 1, 1, -1, -1 }; // ์ขŒ์ธก ํ•˜๋‹จ, ์šฐ์ธก ํ•˜๋‹จ, ์šฐ์ธก ์ƒ๋‹จ, ์ขŒ์ธก ์ƒ๋‹จ.
int dx[] = { -1, 1, 1, -1 };
void setting()
{
	cin >> n;
	board = vector<vector<int>>(n + 1, vector<int>(n + 1));

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cin >> board[i][j];
			if (board[i][j] == 1) // ๋†“์„ ์ˆ˜ ์žˆ๋Š” ๋น„์ˆ ์ž๋ฆฌ๋“ค.
				goodPos.push_back({ i, j });
		}
	}
}

// p์œ„์น˜์— ๋น„์ˆ์„ ๋†“์•˜์„ ๋•Œ ๊ดœ์ฐฎ์€์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜.
bool isOkay(pair<int, int> p)
{
	int y = p.first;
	int x = p.second;
	if (board[y][x] == 1)
		return true;

	for (int i = 0; i < 4; ++i)
	{
		while (true)
		{
			int ny = y + dy[i];
			int nx = x + dx[i];

			if (ny < 1 || nx < 1 || ny > n || nx > n)
				break;
			// ๋‹ค๋ฅธ ๋น„์ˆ์ด ์žˆ์„ ๊ฒฝ์šฐ ์ž˜๋ชป๋œ ์œ„์น˜.
			if (board[ny][nx] == 2)
			{
				return false;
			}

			y = ny;
			x = nx;
		}

		y = p.first;
		x = p.second;
	}

	return true;
}

void DFS(int L)
{
	if (L == goodPos.size())
	{
		bool flag = true;
		for (int i = 0; i < goodPos.size(); ++i)
		{
			if (isOkay({ goodPos[i].first, goodPos[i].second }) == false)
			{
				flag = false;
				break;
			}

		}

		if (flag == true)
		{
			cnt = 0;

			for (int i = 0; i < goodPos.size(); ++i)
			{
				if (board[goodPos[i].first][goodPos[i].second] == 2)
					cnt++;
			}

			res = max(res, cnt);
		}
	}
	else
	{
		board[goodPos[L].first][goodPos[L].second] = 1;
		cache[L] = 1;
		DFS(L + 1);
		board[goodPos[L].first][goodPos[L].second] = 2;
		cache[L] = 2;
		DFS(L + 1);
	}
}

void printBoard()
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cout << board[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

void bishop(int L)
{
	//printBoard();

	if (L == goodPos.size())
	{
		cnt = 0;

		for (int i = 0; i < goodPos.size(); ++i)
		{
			if (board[goodPos[i].first][goodPos[i].second] == 2)
				cnt++;
		}

		res = max(res, cnt);

		return;
	}
	else
	{
		board[goodPos[L].first][goodPos[L].second] = 2; // ์ด ์œ„์น˜์— ๋น„์ˆ ๋†“๊ธฐ
		if (isOkay(goodPos[L]))
			bishop(L + 1);

		board[goodPos[L].first][goodPos[L].second] = 1; // ์›๋ณต
		bishop(L + 1);
	}
}

void solve()
{
	setting();

	//DFS(0);
	bishop(0);
	cout << res;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

์ฐธ ์ž˜ ํ‘ผ๊ฑฐ๊ฐ™์€๋ฐ ์•„์‰ฝ.

๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ๊นŒ ๋น„์ˆ์„ ํŠน์ง•์„ ์ด์šฉํ•ด์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์˜€๋‹ค.

ํฐ์นธ์— ์žˆ๋Š” ๋น„์ˆ์€ ํฐ์นธ์œผ๋กœ๋งŒ ์ด๋™์ด ๊ฐ€๋Šฅํ•˜๊ณ  ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฒ€์€์นธ์— ์žˆ๋Š” ๋น„์ˆ์€ ๊ฒ€์€์นธ์œผ๋กœ๋งŒ ์ด๋™์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

void setting();
void printBoard();
void solve();

int n, res = 0, cnt = 0;
int board[11][11];
vector<pair<int, int>> arr;
vector<pair<int, int>> bishopPos;
void setting()
{
	cin >> n;

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cin >> board[i][j];
			if (board[i][j] == 1 && (i + j) % 2 == 0) // ๋†“์„ ์ˆ˜ ์žˆ๋Š” ๋น„์ˆ ์ž๋ฆฌ๋“ค.
				arr.push_back({ i, j });
		}
	}
}

void printBoard()
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cout << board[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

void bishop(int cnt, int L)
{
	if (cnt > res)
		res = cnt;
	if (L == arr.size())
		return;

	bool flag = true;
	for (int i = 0; i < bishopPos.size(); ++i)
	{
		if (abs(arr[L].first - bishopPos[i].first) == abs(arr[L].second - bishopPos[i].second))
		{
			flag = false;
			break;
		}
	}
	if (flag == true)
	{
		bishopPos.push_back({ arr[L].first, arr[L].second });
		bishop(cnt + 1, L + 1);
		bishopPos.pop_back();
	}
	bishop(cnt, L + 1);

	return;
}

void solve()
{
	setting();

	int ans = 0;
	bishop(0, 0);
	ans += res;
	res = 0;
	arr.clear();

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			if (board[i][j] == 1 && (i + j) % 2 == 1)
				arr.push_back({ i, j });
		}
	}
	bishop(0, 0);
	ans += res;
	res = 0;
	arr.clear();

	cout << ans;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: