
코드
#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;
}
'C++ > 백준 C++' 카테고리의 다른 글
C++ 1475 방 번호 [구현] (0) | 2024.11.05 |
---|---|
C++ 13305 주유소 [그리드, 자료형 범위] (0) | 2024.10.28 |
C++ 15486 퇴사 2 [DP] (0) | 2024.10.25 |
C++ 14889 스타트와 링크 [DFS, 백트래킹] (0) | 2024.10.24 |
C++ 18352 특정 거리의 도시 찾기 [최단 거리, BFS] (0) | 2024.10.23 |