반응형
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
DFS로 구현할 필요없는 시물레이션 문제.
그런데 49개의 테스트에서 자꾸 틀렸다고 나옴.
이력을 보면 시간초과도 메모리 초과도 아니다.
이유를 도저히 모르겠음. 어쨋든 로직은 맞는 듯.
#include<iostream>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
int arr[100][100];
int N;
int starty, startx;
int result;
int y, x, dir, cost;
static vector<pair<int, int> > vec[11];
int changedir(int dir, int shape)
{
if (dir == 1)
{
if (shape == 1)
{
return 2;
}
else if (shape == 2)
{
return 4;
}
else if (shape == 3)
{
return 3;
}
else if (shape == 4)
{
return 2;
}
else if (shape == 5)
{
return 2;
}
}
else if (dir == 2)
{
if (shape == 1)
{
return 4;
}
else if (shape == 2)
{
return 1;
}
else if (shape == 3)
{
return 1;
}
else if (shape == 4)
{
return 3;
}
else if (shape == 5)
{
return 1;
}
}
else if (dir == 3)
{
if (shape == 1)
{
return 1;
}
else if (shape == 2)
{
return 2;
}
else if (shape == 3)
{
return 4;
}
else if (shape == 4)
{
return 4;
}
else if (shape == 5)
{
return 4;
}
}
else if (dir == 4)
{
if (shape == 1)
{
return 3;
}
else if (shape == 2)
{
return 3;
}
else if (shape == 3)
{
return 2;
}
else if (shape == 4)
{
return 1;
}
else if (shape == 5)
{
return 3;
}
}
}
void move()
{
if (dir == 1)
{
y--;
if (y < 0)
{
y++;
cost++;
dir = 2;
}
}
else if (dir == 2)
{
y++;
if (y >= N)
{
y--;
cost++;
dir = 1;
}
}
else if (dir == 3)
{
x--;
if (x < 0)
{
x++;
cost++;
dir = 4;
}
}
else if (dir == 4)
{
x++;
if (x >= N)
{
x--;
cost++;
dir = 3;
}
}
}
void run()
{
while (1)
{
int loc = arr[y][x];
if (!loc)
{
move();
}
else if (loc >= 1 && loc <= 5)
{
cost++;
dir = changedir(dir, loc);
move();
}
else if (loc >= 6 && loc <= 10)
{
for (int i = 0; i < 2; i++)
{
if (vec[loc][i].first != y && vec[loc][i].second != x)
{
y = vec[loc][i].first;
x = vec[loc][i].second;
break;
}
}
if (dir == 1)
{
y--;
if (y < 0)
{
y ++;
cost++;
dir = 2;
}
}
else if (dir == 2)
{
y++;
if (y >= N)
{
y --;
cost++;
dir = 1;
}
}
else if (dir == 3)
{
x--;
if (x < 0)
{
x ++;
cost++;
dir = 4;
}
}
else if (dir == 4)
{
x++;
if (x >= N)
{
x --;
cost++;
dir = 3;
}
}
}
else if (arr[y][x] == -1)
{
if (cost > result)
{
result = cost;
}
break;
}
if ((y == starty && x == startx))
{
if (cost > result)
{
result = cost;
}
break;
}
}
}
int main(int argc, char** argv)
{
int test_case;
int T;
cin >> T;
for (test_case = 1; test_case <= T; ++test_case)
{
cin >> N;
result = 0;
for (int i = 6; i <= 10; i++)
{
vec[i].clear();
}
memset(arr, 0, sizeof(arr));
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> arr[i][j];
if (arr[i][j] >= 6 && arr[i][j] <= 10)
{
vec[arr[i][j]].push_back({ i,j });
}
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (!arr[i][j])
{
starty = i;
startx = j;
y = i;
x = j;
dir = 1;
cost = 0;
run();
y = i;
x = j;
dir = 2;
cost = 0;
run();
y = i;
x = j;
dir = 3;
cost = 0;
run();
y = i;
x = j;
dir = 4;
cost = 0;
run();
}
}
}
cout << "#" << test_case << " " << result << endl;
}
return 0;//정상종료시 반드시 0을 리턴해야합니다.
}반응형
'Algorithm' 카테고리의 다른 글
| SW 1211. [S/W 문제해결 기본] 2일차 - Ladder2 (0) | 2020.03.24 |
|---|---|
| 1218. [S/W 문제해결 기본] 4일차 - 괄호 짝짓기 (0) | 2020.03.23 |
| 1868. 파핑파핑 지뢰찾기 (0) | 2020.03.21 |
| 3459. 승자 예측하기 (0) | 2020.03.20 |
| 2819. 격자판의 숫자 이어 붙이기 (0) | 2020.03.20 |