코딩스터디

1210. [S/W 문제해결 기본] 2일차 - Ladder1

애플앤마블 2019. 5. 5. 16:52
반응형
SMALL

본 문제는 swexpertacademy의 D4 1210문제임을 알립니다

 

처음 설계한 SudoCode에서는 100개의 시작점을 모두 탐색하여 끝점인 배열 2를 찾으면 break하는 방법으로 진행했지만,

 

10개의 테스트케이스를 다 마치기도 전에 Runtime error가 발생했기 때문에 경우의 수를 줄이는 방안으로

 

끝점을 탐색하고 거기에 대응하는 시작점을 출력합니다.

 

출처:swexpertacademy D4 P.1210 (하단 URL 참조)

 

#include<iostream>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int arr[102][102]={{0,},};
    int d[2]={1,-1};
    for(int t=0; t<10; t++){
        int T;
        cin>>T;
        cout<<"#"<<T<<" ";
        int x, y;
        for(int i=1; i<=100; i++){
            for(int j=1; j<=100; j++){
                cin>>arr[i][j];
                if(arr[i][j]==2){
                    x=i;
                    y=j;
                }
            }
        }
        int tmp=1;
        while(x!=1){
            if(tmp==1){
                if(arr[x][y+d[0]]==0&&arr[x][y+d[1]]==0){
                    tmp=1;
                    x-=1;
                }
                else if(arr[x][y+d[1]]==1){
                    tmp=2;
                    y-=1;
                }
                else if(arr[x][y+d[0]]==1){
                    tmp=3;
                    y+=1;
                }
            }
            if(tmp==2){
                if(arr[x][y+d[1]]==0||y+d[1]==0){
                    tmp=1;
                    x-=1;
                }
                else
                    y-=1;
            }
            if(tmp==3){
                if(arr[x][y+d[0]]==0||y+d[0]==101){
                    tmp=1;
                    x-=1;
                }
                else
                    y+=1;
            }
        }
        cout<<y-1<<endl;
    }
    return 0;
}

 

ttps://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14mbSaAEwCFAYD&categoryId=AV14mbSaAEwCFAYD&categoryType=CODE

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com

 

반응형
LIST