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

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

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

programmers.co.kr

 

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

 

์–ด๋ ค์šด ๊ตฌํ˜„ ๋ฌธ์ œ๊ฐ€ ์•„๋‹Œ๋ฐ, ์ ์ด ์•„๋‹Œ ์„ ๋ถ„(line) ์ฒ˜๋ฆฌ๊ฐ€ ๊นŒ๋‹ค๋กœ์šด ๋ฌธ์ œ์˜€๋„ค์š”. ์„ ๋ถ„์€ ๋‘ ๊ฐœ์˜ ์ (Point)์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋‹ˆ, map์„ ํ†ตํ•ด <์‹œ์ž‘์ , ๋ ์ ๋“ค>์„ ๊ด€๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ ์ ๋“ค์ด๋ผ๊ณ  ํ‘œํ˜„ํ•ด๋†จ๋Š”๋ฐ, ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ํ•œ ๋ฒˆ์— ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์นธ์ˆ˜๋Š” 1์นธ์ด๋ฏ€๋กœ, ์  ํ•˜๋‚˜๋‹น ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์„ ๋ถ„์˜ ์ข…๋ฅ˜๋Š” 4๊ฐ€์ง€(์ƒ, ํ•˜, ์ขŒ, ์šฐ)์ž…๋‹ˆ๋‹ค.

 

 

์ œ๊ฐ€ ๊ตฌํ˜„ํ•œ ๋กœ์ง์„ ์‹œ๋‚˜๋ฆฌ์˜ค๋Œ€๋กœ ํ•œ๋ฒˆ ๋ณด๋„๋ก ํ•˜์ฃ . "UUDL"์„ ํ•œ ๋ฒˆ ์˜ˆ์‹œ๋กœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

1. [U] ์ถœ๋ฐœ์ง€(0, 0)์—์„œ ํ•œ ์นธ ์œ„๋กœ ์˜ฌ๋ผ๊ฐ€๊ณ , ํ•ด๋‹น ์„ ๋ถ„์„ map์— ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

  • ๋„์ฐฉ์ง€(0, 1)์—์„œ ์ถœ๋ฐœ์ง€(0, 0)๊นŒ์ง€์˜ ์„ ๋ถ„๋„ ๊ฐ™์€ ์„ ๋ถ„์ด๋ฏ€๋กœ, ์ถœ๋ฐœ์ง€์™€ ๋„์ฐฉ์ง€ ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•œ ๊ฒƒ๋„ map์— ๋“ฑ๋กํ•ด์ค๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ๋ชจ๋“  ์ด๋™์—์„œ ๋˜‘๊ฐ™์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ๋“ฑ๋ก๋œ ์„ ๋ถ„๋“ค ์ค‘์— ํ˜„์žฌ ์„ ๋ถ„์ด ์—†๋‹ค๋ฉด answer + 1์„ ํ•ด์ค๋‹ˆ๋‹ค.

 

 

2. [U] (0, 1) \( \rightarrow \) (0, 2)๋กœ ์ด๋™ํ•˜์—ฌ ๋˜‘๊ฐ™์ด map์— ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

 

 

3. [D] (0, 2) \( \rightarrow \) (0, 1)๋กœ ์ด๋™ํ•˜์ง€๋งŒ, ์ด ์„ ๋ถ„์€ ๋ ์ ์— ๋“ฑ๋ก๋œ ์„ ๋ถ„์ด๋ฏ€๋กœ ์นด์šดํŠธ X

 

 

4. [L] (0, 1) \( \rightarrow \) (-1, 1)๋กœ ์ด๋™ํ•ด์ค๋‹ˆ๋‹ค.

 

 

์ด๋กœ์จ answer = 3์œผ๋กœ ๋‹ต์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

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

#include <string>
#include <vector>
#include <map>
using namespace std;
const int MAX_LENGTH = 11;

struct Point {
	Point() { }
	Point(int row, int col) : _row(row), _col(col) { }

	bool operator<(const Point& other) const {
		if (_row == other._row)
			return _col < other._col;
		return _row < other._row;
	}

	bool operator==(const Point& other) const {
		return _row == other._row and _col == other._col;
	}

	int _row;
	int _col;
};

map<char, Point> directions {  {'U', {-1, 0 }}, { 'D', {1, 0} }, { 'R', {0, 1} }, { 'L', {0, -1} }  };


class Record {
public:
	void Insert(const Point& otherStart, const Point& otherEnd) {
		auto iter = _lines.find(otherStart);
		
		/* ๋‘ ์„ ๋ถ„์˜ ์‹œ์ž‘์ ์ด ๊ฐ™๋‹ค๋ฉด, ๋ ์ ์„ ๋น„๊ตํ•ด๋ณธ๋‹ค. */
		if (iter != _lines.end()) {
			for (const auto myEndPoint : iter->second) {
				if (myEndPoint == otherEnd)
					return;
			}
		}

        // ์  A, B๊ฐ€ ์žˆ๋‹ค๋ฉด A-B, B-A๋Š” ๊ฐ™์€ ์„ ๋ถ„์ด๋ฏ€๋กœ ๋‘˜ ๋‹ค ์ถ”๊ฐ€
		_lines[otherStart].push_back(otherEnd);
		_lines[otherEnd].push_back(otherStart);
		_firstStepCount++;
	}

	int GetAnswer() { return _firstStepCount; }
private:
	map<Point, vector<Point>> _lines;    // <์‹œ์ž‘์ , ๋ ์ ๋“ค>
	int _firstStepCount = 0;
};


int solution(string dirs) {
	int firstStepCount = 0;
	int halfLine = MAX_LENGTH / 2;

	Point player(halfLine, halfLine);
	Record record;

	for (const auto instruction : dirs) {
	int nextRow = player._row + directions[instruction]._row;
	int nextCol = player._col + directions[instruction]._col;

	if (nextRow < 0 or nextRow >= MAX_LENGTH or nextCol < 0 or nextCol >= MAX_LENGTH)
		continue;

	Point start = player;
	Point end = player = Point(nextRow, nextCol);
	
	record.Insert(start, end);
	}

	return record.GetAnswer();
}

 

 

728x90
๋ฐ˜์‘ํ˜•