Skip to content

Conversation

@alirz-pixel
Copy link
Member

🚀 이슈 번호

Resolve: {#2328}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지: BFS
  • 🔹 어떤 방식으로 접근했는지

다른 문제들과 같이 똑같이 BFS를 사용하되,
회전에 따른 visited를 0, 1로 구분하여 처리하였다.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(N^4)
  • 이유:

visited 배열로 인해 이미 방문한 곳은 새로 방문되지 않는다.
이로 인해 각 board를 한 번 씩만 반복하기 때문에 N^2이지만,
회전에 따른 방문을 또 진행하기 때문에 N^4이 된다.

💻 구현 코드

#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;

int n;
char a[55][55];
const int dy[4] = { -1,0,1,0 };
const int dx[4] = { 0,1,0,-1 };
struct g {
	int y, x;
	int status; // 세로면 0 가로면 1
};

int b1y, b1x, b2y, b2x;
int e1y, e1x, e2y, e2x;
int estatus;
int solve(int cy, int cx, int cst)
{
	//가로 세로
	int d[55][55][2];
	queue<g> q;
	memset(d, -1, sizeof(d));
	q.push({ cy,cx,cst });
	d[cy][cx][cst] = 0;

	int ret = 0;

	while (!q.empty()) {
		int y = q.front().y;
		int x = q.front().x;
		int st = q.front().status;

		q.pop();

		for (int k = 0; k < 4; ++k) {

			int ny = y + dy[k];
			int nx = x + dx[k];

			if (!(0 <= ny && ny < n && 0 <= nx && nx < n)) continue;
			if (d[ny][nx][st] != -1) continue;

			// 세로
			if (st == 0) {
				if (ny - 1 < 0 || ny + 1 >= n) continue;
				if (a[ny - 1][nx] == '1') continue;
				if (a[ny][nx] == '1') continue;
				if (a[ny + 1][nx] == '1') continue;

				d[ny][nx][st] = d[y][x][st] + 1;
				q.push({ ny,nx,st });
			}
			// 가로
			else if (st == 1) {
				if (nx - 1 < 0 || nx + 1 >= n) continue;
				if (a[ny][nx - 1] == '1') continue;
				if (a[ny][nx] == '1') continue;
				if (a[ny][nx + 1] == '1') continue;

				d[ny][nx][st] = d[y][x][st] + 1;
				q.push({ ny,nx,st });
			}
		}
		//회전이 가능한지

		if (d[y][x][1 - st] != -1) continue;
		if (st == 0) {
			if (y - 1 < 0 || y + 1 >= n || x - 1 < 0 || x + 1 >= n) continue;

			if (a[y - 1][x - 1] != '1' && a[y][x - 1] != '1' && a[y + 1][x - 1] != '1') {
				if (a[y - 1][x + 1] != '1' && a[y][x + 1] != '1' && a[y + 1][x + 1] != '1') {

					d[y][x][1 - st] = d[y][x][st] + 1;
					q.push({ y,x,1 - st });
				}
			}
		}
		else if (st == 1) {
			if (y - 1 < 0 || y + 1 >= n || x - 1 < 0 || x + 1 >= n) continue;

			if (a[y - 1][x - 1] != '1' && a[y - 1][x] != '1' && a[y - 1][x + 1] != '1')
				if (a[y + 1][x - 1] != '1' && a[y + 1][x] != '1' && a[y + 1][x + 1] != '1') {

					d[y][x][1 - st] = d[y][x][st] + 1;
					q.push({ y,x,1 - st });
				}
		}
	}
	if (d[e2y][e2x][estatus] == -1)
		return 0;
	else return d[e2y][e2x][estatus];
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	cin >> n;
	bool flg = false;
	bool flg2 = false;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j) {
			cin >> a[i][j];
			if (a[i][j] == 'B') {
				if (flg == false) {
					b1y = i;
					b1x = j;
					flg = true;
				}
				else {
					b2y = i;
					b2x = j;
					flg = false;
				}
			}
			if (a[i][j] == 'E') {
				if (flg2 == false) {
					e1y = i;
					e1x = j;
					flg2 = true;
				}
				else {
					e2y = i;
					e2x = j;
					flg2 = false;
				}
			}
		}

	if (e2x - e1x == 0) {
		estatus = 0;
	}
	else estatus = 1;

	int ans = 0;
	if (b2x - b1x == 0) {
		ans = solve(b2y, b2x, 0);
	}
	else {
		ans = solve(b2y, b2x, 1);
	}

	cout << ans << "\n";
	return 0;
}

@alirz-pixel alirz-pixel self-assigned this Jan 23, 2026
@alirz-pixel alirz-pixel linked an issue Jan 23, 2026 that may be closed by this pull request
@alirz-pixel alirz-pixel merged commit 2a4db23 into main Jan 25, 2026
1 check passed
@alirz-pixel alirz-pixel deleted the munhyeong/2328/2 branch January 25, 2026 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260123 : 코딩테스트

2 participants