반응형

 

 

<코드>

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int map[101][101];
int direction = 0; //남(0),서(1),북(2),동(3) 
int N, R, C;
int R_min, R_max, C_min, C_max;
string s;

int main()
{
	cin >> N;
	cin >> s;

	R = C = 50;
	map[R][C] = 1;
	R_min = 50;
	C_min = 50;
	R_max = 50;
	C_max = 50;
	
	for (int i = 0; i < N; i++)
	{
		
		// L,R,F 명령 처리
		if (s[i] == 'L') direction--;
		else if (s[i] == 'R') direction++;
		else if (s[i] == 'F')
		{
			if (direction == 0) R++;
			else if (direction == 1) C--;
			else if (direction == 2) R--;
			else if (direction == 3) C++;
			map[R][C] = 1;
		}

		if (direction < 0) direction += 4;
		else if (direction >= 4) direction -= 4;

		R_min = min(R_min, R);
		C_min = min(C_min, C);
		R_max = max(R_max, R);
		C_max = max(C_max, C);
	}

	for (int i = R_min; i <= R_max; i++)
	{
		for (int j = C_min; j <= C_max; j++)
		{
			cout << (map[i][j]?'.':'#');
		}
		cout << '\n';
	}
}

 

풀이 방법

미로의 시작점이 주어지지 않았으므로 map의 크기는 50x50이 아닌 100x100으로 설정하고 시작점을 (50,50)으로 설정하여야 합니다. 그리고 반복문을 돌때마다 행과 열의 최대값, 최소값들을 기록하여 미로부분만 출력할 수 있도록 합니다.

 

www.acmicpc.net/problem/1347

 

1347번: 미로 만들기

홍준이는 미로 안의 한 칸에 남쪽을 보며 서있다. 미로는 직사각형 격자모양이고, 각 칸은 이동할 수 있거나, 벽을 포함하고 있다. 모든 행과 열에는 적어도 하나의 이동할 수 있는 칸이 있다. 홍

www.acmicpc.net

 

반응형

+ Recent posts