본문 바로가기
IT/BOJ

백준(BOJ) 1938 통나무 **

by 빨강자몽 2018. 5. 31.

# BFS


정말 너무 나도 지저분한 문제여서 소름이 끼쳤다.헿

공부 용으로 좋은 문제는 아닌 것 같다.


#include<iostream>
#include<cstdio>
#include<queue>
#include<climits>
using namespace std;
struct Node
{
	int se;
	int ga;
	int k;
	int gab;
};
int n;
char map[51][51];
Node s, e;
int dp[2][50][50];
int dx[4] = { 0,1,0,-1 };
int dy[4] = { -1,0,1,0 };
int dkx[2] = {0,1};
int dky[2] = { 1,0 };
bool inQ[2][50][50];
queue<Node> qu;
bool check(Node now,int i)
{
	if (now.se + dy[i] - dky[now.k] < 0)
		return false;
	if (now.ga + dx[i] - dkx[now.k] < 0)
		return false;
	if (now.se + dy[i] + dky[now.k] >= n)
		return false;
	if (now.ga + dx[i] + dkx[now.k] >= n)
		return false;
	return true;
}
int main()
{
	Node now;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		getchar();
		for (int j = 0; j < n; j++)
			scanf("%c", &map[i][j]);
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (map[i][j] == 'B')
			{
				if (map[i + 1][j] == 'B')
				{
					s = { i+1,j,0,0 };
					map[i][j] = map[i + 1][j] = map[i + 2][j] = '0';
				}
				else
				{
					s = { i,j+1,1,0 };
					map[i][j] = map[i][j + 1] = map[i][j + 2] = '0';
				}
				i = n;
				break;
			}
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if (map[i][j] == 'E')
			{
				if (map[i + 1][j] == 'E')
				{
					e = { i+1,j,0,0 };
					map[i][j] = map[i + 1][j] = map[i + 2][j] = '0';
				}
				else
				{
					e = { i,j+1,1,0 };
					map[i][j] = map[i][j + 1] = map[i][j + 2] = '0';
				}
				i = n;
				break;
			}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			for (int k = 0; k < 2; k++)
				dp[k][i][j] = INT32_MAX;
	dp[s.k][s.se][s.ga] = 0;
	qu.push(s);
	inQ[s.k][s.se][s.ga] = true;
	while (qu.empty() == false)
	{
		now = qu.front();
		inQ[now.k][now.se][now.ga] = false;
		qu.pop();
		if (now.ga == e.ga && now.se == e.se && now.k == e.k)
			break;
		for (int i = 0; i < 4; i++)
		{
			if (check(now, i) == true)
			{
				if (map[now.se + dy[i] - dky[now.k]][now.ga + dx[i] - dkx[now.k]] == '0' && map[now.se + dy[i]][now.ga + dx[i]] == '0' && map[now.se + dy[i] + dky[now.k]][now.ga + dx[i] + dkx[now.k]] == '0')
				{
					if (dp[now.k][now.se + dy[i]][now.ga + dx[i]] > now.gab + 1)
					{
						dp[now.k][now.se + dy[i]][now.ga + dx[i]] = now.gab + 1;
						if (inQ[now.k][now.se + dy[i]][now.ga + dx[i]] == false)
						{
							inQ[now.k][now.se + dy[i]][now.ga + dx[i]] = true;
							qu.push({ now.se + dy[i],now.ga + dx[i],now.k,now.gab + 1 });
						}
					}
				}
			}
		}
		if (now.se - 1 >= 0 && now.se + 1 < n && now.ga - 1 >= 0 && now.ga + 1 < n)
		{
			bool able = true;
			for (int i = now.se - 1; i <= now.se + 1; i++)
				for (int j = now.ga - 1; j <= now.ga + 1; j++)
					if (map[i][j] == '1')
					{
						able = false;
						i = now.se + 2;
						break;
					}
			if (able == true && dp[1 - now.k][now.se][now.ga] > now.gab + 1)
			{
				dp[1 - now.k][now.se][now.ga] = now.gab + 1;
				if (inQ[1 - now.k][now.se][now.ga] == false)
				{
					inQ[1 - now.k][now.se][now.ga] = true;
					qu.push({ now.se,now.ga,1 - now.k,now.gab + 1 });
				}
			}
		}
	}
	if (dp[e.k][e.se][e.ga] == INT32_MAX)
		dp[e.k][e.se][e.ga] = 0;
	printf("%d", dp[e.k][e.se][e.ga]);
	return 0;
}


'IT > BOJ' 카테고리의 다른 글

백준(BOJ) 1261 알고스팟 2 **  (0) 2018.06.01
백준(BOJ) 1766 문제집 **  (0) 2018.06.01
백준(BOJ) 2623 음악프로그램 ***  (0) 2018.06.01
백준(BOJ) 2468 안전 영역 **  (0) 2018.05.31
백준(BOJ) 1600 말이 되고픈 원숭이 ***  (0) 2018.05.31