기억보다는 기록을 해볼까

C++ 공부 18일차 (1012, 1541, 5525 (미완성)) 본문

백준으로 C++ 공부하기

C++ 공부 18일차 (1012, 1541, 5525 (미완성))

옥상에서 2021. 11. 10. 00:01
728x90

오늘 공부한 백준

1012, 1541, 5525 (미완성)

 

1012

DFS!!!!

for(int i = 1; i <= n; i++){
    for(int j = 1; j <= m; j++){
        if(vst[i][j] == 0 && map[i][j] == 1){
            cnt++;
            //bfs
            queue<pair<int, int>> q;
            vst[i][j] = 1;
            q.push({i, j});
            while(!q.empty()){
                auto [cy, cx] = q.front();
                q.pop();
                vst[cy][cx] = 1;

                for(int d = 0; d < 4; d++){
                    int ny = cy+dy[d], nx = cx+dx[d];
                    if(ny < 1 || ny > n || nx < 1 || nx > m) continue;
                    if(vst[ny][nx] == 0 && map[ny][nx] == 1){
                        vst[ny][nx] = 1;
                        q.push({ny, nx});
                    }
                }
            }
        }
    }
}

 

1541

문자열 파싱

   for(int i = 0; i < str.size(); i++){
        if(str[i] == '-' || str[i] == '+'){
            
            if(minus == true){
                sum -= tmp;
                tmp = 0;
            }
            else {
                sum += tmp;
                tmp = 0;
            }
            if(str[i] == '-'){
                minus = true;
            }
        }

        else {
            tmp *= 10; //previous number
            tmp += str[i] - '0'; //current number renewal
            if(i == str.size() - 1){
                if(minus == true) sum -= tmp;
                else sum += tmp;
            }
        }
    }
728x90
Comments