본문 바로가기

problem solving

[SWEA]2814. 최장 경로

삽질....

0< N < 10인줄 알고 범위 잘못해서 몇시간동안 고생...

#include <iostream>
#include <algorithm>
 
using namespace std;
 
bool check[11][11];
bool posCheck[11];
int final;
int lastX;
 
void DFS(int x, int count) {
    if(count > final) {
        final = count;
        lastX = x;
    }
 
    for(int idx = 1; idx < 11; idx++) {
        if(!check[x][idx] || posCheck[idx]) { continue; }
        posCheck[idx] = true;
        DFS(idx, count + 1);
        posCheck[idx] = false;
    }
}
 
int main(int argc, char** argv)
{
    int test_case;
    int T;
    cin>>T;
 
    int N, M;
    for(test_case = 1; test_case <= T; ++test_case) {
        cin >> N >> M;
        for(int idx = 1; idx < 11; idx++) {
            fill(check[idx], check[idx] + 11false);
        }
        int y, x;
        for(int i = 0; i < M; i++) {
            cin >> y >> x;
            check[y][x] = true;
            check[x][y] = true;
        }
        lastX = x;
        final = 1;
 
        if(N > 1) {
            fill(posCheck, posCheck + 11false);
            posCheck[x] = true;
            DFS(x, 1);
            fill(posCheck, posCheck + 11false);
            posCheck[lastX] = true;
            DFS(lastX, 1);
        }
 
        cout << "#" << test_case << " " << final << "\n";
 
    }
    return 0;
}
cs

'problem solving' 카테고리의 다른 글

[ 프로그래머스]다음 큰 숫자  (0) 2020.03.28
[프로그래머스]단체사진 찍기  (0) 2020.03.28
[BOJ]1874번: 스택 수열  (0) 2020.03.14
[LeetCode]#2 Add Two Numbers  (0) 2020.03.14
[프로그래머스]디스크 컨트롤러  (0) 2020.03.14