https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRF8s6ezEDFAUo



문제에 제시된 대로 구현하면 되는 시뮬레이션 문제다.

블록의 상태에 따라 이동방향을 변경해주는 작업과 웜홈을 만났을 때 다른 웜홀로 이동시켜주는 부분만 신경써주면 된다.


(0,0)부터 (N,N)까지 값이 0인 지점에서 4방향으로 출발점을 설정하여

얻을 수 있는 점수의 최대값을 계산해주면 된다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
 
int dy[4]={-1,0,1,0};
int dx[4]={0,1,0,-1};
 
/*rotate_arr[n][dir] => 기존 dir방향일때 n번 블록 만나면 바뀌는 dir 값 */
int rotate_arr[6][4]={{0,0,0,0},{2,3,1,0},{1,3,0,2},{3,2,0,1},{2,0,3,1},{2,3,0,1}};
int map[100][100]={0}; //맵
vector<pair<int,int>> worm_holl[11]; //n번째 홀 (y,x) 좌표
 
int N;
int sy=0,sx=0//시작 좌표
 
int solve(int y,int x,int dir){
 
    int score=0;
    while(true){
 
        y+=dy[dir];
        x+=dx[dir];
 
        //벽에 부딪혔을 때
        if(y<0 || y>=|| x<0 || x>=N){
            dir=(dir+2)%4;
            score++;
            continue;
        }
 
        //블록 부딪혔을 때
        if(map[y][x]>=1 && map[y][x]<=5){
            dir = rotate_arr[map[y][x]][dir];
            score++;
            continue;
        }
 
        //웜홀 빠졌을 때
        if(map[y][x]>5){
            
            int num=map[y][x];
            if(y==worm_holl[num][0].first && x==worm_holl[num][0].second){
                y=worm_holl[num][1].first;
                x=worm_holl[num][1].second;
            }else{
                y=worm_holl[num][0].first;
                x=worm_holl[num][0].second;
            }
 
            continue;
        }
 
        //블랙홀
        if(map[y][x]==-1)
            break;
 
        //시작점
        if((y==sy && x==sx)){
            break;
        }
 
    }
 
    return score;
}
int main(){
 
    int T;
    cin>>T;
 
    for(int testCase=1;testCase<=T;testCase++){
 
        cin>>N;
 
        memset(map,0,sizeof(map)); //맵 초기화
        
        for(int i=6;i<11;i++)   // 웜홀 초기화
            worm_holl[i].clear();
        
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                scanf("%d",&map[i][j]);
 
                if(map[i][j]>5){
                    worm_holl[map[i][j]].push_back(make_pair(i,j));
                }
            }
        }
 
        int score=0;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
 
                if(map[i][j]!=0)
                    continue;
 
                sy=i;sx=j;
                for(int dir=0;dir<4;dir++){
                    score=max(score,solve(i,j,dir));
                }
 
            }
        }
 
        cout<<"#"<<testCase<<" "<<score<<endl;
    }
 
    return 0;
}
 
cs



'알고리즘(SWEA) > 시뮬레이션' 카테고리의 다른 글

SWEA 2383번 - 점심 식사시간  (2) 2019.10.13
SWEA 5644번 - 무선충전  (0) 2019.10.09

+ Recent posts