구현 예제4-1 상하좌우

·2022년 6월 24일
0

이코테_알고리즘

목록 보기
22/23

코드


using namespace std;
#include <iostream>
#include <vector>
#include <string>
#include <map>

int main()
{
	// shift alt 로 동일한 행 내려가기 가능!
	
	int n;
	cin >> n;

	// r, l, u , d를 처리하는 배열을 만들자.
	map<char, pair<int, int>>m;
	//1번째가 y값 , 2번째가 x값
	m['R'] = { 0, 1 };
	m['L'] = { 0, -1 };
	m['U'] = { -1, 0 };
	m['D'] = { 1, 0 };
	//vector<vector<int>>v(n + 1, vector<int>(n + 1));

	//=====================
	cin.clear();
	cin.ignore();
	// 엔터를 치면 끝남? 
	string s;
	getline(cin, s);

	pair<int, int>pos(1,1);

	for (auto iter : s)
	{
		if (pos.first + m[iter].first >= 1 
			&& pos.first + m[iter].first <= n
			&& pos.second + m[iter].second >= 1
			&& pos.first + m[iter].first <= n)
		{
			pos.first += m[iter].first;
			pos.second += m[iter].second;
		}
	}
	cout << pos.first << " " << pos.second;

}
profile
🔥🔥🔥

0개의 댓글