๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Summer/Winder coding ๋ฐฉ๋ฌธ ๊ธธ์ด level2

by Jouureee 2021. 4. 30.

๋ฌธ์ œ :

programmers.co.kr/learn/courses/30/lessons/49994

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฐฉ๋ฌธ ๊ธธ์ด

 

programmers.co.kr

 

๋ถ„์„ :

์ฒ˜์Œ์— dfs๋ฌธ์ œ์ธ์ค„ ์•Œ๊ณ  ๋งˆ์นจ ์˜ค๋Š˜ dfs ์Šคํ„ฐ๋”” ํ•ด์„œ dfs ์งœ๋‹ค๊ฐ€ stack์— push ํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†์Œ์„ ๋Š๋ผ๊ณ  

๋‹จ์ˆœ ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒํผ for๋ฌธ์„ ๋Œ์•„ answer ๊ฐ’์„ ์ฆ๊ฐ€ ํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ๊ฐ„์†Œํ™” ํ•˜์˜€๋‹ค.

 

1. ์ฒซ ์ ‘๊ทผ ๋ฐฉ์‹

๊ฐ”๋˜ ๊ฐ„์„ ์€ ๋˜ ๊ฐˆ ์ˆ˜ ์—†์œผ๋‹ˆ 2์ฐจ์› visit ๋ฐฐ์—ด ์ƒ์„ฑ ํ›„ false์ธ ์ขŒํ‘œ์— ๋Œ€ํ•ด์„œ๋งŒ answer ๊ฐ’ ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ 

ํ•˜์ง€๋งŒ ์ด ๋ฐฉ์‹์€ 1๋ฒˆ์‹œ, ๋„์ฐฉ ๋…ธ๋“œ๋กœ ๋ฐฉ๋ฌธํ–ˆ๋˜ (5,6)๋ฒˆ์ง€๋ฅผ 7๋ฒˆ ์ž‘์—… ์ˆ˜ํ–‰์‹œ (5,6)๋ฒˆ์ด ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๊ฐ€ ๋˜์—ˆ์œผ๋ฏ€๋กœ 7๋ฒˆ ๊ฐ„์„ ์„ ๋งŒ๋“ค์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ์ •์  ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•˜๋ฉด ์•ˆ๋œ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ซ๊ณ  ๊นŠ์€ ๊ณ ๋‡Œ์— ๋น ์กŒ๋‹ค ... 

 

2. ๋‘๋ฒˆ์งธ ์ ‘๊ทผ ๋ฐฉ์‹ 

์•„ ๊ทธ๋Ÿฌ๋ฉด ์ด์ „ ๋ฐฉ๋ฌธ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ  ๋‹ค์Œ ๋…ธ๋“œ๋Š” ์ด๋ฏธ ๋ฐฉ๋ฌธ๋œ ์ƒํƒœ์ž„์—๋„ 7๋ฒˆ ๋…ธ๋“œ๋ฅผ ์œ„ํ•ด ๋‹ค์‹œ ๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ์ถ”๊ฐ€ํ•ด์ฃผ์ž !

ํ•˜์ง€๋งŒ ์ด ์—ญ์‹œ 

๊ฐ”๋˜ 9๋ฒˆ, 8๋ฒˆ ๊ฐ„์„ ์€ ํฌํ•จํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š”๋ฐ ์œ„ ์ถ”๊ฐ€ํ•œ ์กฐ๊ฑด์— ์˜ํ•ด ํฌํ•จ๋˜๊ฒŒ ๋œ๋‹ค ๐Ÿ˜ฉ์œผ .. ๊ทธ๋Ÿฌ๋ฉด ์–ด์ฉŒ๋ผ๊ตฌ ...

 

3๋ฒˆ์งธ ์ ‘๊ทผ ๋ฐฉ์‹

์ •์  ๊ธฐ๋ฐ˜์ด ์•„๋‹ˆ๋ผ ๊ฐ„์„  ๋ฐฉ์‹์ด ํ•„์š”ํ• ๊ฑฐ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. 

์ด ์ „ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•˜๋˜ ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ „์—๋„ ๊ฐ”๊ณ  ์ด๋ฒˆ์—๋„ ๊ฐ„ ๋…ธ๋“œ๊ฐ€ true์ธ ๊ฒฝ์šฐ๋งŒ ๋ฐฐ์ œํ•˜๊ณ  ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค ์‹ถ์—ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ฝ”๋“œ๋กœ ๐Ÿ‘‰๐Ÿป๐Ÿ‘‰๐Ÿป

์‹œ์ž‘ ๋…ธ๋“œ์™€ ๋„์ฐฉ ๋…ธ๋“œ๋ฅผ pair ๋กœ ๋ฌถ์–ด key ๊ฐ’์œผ๋กœ ๋งŒ๋“ค์—ˆ๊ณ  ์ด๋ฅผ bool ๊ฐ’์„ ๊ฐ€์ง€๋Š” edge map์„ ํ•˜๋‚˜ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  false์ผ๋•Œ์— ๋Œ€ํ•ด true๋กœ ๊ฐ„ ํ‘œ์‹œ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

 

 

c++ ์ฝ”๋“œ :

//
//  W] 2018_gameLine.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/04/30.
//

#include <stdio.h>
#include <stack>
#include <iostream>
#include <string.h>
#include <map>

using namespace std;

string dirs[3] = {"ULURRDLLU", "LRLRL", "LULLLLLLU" };


void make_line_dfs(){
    int cnt = 0;
    map<pair<pair<int, int>, pair<int, int>>, bool> edge;
    
    int dx = 5;
    int dy = 5;
    
    for(int i = 0; i < dirs[0].length(); i++){
       int priorX = dx;
       int priorY = dy;
       switch (dirs[0][i]) {
           case 'U':
               if( dy + 1 > 10){
                   continue;
               }
               dy += 1;
               break;
           case 'D':
                if(dy - 1 < 0){
                   continue;
               }
               dy -= 1;
              
               break;
           case 'R':
               if(dx + 1 > 10){
                   continue;
               }
               dx += 1;
               break;
           case 'L':
               if(dx - 1 < 0){
                   continue;
               }
               dx -= 1;
               break;
           default:
               break;
        }
        
        //if(dx < 0 || dy < 0 || dx >= 10 || dy >= 10) continue; // ๊ฑธ์–ด์ฃผ๋‹ˆ false๊ฐ€ ๋–ด๋‹ค ์™œ์ผ๊นŒ ใ… 
        if(edge[make_pair(make_pair(priorX, priorY), make_pair(dx, dy))] == true) continue;
        
        //false ์ด๋ฉด
        edge[make_pair(make_pair(priorX, priorY), make_pair(dx, dy))] = true;
        edge[make_pair(make_pair(dx, dy), make_pair(priorX, priorY))] = true;
        cnt++;
       

        
    }
    cout << "cnt : " << cnt << '\n';
        
    }
    
    
    
    

int main(){
    make_line_dfs();

    
    int answer = 0;
    
    return 0;
}

 

 

๊ฒฐ๊ณผ :

๋Œ“๊ธ€