C++/백준 C++

C++ 3190 뱀 [시물레이션]

CE : 하랑 2024. 10. 26. 16:11

 

 

 

코드

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

struct FBoardsValue
{
public:
	bool IsApple = false;
	bool Visited = false;
};

struct FDirTime
{
public:
	int Time = 0;
	char Dir;
};


int MoveSerch(std::vector<std::vector<FBoardsValue>>& _boards, std::vector<FDirTime>& _DirTime)
{
	int dy[] = { 0,1,0,-1 };
	int dx[] = { 1,0,-1,0 };

	int time = 0;
	int MoveIndex = 0;
	std::list<std::pair<int, int>> Dummy;

	Dummy.push_front(std::make_pair(0, 0));

	int Nexty = 0;
	int Nextx = 0;

	int FDirTimeIndex=0;

	while (true)
	{
		++time;
		Nexty = Nexty + dy[MoveIndex];
		Nextx = Nextx + dx[MoveIndex];

		if (Nexty >= _boards.size() || Nextx >= _boards.size() || 0 > Nextx || 0 > Nexty || _boards[Nexty][Nextx].Visited == true)
		{
			break;
		}
		_boards[Nexty][Nextx].Visited = true;

		Dummy.push_front(std::make_pair(Nexty, Nextx));
		// 이동할 공간에 사과가 있을 경우
		if (true == _boards[Nexty][Nextx].IsApple)
		{
			_boards[Nexty][Nextx].IsApple = false;

		}
		else // 이동할 공간에 사과가 없는 경우
		{
			int CurY = Dummy.back().first;
			int CurX = Dummy.back().second;

			_boards[CurY][CurX].Visited = false;

			Dummy.pop_back();
		}



		if (time == _DirTime[FDirTimeIndex].Time)
		{
			if (_DirTime[FDirTimeIndex].Dir == 'D')
			{
				++MoveIndex;

				if (MoveIndex > 3)
				{
					MoveIndex = 0;
				}
			}
			else
			{
				--MoveIndex;

				if (MoveIndex < 0)
				{
					MoveIndex = 3;
				}
			}

			++FDirTimeIndex;
		}

	}

	return time;
}

int main()
{
	std::vector<std::vector<FBoardsValue>> boards;
	std::vector<FDirTime> DirTime;

	int n;
	std::cin >> n;

	boards.resize(n);

	for (int i = 0; i < n; i++)
	{
		boards[i].resize(n);
	}

	int K;

	std::cin >> K;

	for (int i = 0; i < K; i++)
	{
		int y, x;

		std::cin >> y >> x;

		boards[y - 1][x - 1].IsApple = true;
	}

	int L;
	std::cin >> L;

	DirTime.resize(L);

	for (int i = 0; i < L; i++)
	{

		std::cin >> DirTime[i].Time >> DirTime[i].Dir;
	}

	int count = MoveSerch(boards, DirTime);

	std::cout << count << "\n";
	return 0;
}