tensorflow를 설치하기 위해서 일단 python과 anaconda를 설치해야한다.

본 포스터의 환경은 python3.8 버전이다. 

ijsilver.tistory.com/54

 

mac os에서 Anaconda 설치하기

일단 아나콘다를 설치하기 위해서 아나콘다 사이트에 들어간다. www.anaconda.com/products/individual Anaconda | Individual Edition Anaconda's open-source Individual Edition is the easiest way to perform..

ijsilver.tistory.com

이 링크를 보고 아나콘다를 설치하면 된다.

아나콘다를 설치한 후

conda create -n tf2 python=3.8

명령어를 입력하여 tf2라는 가상환경을 만든다. 

그 후 conda activate tf2 명령어를 통해 가상환경을 실행 시킨다.

그리고 pip install tensorflow 명령어를 입력하여 tensorflow를 설치한다.

설치가 완료되면 아래 그림과 같이 나타난다.

설치가 완료되었다면 기본적인 명령어를 통해 실험을 해본다. 

python을 실행시키고

import tensorflow as tf

print(tf.reduce_sum(tf.random.normal([1000,1000])))

성공적으로 tensorflow가 설치됐다면 위처럼 결과가 나올것이다.

텐서플로우 설치 성공적! 바위~~

'Development > ML' 카테고리의 다른 글

mac에서 jupyter notebook 설치 방법!!  (0) 2021.01.11
mac os에서 Anaconda 설치하기  (0) 2020.12.29

일단 아나콘다를 설치하기 위해서 아나콘다 사이트에 들어간다.

 

www.anaconda.com/products/individual

 

Anaconda | Individual Edition

Anaconda's open-source Individual Edition is the easiest way to perform Python/R data science and machine learning on a single machine.

www.anaconda.com

들어가서 다운로드를 누르고 mac 전용으로 다운로드한다. 개인적으로 아래그림의 Graphic버전으로 받는것을 추천한다.

다운받은 후 아래그림처럼 실행될것이다.

상식적으로 계속 동의함 등을 눌러서 진행하면 된다.

이렇게 뜨면 완료가 된것이다. 

아나콘다 설치 끄읕~~~~

'Development > ML' 카테고리의 다른 글

mac에서 jupyter notebook 설치 방법!!  (0) 2021.01.11
mac os에서 tensorflow 설치하기  (0) 2020.12.29

서울대학교 컴퓨터공학과 대학원 입시 1차 전공관련 문제 면접이고 2차는 교수님 컨택이다.

보통 교수님 컨택을 먼저하는 경우가 대다수이지만, 나는 그럴 여유가 없어서 텝스성적 맞추고 1차면접을 치루고 2차로 교수님 컨텍을 했다.

텝스 성적과 교수님 컨텍을 개인적인 역량이고 1차 면접은 전공 공부를 잘 준비하면 된다.

이전에는 4과목중 3과목을 고르는 형태였지만, 이제는 2과목으로 줄어들었다.

본인은 2020-2학기 입학하는 입시를 치뤘고, 운영체제와 자료구조를 선택하였다.

자료구조 - DFS(그래프가 주어지고 한 노드에서 출발해서 나올수있는 결과값)

운영체제 - 프로세서 동기화, 멀티코어 환경에서 스케줄링 기법, NUMA등

자료구조 문제는 비교적 쉬웠지만, 운영체제는 문제 난이도가 꽤 되었다.

특히 멀티코어 환경에서 스케줄링 기법을 대답하라고 했을때 생각나는 스케줄링 이름이 없어서 당황했었다. 하지만 이름은 모르겠지만, 그 뒤를 잘 설명하니 이해해주셨다.

뒤에 들어올 후배들도 화이팅하고 잘 준비했으면 좋겠다.

tmux 사용하면서 지나간 내용을 보고싶은데 스크롤이 올라가지 않는다. 스크롤 모드로 넘어가야 한다.

컨트롤 + b 을 누르고 [ 를 누르면 스크롤 모드로 넘어간다.

이때 방향키나 마우스 스크롤을 올리면 올라간다.

그리고 q를 누르면 스크롤 모드에서 빠져나온다.

내가 몰라서 찾아본 티먹스,,,,

끄읕!!

이 문제는 크게 어렵지 않았다. 그냥 전형적인 재귀함수 문제였다. 

작은 예제부터 천천히 생각해본다면 쉽게 해결할수있었다.

모든 사분면이 같은 숫자면 그 숫자 출력하고 아니면 하나씩 분리해서 출력한다. 

2x2일때부터 생각해보면 쉽게 할수있다.

단, 입력받을때 string으로 받고 하나씩 넣어줘야하는 불편함이 있으니 주의해야한다.

#include <iostream>

using namespace std;

int board[64][64];

void dfs(int N, int x, int y){
    int ao=0;
    int az=0;
    if(N==1){
        cout<<board[x][y];
        return ;
    }
    for(int i=x;i<x+N;i++){
        for(int j=y;j<y+N;j++){
            if(board[i][j]==1){
                ao++;
            }
            else{
                az++;
            }
        }
    }
    if(ao==N*N){
        cout<<1;
    }
    else if(az==N*N){
        cout<<0;
    }
    else{
        cout<<"(";
        dfs(N/2,x,y);
        dfs(N/2,x,y+N/2);
        dfs(N/2,x+N/2,y);
        dfs(N/2,x+N/2,y+N/2);
        cout<<")";
    }

}

int main(){
    int N;
    string str;
    cin>>N;
    for(int i=0;i<N;i++){
        cin>>str;
        for(int j=0;j<N;j++){
            board[i][j]=str[j]-'0';
        }
    }
    dfs(N,0,0);
    return 0;
}

아마도 이 사이트에서 문제를 처음 풀어보는것같다. 연주가 문제를 추천해줘서 한번 풀어봤다.

구현 자체는 크게 어렵지 않았다. 그러나 어떻게 코드를 직관적이고 효율적으로 만들지 고민이 많이 들었지만, 일단 맞는것이 중요하다고 생각해 개판으로 짯다......

회전을 할때 진짜 회전을 하는것이 아니라 head를 지정해주고 head의 값만 움직여주면 간편하다.

그렇게 오른쪽 접하는 값 왼쪽 접하는 값을 비교하고 돌릴지 말지 노가다 해주면 끝,,,

#include <iostream>

using namespace std;

int mag[4][8];
int head[4];

int get_right(int idx){
    int temp=head[idx-1];
    for(int i=0;i<2;i++){
        if(temp==7)temp=0;
        else temp++;
    }
    return mag[idx-1][temp];
}

int get_left(int idx){
    int temp=head[idx-1];
    for(int i=0;i<6;i++){
        if(temp==7)temp=0;
        else temp++;
    }
    return mag[idx-1][temp];
}

void turn(int idx, int dir){
    if(dir==1){
        if(head[idx-1]==0) head[idx-1]=7;
        else head[idx-1]--;
    }
    else{
        if(head[idx-1]==7) head[idx-1]=0;
        else head[idx-1]++;
    }
}

int main(void){
    int T,K;
    int idx, dir;
    int time=0;
    cin>>T;

    while(T--){
        time++;
        cin>>K;

        for(int i=0;i<4;i++){
            head[i]=0;
        }
        for(int i=0;i<4;i++){
            for(int j=0;j<8;j++){
                cin>>mag[i][j];
            }
        }
        for(int i=0;i<K;i++){
            cin>>idx>>dir;
            bool same[3]={false,false,false};

            if(get_right(1)==get_left(2))same[0]=true;
            if(get_right(2)==get_left(3))same[1]=true;
            if(get_right(3)==get_left(4))same[2]=true;

            if(idx==1){
                turn(idx, dir);
                if(same[0]==false){
                    turn(idx+1,dir*-1);
                    if(same[1]==false){
                        turn(idx+2,dir);
                        if(same[2]==false){
                            turn(idx+3,dir*-1);
                        }
                    }
                }
            }
            else if(idx==2){
                turn(idx,dir);
                if(same[0]==false){
                    turn(idx-1,dir*-1);
                }
                if(same[1]==false){
                    turn(idx+1,dir*-1);
                    if(same[2]==false){
                        turn(idx+2,dir);
                    }
                }
            }
            else if(idx==3){
                turn(idx,dir);
                if(same[2]==false){
                    turn(idx+1,dir*-1);
                }
                if(same[1]==false){
                    turn(idx-1,dir*-1);
                    if(same[0]==false){
                        turn(idx-2,dir);
                    }
                }
            }
            else{
                turn(idx,dir);
                if(same[2]==false){
                    turn(idx-1,dir*-1);
                    if(same[1]==false){
                        turn(idx-2,dir);
                        if(same[0]==false){
                            turn(idx-3,dir*-1);
                        }
                    }
                }
            }
        }
        int total=0;
        for(int i=0;i<4;i++){
            if(mag[i][head[i]]==1){
                int score=1;
                for(int j=0;j<i;j++) score*=2;
                total+=score;
            }
        }
        cout<<"#"<<time<<" "<<total<<endl;
    }
}

이 문제는 이번에 삼성 sw역량평가 오전 1번 문제였다. 이번에 나도 코딩테스트를 봤는데 이문제를 풀었었다.

현장에서 test case를 모두 통과했지만, 다시한번 풀어보고 확인해보았다.

결과는 성-공!

구현 자체는 별로 어렵지않았다. 순서대로 한바퀴 돌려주고 사람 이동시키고 내리고 올리고 하면되기때문에 간단했다.

#include <iostream>

using namespace std;

struct block{
    bool human;
    int dur;
};

block belt[202];

void init(int n){
    for(int i=0;i<2*n;i++){
        belt[i].human=false;
        belt[i].dur=0;
    }
}

int main(void){
    int N,K;
    cin>>N>>K;
    init(N);
    for(int i=0;i<2*N;i++){
        cin>>belt[i].dur;
    }
    int time=0;
    while(1){
        time++;
        //한바귀 회전
        block c_belt[202];
        for(int i=0;i<2*N-1;i++){
            c_belt[i+1]=belt[i];
        }
        c_belt[0]=belt[2*N-1];

        for(int i=0;i<2*N;i++){
            belt[i]=c_belt[i];
        }

        if(belt[N-1].human == true) belt[N-1].human=false;

        for(int i=N-2;i>=0;i--){
            if(belt[i+1].human == false and belt[i].human==true and belt[i+1].dur >0){
                belt[i].human=false;
                belt[i+1].human=true;
                belt[i+1].dur--;
            }
        }

        if(belt[N-1].human==true) belt[N-1].human=false;

        if(belt[0].human == false and belt[0].dur>0){
            belt[0].human=true;
            belt[0].dur--;
        }
        int cnt=0;
        for(int i=0;i<2*N;i++){
            if(belt[i].dur==0)cnt++;
        }
        if(cnt>=K)break;

    }
    cout<<time<<endl;

}

하 오늘 청소년 어른 상어를 풀었다..... 그래도 어른상어는 난이도가 조금 낮았던것같다.

그래도 코드는 엄청 길어졌다는거.......

이건 조건이 많아서 헷갈릴수있다. 차근차근하면 되는것같다.

1만 남을때까지 하는것이기 때문에 반복문을 통해서 1만 남을때까지 반복한다.

그다음에 냄새를 뿌리고, 상어가 이동한다. 이때 우선순위에따라서 잘 이동시켜준다.

비어있을경우 한번, 비어있지 않을경우 한번 해준다.

여기서 배열을 복사해주는게 좋은게 유효성 검사를 할때 앞에서 바뀌게 되면 일관성이 없어진다.

그래서 카피를 만들어 줘야한다.

끝..

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct shark {
    int x;
    int y;
    int dir;//쳐다보고있는 방향
    bool move = false;
    int prior[4][4];
    bool out = false;
};
struct pos_info {
    int smell_cnt;
    int smell_num;
    int shark_num;
};



int main() {
    int N, M, K;
    int time = 0;
    pos_info board[20][20];
    pos_info c_board[20][20];
    shark s_list[400];
    cin >> N >> M >> K;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cin >> board[i][j].shark_num;
            board[i][j].smell_cnt = 0;
            board[i][j].smell_num = 0;
            if (board[i][j].shark_num != 0) {
                s_list[board[i][j].shark_num].x = i;
                s_list[board[i][j].shark_num].y = j;
            }
        }
    }
    for (int i = 1; i <= M; i++)cin >> s_list[i].dir; // 쳐다보고있는 방향
    for (int i = 1; i <= M; i++) {
        for (int j = 0; j < 4; j++) {
            for (int k = 0; k < 4; k++) {
                cin >> s_list[i].prior[j][k];
            }
        }
    }//입력받기

    while (1) {
        int s_cnt = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (board[i][j].shark_num != 0)s_cnt++;
            }
        }
        if (s_cnt == 1) {
            cout << time << endl;
            break;
        }

        for (int i = 1; i <= M; i++) { 
            if (s_list[i].out)continue;
            s_list[i].move = false; 
        }
        
        for (int i = 1; i <= M; i++) {
            shark temp = s_list[i];
            if (temp.out)continue;
            board[temp.x][temp.y].smell_cnt = K;
            board[temp.x][temp.y].smell_num = i;
        }//방구끼고

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                c_board[i][j] = board[i][j];
            }
        }

        for (int i = M; i >= 1; i--) {
            shark temp = s_list[i];
            if (temp.out)continue;
            int dir_prior;
            for (int j = 0; j < 4; j++) {
                dir_prior = temp.prior[temp.dir - 1][j];//우선순위 순서대로 간다
                if (dir_prior == 1) {
                    if (temp.x - 1 < 0)continue;
                    if (board[temp.x - 1][temp.y].smell_cnt == 0) { // 냄새가 비어있을때
                        if (c_board[temp.x - 1][temp.y].shark_num != 0) {
                            s_list[c_board[temp.x - 1][temp.y].shark_num].out = true;
                        }
                        c_board[temp.x - 1][temp.y].shark_num = i;
                        c_board[temp.x][temp.y].shark_num = 0;
                        s_list[i].x--;
                        s_list[i].move = true;
                        s_list[i].dir = 1;
                        break;
                    }
                }//상
                else if (dir_prior == 2) {
                    if (temp.x + 1 > N - 1)continue;
                    if (board[temp.x + 1][temp.y].smell_cnt == 0) {
                        if (c_board[temp.x + 1][temp.y].shark_num != 0) {
                            s_list[c_board[temp.x + 1][temp.y].shark_num].out = true;
                        }
                        c_board[temp.x + 1][temp.y].shark_num = i;
                        c_board[temp.x][temp.y].shark_num = 0;
                        s_list[i].x++;
                        s_list[i].move = true;
                        s_list[i].dir = 2;
                        break;
                    }
                }//하
                else if (dir_prior == 3) {
                    if (temp.y - 1 < 0)continue;
                    if (board[temp.x][temp.y - 1].smell_cnt == 0) {
                        if (c_board[temp.x][temp.y-1].shark_num != 0) {
                            s_list[c_board[temp.x][temp.y-1].shark_num].out = true;
                        }
                        c_board[temp.x][temp.y - 1].shark_num = i;
                        c_board[temp.x][temp.y].shark_num = 0;
                        s_list[i].y--;
                        s_list[i].move = true;
                        s_list[i].dir = 3;
                        break;
                    }
                }//왼
                else {
                    if (temp.y + 1 > N - 1)continue;
                    if (board[temp.x][temp.y + 1].smell_cnt == 0) {
                        if (c_board[temp.x][temp.y+1].shark_num != 0) {
                            s_list[c_board[temp.x][temp.y+1].shark_num].out = true;
                        }
                        c_board[temp.x][temp.y + 1].shark_num = i;
                        c_board[temp.x][temp.y].shark_num = 0;
                        s_list[i].y++;
                        s_list[i].move = true;
                        s_list[i].dir = 4;
                        break;
                    }
                }//오
            }
        }//비어있을때 이동

        for (int i = M; i >= 1; i--) {
            if (s_list[i].out)continue;
            if (!s_list[i].move) { //냄새가 다 차있어서 이동 못할때
                shark temp = s_list[i];
                int dir_prior;
                for (int j = 0; j < 4; j++) {
                    dir_prior = temp.prior[temp.dir - 1][j];
                    if (dir_prior == 1) {
                        if (temp.x - 1 < 0)continue;
                        if (board[temp.x - 1][temp.y].smell_num == i) {
                            if (c_board[temp.x-1][temp.y].shark_num != 0) {
                                s_list[c_board[temp.x-1][temp.y].shark_num].out = true;
                            }
                            c_board[temp.x - 1][temp.y].shark_num = i;
                            c_board[temp.x][temp.y].shark_num = 0;
                            s_list[i].x--;
                            s_list[i].move = true;
                            s_list[i].dir = 1;
                            break;
                        }
                    }
                    else if (dir_prior == 2) {
                        if (temp.x + 1 > N - 1)continue;
                        if (board[temp.x + 1][temp.y].smell_num == i) {
                            if (c_board[temp.x + 1][temp.y].shark_num != 0) {
                                s_list[c_board[temp.x + 1][temp.y].shark_num].out = true;
                            }
                            c_board[temp.x + 1][temp.y].shark_num = i;
                            c_board[temp.x][temp.y].shark_num = 0;
                            s_list[i].x++;
                            s_list[i].move = true;
                            s_list[i].dir = 2;
                            break;
                        }
                    }
                    else if (dir_prior == 3) {
                        if (temp.y - 1 < 0)continue;
                        if (board[temp.x][temp.y - 1].smell_num == i) {
                            if (c_board[temp.x][temp.y - 1].shark_num != 0) {
                                s_list[c_board[temp.x][temp.y - 1].shark_num].out = true;
                            }
                            c_board[temp.x][temp.y - 1].shark_num = i;
                            c_board[temp.x][temp.y].shark_num = 0;
                            s_list[i].y--;
                            s_list[i].move = true;
                            s_list[i].dir = 3;
                            break;
                        }
                    }
                    else {
                        if (temp.y + 1 > N - 1)continue;
                        if (board[temp.x][temp.y + 1].smell_num == i) {
                            if (c_board[temp.x][temp.y + 1].shark_num != 0) {
                                s_list[c_board[temp.x][temp.y + 1].shark_num].out = true;
                            }
                            c_board[temp.x][temp.y + 1].shark_num = i;
                            c_board[temp.x][temp.y].shark_num = 0;
                            s_list[i].y++;
                            s_list[i].move = true;
                            s_list[i].dir = 4;
                            break;
                        }
                    }
                }
            }
        }
        //이동

       

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                board[i][j] = c_board[i][j];
                if (board[i][j].shark_num == 0 and board[i][j].smell_cnt>0) {
                    board[i][j].smell_cnt--;
                    if (board[i][j].smell_cnt == 0)board[i][j].smell_num = 0;
                }
            }
        }//상어없는 모든칸에 냄새 하나줄이기

       

        time++;
        if (time > 1000) {
            cout << -1 << endl;
            return 0;
        }
    }
}

삼성 코딩테스트 준비하면서 푼 문제이다.

다른사람들은 다들 코드를 짧게 효율적으로 잘짜던데 나는 왜 안될까....

그래도 맞긴 맞았다.

dfs로 풀었는데, 트리구조에서 모든 노드 탐색하고 최소값 갱신해주는 형태로 풀었다.

 

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct pos {
    int x;
    int y;
};

int board[8][8];
int N, M;
int min_cnt = 100;

void count_zero(int p_board[8][8]) {
    int cnt = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (p_board[i][j] == 0)cnt++;
        }
    }
    if (min_cnt > cnt)min_cnt = cnt;
}


void dfs(int p_board[8][8], vector<pos> vec1, vector<pos> vec2, vector<pos> vec3, vector<pos> vec4, vector<pos> vec5) {
    int c_board[8][8];
    while (!vec5.empty()) {
        pos cur = vec5.back();
        vec5.pop_back();
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                c_board[i][j] = p_board[i][j];
            }
        }
        for (int i = cur.x; i < N; i++) {
            if (c_board[i][cur.y] == 6) break;
            else {
                c_board[i][cur.y] = 7;
            }
        }
        for (int i = cur.x; i >= 0; i--) {
            if (c_board[i][cur.y] == 6) break;
            else {
                c_board[i][cur.y] = 7;
            }
        }
        for (int i = cur.y; i < M; i++) {
            if (c_board[cur.x][i] == 6)break;
            else {
                c_board[cur.x][i] = 7;
            }
        }
        for (int i = cur.y; i >= 0; i--) {
            if (c_board[cur.x][i] == 6)break;
            else {
                c_board[cur.x][i] = 7;
            }
        }
        dfs(c_board,vec1,vec2,vec3,vec4,vec5);
    }

    while (!vec4.empty()) {
        pos cur = vec4.back();
        vec4.pop_back();
        for (int a = 0; a < 4; a++) {
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    c_board[i][j] = p_board[i][j];
                }
            }
            if (a == 0) {
                for (int i = cur.x; i >= 0; i--) {
                    if (c_board[i][cur.y] == 6)break;
                    c_board[i][cur.y] = 7;
                }
                for (int i = cur.x; i < N; i++) {
                    if (c_board[i][cur.y] == 6)break;
                    c_board[i][cur.y] = 7;
                }
                for (int i = cur.y; i >= 0; i--) {
                    if (c_board[cur.x][i] == 6)break;
                    c_board[cur.x][i] = 7;
                }
            }
            else if (a == 1) {
                for (int i = cur.x; i >= 0; i--) {
                    if (c_board[i][cur.y] == 6)break;
                    c_board[i][cur.y] = 7;
                }
                for (int i = cur.x; i < N; i++) {
                    if (c_board[i][cur.y] == 6)break;
                    c_board[i][cur.y] = 7;
                }
                for (int i = cur.y; i < M; i++) {
                    if (c_board[cur.x][i] == 6)break;
                    c_board[cur.x][i] = 7;
                }
            }
            else if (a == 2) {
                for (int i = cur.x; i < N; i++) {
                    if (c_board[i][cur.y] == 6)break;
                    c_board[i][cur.y] = 7;
                }
                for (int i = cur.y; i < M; i++) {
                    if (c_board[cur.x][i] == 6)break;
                    c_board[cur.x][i] = 7;
                }
                for (int i = cur.y; i >= 0; i--) {
                    if (c_board[cur.x][i] == 6)break;
                    c_board[cur.x][i] = 7;
                }
            }
            else if (a == 3) {
                for (int i = cur.x; i >= 0; i--) {
                    if (c_board[i][cur.y] == 6)break;
                    c_board[i][cur.y] = 7;
                }
                for (int i = cur.y; i < M; i++) {
                    if (c_board[cur.x][i] == 6)break;
                    c_board[cur.x][i] = 7;
                }
                for (int i = cur.y; i >= 0; i--) {
                    if (c_board[cur.x][i] == 6)break;
                    c_board[cur.x][i] = 7;
                }
            }
            dfs(c_board, vec1, vec2, vec3, vec4, vec5);
        }
    }

    while (!vec3.empty()) {
        pos cur = vec3.back();
        vec3.pop_back();
        for (int a = 0; a < 4; a++) {
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    c_board[i][j] = p_board[i][j];
                }
            }
            if (a == 0) {
                for (int i = cur.x; i < N; i++) {
                    if (c_board[i][cur.y] == 6)break;
                    c_board[i][cur.y] = 7;
                }
                for (int i = cur.y; i < M; i++) {
                    if (c_board[cur.x][i] == 6)break;
                    c_board[cur.x][i] = 7;
                }
            }
            else if (a == 1) {
                for (int i = cur.x; i >=0; i--) {
                    if (c_board[i][cur.y] == 6)break;
                    c_board[i][cur.y] = 7;
                }
                for (int i = cur.y; i < M; i++) {
                    if (c_board[cur.x][i] == 6)break;
                    c_board[cur.x][i] = 7;
                }
            }
            else if (a == 2) {
                for (int i = cur.x; i < N; i++) {
                    if (c_board[i][cur.y] == 6)break;
                    c_board[i][cur.y] = 7;
                }
                for (int i = cur.y; i >=0; i--) {
                    if (c_board[cur.x][i] == 6)break;
                    c_board[cur.x][i] = 7;
                }
            }
            else if (a == 3) {
                for (int i = cur.x; i >= 0; i--) {
                    if (c_board[i][cur.y] == 6)break;
                    c_board[i][cur.y] = 7;
                }
                for (int i = cur.y; i >= 0; i--) {
                    if (c_board[cur.x][i] == 6)break;
                    c_board[cur.x][i] = 7;
                }
            }
            dfs(c_board, vec1, vec2, vec3, vec4, vec5);
        }
    }

    while (!vec2.empty()) {
        pos cur = vec2.back();
        vec2.pop_back();
        for (int a = 0; a < 2; a++) {
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    c_board[i][j] = p_board[i][j];
                }
            }
            if (a == 0) {
                for (int i = cur.x; i < N; i++) {
                    if (c_board[i][cur.y] == 6)break;
                    else {
                        c_board[i][cur.y] = 7;
                    }
                }
                for (int i = cur.x; i >=0; i--) {
                    if (c_board[i][cur.y] == 6)break;
                    else {
                        c_board[i][cur.y] = 7;
                    }
                }
            }
            else if (a == 1) {
                for (int i = cur.y; i < M; i++) {
                    if (c_board[cur.x][i] == 6)break;
                    else {
                        c_board[cur.x][i] = 7;
                    }
                }
                for (int i = cur.y; i >= 0; i--) {
                    if (c_board[cur.x][i] == 6)break;
                    else {
                        c_board[cur.x][i] = 7;
                    }
                }
            }
            dfs(c_board, vec1, vec2, vec3, vec4, vec5);
        }
    }

    while (!vec1.empty()) {
        pos cur = vec1.back();
        vec1.pop_back();
        for (int a = 0; a < 4; a++) {
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    c_board[i][j] = p_board[i][j];
                }
            }
            if (a == 0) {
                for (int j = cur.x; j < N; j = j + 1) {
                    if (c_board[j][cur.y] == 6)break;
                    else {
                        c_board[j][cur.y] = 7;
                    }
                }
            }
            else if (a == 2) {
                for (int j = cur.x; j >= 0; j = j - 1) {
                    if (c_board[j][cur.y] == 6)break;
                    else {
                        c_board[j][cur.y] = 7;
                    }
                }
            }
            else if (a == 3) {
                for (int j = cur.y; j < M; j++) {
                    if (c_board[cur.x][j] == 6)break;
                    else {
                        c_board[cur.x][j] = 7;
                    }
                }
            }
            else {
                for (int j = cur.y; j >= 0; j--) {
                    if (c_board[cur.x][j] == 6)break;
                    else {
                        c_board[cur.x][j] = 7;
                    }
                }
            }
            dfs(c_board,vec1,vec2,vec3,vec4,vec5);
        }
    }
    count_zero(p_board);
}


int main() {
    vector<pos> vec1, vec2, vec3, vec4, vec5;
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> board[i][j];
            if (board[i][j] == 1) {
                vec1.push_back({ i,j,0 });
            }
            else if (board[i][j] == 2) {
                vec2.push_back({ i,j });
            }
            else if (board[i][j] == 3) {
                vec3.push_back({ i,j });
            }
            else if (board[i][j] == 4) {
                vec4.push_back({ i,j });
            }
            else if (board[i][j] == 5) {
                vec5.push_back({ i,j });
            }
        }
    }

    dfs(board,vec1,vec2,vec3,vec4,vec5);
    cout << min_cnt << endl;
}

이문제를 푸는데 정말 오랜시간이 걸린것같다. 혼자서 풀다가 힘들어서 다른사람의 블로그를 뒤져봤다.

 

정말 오랜만에 푸는거라 코드도 노가다가 정말 많았다.

 

이문제는 물고기의 이동이 첫번째 난관이고 상어가 먹는것을 DFS형태로 완전탐색을 해주면 된다.

트리구조로 이루어진다. 전역변수를 max_eat을 두고 큰값이 나오면 갱신해 주는 형태로 했다. 코드가 굉장히 비효율적으로 짜졌지만 직관적이라 이해하는데 도움은 될것같다. 다음에 다시 리팩토링해서 올리면 좋을것같다......

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct fish {
	int x;
	int dir;
};
struct pos {
	int x;
	int y;
};

int max_eat = 0;

int xdir[9] = {0,-1,-1,0,1,1,1,0,-1};
int ydir[9] = {0,0,-1,-1,-1,0,1,1,1};

void dfs(fish board[4][4], int eat_num, int ox, int oy, int nx, int ny) {
	eat_num += board[nx][ny].x;
	board[nx][ny].x = 17;
	if (ox != -1 and oy != -1) {
		board[ox][oy].x = 0;
		board[ox][oy].dir = 0;
	}
	fish temp;
	for (int k = 1; k <= 16; k++) {
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (board[i][j].x == k) {
					//fish move
					while (1) {
						if (i + xdir[board[i][j].dir] < 0 or i + xdir[board[i][j].dir] >= 4 or j + ydir[board[i][j].dir] < 0 or j + ydir[board[i][j].dir] >= 4) {
							board[i][j].dir = board[i][j].dir+1;
							if (board[i][j].dir == 9)board[i][j].dir = 1;
							continue;
						}
						else if (board[i + xdir[board[i][j].dir]][j + ydir[board[i][j].dir]].x == 17) {
							board[i][j].dir = board[i][j].dir + 1;
							if (board[i][j].dir == 9)board[i][j].dir = 1;

							continue;
						}
						else {
							temp = board[i + xdir[board[i][j].dir]][j + ydir[board[i][j].dir]];
							board[i + xdir[board[i][j].dir]][j + ydir[board[i][j].dir]] = board[i][j];
							board[i][j] = temp;
							goto silver;
						}
					}
				}
			}
		}
	silver:;
	}

	//먹을거 결정
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			if (board[i][j].x == 17) {
				if (board[i][j].dir == 1) {
					vector<pos> vec;
					if (i - 1 < 0) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					int t_i = i - 1;
					while (1) {
						if (board[t_i][j].x != 0)vec.push_back({ t_i,j });
						t_i--;
						if (t_i < 0)break;
					}
					if (vec.empty()) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					while (!vec.empty()) {
						pos cur = vec.back();
						vec.pop_back();
						fish temp_board[4][4];
						for (int k = 0; k < 4; k++) {
							for (int t = 0; t < 4; t++) {
								temp_board[k][t] = board[k][t];
							}
						}
						dfs(temp_board, eat_num, i, j, cur.x, cur.y);
					}

				}
				else if (board[i][j].dir == 2) {
					vector<pos> vec;
					if (i - 1 < 0 or j - 1 < 0) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					int t_i = i - 1, t_j = j - 1;
					while (1) {
						if (board[t_i][t_j].x != 0)vec.push_back({ t_i,t_j });
						t_i--;
						t_j--;
						if (t_i < 0 or t_j < 0)break;
					}
					if (vec.empty()) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					while (!vec.empty()) {
						pos cur = vec.back();
						vec.pop_back();
						fish temp_board[4][4];
						for (int k = 0; k < 4; k++) {
							for (int t = 0; t < 4; t++) {
								temp_board[k][t] = board[k][t];
							}
						}
						dfs(temp_board, eat_num, i, j, cur.x, cur.y);
					}
				}
				else if (board[i][j].dir == 3) {
					vector<pos> vec;
					if (j - 1 < 0) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					int t_j = j - 1;
					while (1) {
						if (board[i][t_j].x != 0)vec.push_back({ i,t_j });
						t_j--;
						if (t_j < 0)break;
					}
					if (vec.empty()) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					while (!vec.empty()) {
						pos cur = vec.back();
						vec.pop_back();
						fish temp_board[4][4];
						for (int k = 0; k < 4; k++) {
							for (int t = 0; t < 4; t++) {
								temp_board[k][t] = board[k][t];
							}
						}
						dfs(temp_board, eat_num, i, j, cur.x, cur.y);
					}
				}
				else if (board[i][j].dir == 4) {
					vector<pos> vec;
					if (i + 1 >= 4 or j - 1 < 0) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					int t_i = i + 1, t_j = j - 1;
					while (1) {
						if (board[t_i][t_j].x != 0)vec.push_back({ t_i,t_j });
						t_i++;
						t_j--;
						if (t_i >= 4 or t_j < 0)break;
					}
					if (vec.empty()) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					while (!vec.empty()) {
						pos cur = vec.back();
						vec.pop_back();
						fish temp_board[4][4];
						for (int k = 0; k < 4; k++) {
							for (int t = 0; t < 4; t++) {
								temp_board[k][t] = board[k][t];
							}
						}
						dfs(temp_board, eat_num, i, j, cur.x, cur.y);
					}
				}
				else if (board[i][j].dir == 5) {
					vector<pos> vec;
					if (i + 1 >= 4) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					int t_i = i + 1;
					while (1) {
						if (board[t_i][j].x != 0)vec.push_back({ t_i, j });
						t_i++;
						if (t_i >= 4)break;
					}
					if (vec.empty()) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					while (!vec.empty()) {
						pos cur = vec.back();
						vec.pop_back();
						fish temp_board[4][4];
						for (int k = 0; k < 4; k++) {
							for (int t = 0; t < 4; t++) {
								temp_board[k][t] = board[k][t];
							}
						}
						dfs(temp_board, eat_num, i, j, cur.x, cur.y);
					}
				}
				else if (board[i][j].dir == 6) {
					vector<pos> vec;
					if (i + 1 >= 4 or j + 1 >= 4) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					int t_i = i + 1, t_j = j + 1;
					while (1) {
						if (board[t_i][t_j].x != 0)vec.push_back({ t_i,t_j });
						t_i++;
						t_j++;
						if (t_i >= 4 or t_j >= 4)break;
					}
					if (vec.empty()) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					while (!vec.empty()) {
						pos cur = vec.back();
						vec.pop_back();
						fish temp_board[4][4];
						for (int k = 0; k < 4; k++) {
							for (int t = 0; t < 4; t++) {
								temp_board[k][t] = board[k][t];
							}
						}
						dfs(temp_board, eat_num, i, j, cur.x, cur.y);
					}
				}
				else if (board[i][j].dir == 7) {
					vector<pos> vec;
					if (j + 1 >= 4) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					int t_j = j + 1;
					while (1) {
						if (board[i][t_j].x != 0)vec.push_back({ i,t_j });
						t_j++;
						if (t_j >= 4)break;
					}
					if (vec.empty()) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					while (!vec.empty()) {
						pos cur = vec.back();
						vec.pop_back();
						fish temp_board[4][4];
						for (int k = 0; k < 4; k++) {
							for (int t = 0; t < 4; t++) {
								temp_board[k][t] = board[k][t];
							}
						}
						dfs(temp_board, eat_num, i, j, cur.x, cur.y);
					}
				}
				else if (board[i][j].dir == 8) {
					vector<pos> vec;
					if (i - 1 < 0 or j + 1 >= 4) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					int t_i = i - 1, t_j = j + 1;
					while (1) {
						if (board[t_i][t_j].x != 0)vec.push_back({ t_i,t_j });
						t_i--;
						t_j++;
						if (t_i < 0 or t_j >= 4)break;
					}
					if (vec.empty()) {
						if (max_eat < eat_num)max_eat = eat_num;
						return;
					}
					while (!vec.empty()) {
						pos cur = vec.back();
						vec.pop_back();
						fish temp_board[4][4];
						for (int k = 0; k < 4; k++) {
							for (int t = 0; t < 4; t++) {
								temp_board[k][t] = board[k][t];
							}
						}
						dfs(temp_board, eat_num, i, j, cur.x, cur.y);
					}
				}
			}
		}
	}
}

int main() {
	fish arr[4][4];
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			cin >> arr[i][j].x >> arr[i][j].dir;
		}
	}//입력받기
	dfs(arr, 0, -1, -1, 0, 0);
	cout << max_eat << endl;
}

+ Recent posts