using System;
public class Solution {
public int[] solution(string[] park, string[] routes) {
int[] answer = new int[2];
int[] startPos = new int[2];
bool[,] canGo = new bool[park.Length, park[0].Length];
bool allCanGo = true;
for(int i = 0; i < park.Length; ++i)
{
for(int j = 0; j < park[0].Length; ++j)
{
if(park[i][j] == 'S')
{
startPos[1] = j;
startPos[0] = i;
canGo[i,j] = true;
continue;
}
else if(park[i][j] == 'X')
{
canGo[i,j] = false;
}
else
{
canGo[i,j] = true;
}
}
}
for (int i = 0; i < routes.Length; ++i)
{
if (routes[i][0] == 'E')
{
if (startPos[1] + int.Parse(routes[i][2].ToString()) >= park[0].Length) continue;
for(int j = 1; j <= int.Parse(routes[i][2].ToString()); ++j)
{
if (canGo[startPos[0], startPos[1] + j] == false)
{
allCanGo = false;
break;
}
}
if(allCanGo == true)
{
startPos[1] += int.Parse(routes[i][2].ToString());
}
allCanGo = true;
}
else if(routes[i][0] == 'W')
{
if (startPos[1] - int.Parse(routes[i][2].ToString()) < 0) continue;
for(int j = 1; j <= int.Parse(routes[i][2].ToString()); ++j)
{
if (canGo[startPos[0], startPos[1] - j] == false)
{
allCanGo = false;
break;
}
}
if(allCanGo == true)
{
startPos[1] -= int.Parse(routes[i][2].ToString());
}
allCanGo = true;
}
else if (routes[i][0] == 'S')
{
if (startPos[0] + int.Parse(routes[i][2].ToString()) >= park.Length) continue;
for(int j = 1; j <= int.Parse(routes[i][2].ToString()); ++j)
{
if (canGo[startPos[0] + j, startPos[1]] == false)
{
allCanGo = false;
break;
}
}
if(allCanGo == true)
{
startPos[0] += int.Parse(routes[i][2].ToString());
}
allCanGo = true;
}
else if (routes[i][0] == 'N')
{
if (startPos[0] - int.Parse(routes[i][2].ToString()) < 0) continue;
for(int j = 1; j <= int.Parse(routes[i][2].ToString()); ++j)
{
if (canGo[startPos[0] - j, startPos[1]] == false)
{
allCanGo = false;
break;
}
}
if(allCanGo == true)
{
startPos[0] -= int.Parse(routes[i][2].ToString());
}
allCanGo = true;
}
}
answer = startPos;
return answer;
}
}
bool 2차 배열로 모든 길을 갈 수 있는지 판별 후, 단순히 계산해줬다. 계산하는 과정에 실수가 있어서 오래걸렸지만 해결법은 단순한 편.
'C# 알고리즘 코드카타' 카테고리의 다른 글
75. 최대값과 최솟값 (0) | 2024.02.07 |
---|---|
74. 신고 결과 받기 (0) | 2024.02.07 |
72. 달리기 경주 (0) | 2024.01.29 |
71. 개인정보 수집 유효기간 (0) | 2024.01.25 |
70. 바탕화면 정리 (0) | 2024.01.23 |