14889,15661_스타트와 링크 및 링크와 스타트

이준혁·2025년 12월 5일

code

목록 보기
7/7


예제 입력 1
4
0 1 2 3
4 0 5 6
7 1 0 2
3 4 5 0
예제 출력 1
0
예제 입력 2
6
0 1 2 3 4 5
1 0 2 3 4 5
1 2 0 3 4 5
1 2 3 0 4 5
1 2 3 4 0 5
1 2 3 4 5 0
예제 출력 2
2
예제 입력 3
8
0 5 4 5 4 5 4 5
4 0 5 1 2 3 4 5
9 8 0 1 2 3 1 2
9 9 9 0 9 9 9 9
1 1 1 1 0 1 1 1
8 7 6 5 4 0 3 2
9 1 9 1 9 1 0 9
6 5 4 3 2 1 9 0
예제 출력 3
1

공부노트

  • 해당 개념에서 백트래킹을 배우면서 우리가 원하는 값이 안나오면 돌아간다라는 개념을 공부하게 되었음
  • 14889의 문제 같은경우 상대팀과 내 팀의 인원이 같은경우를 생각해야 되어서 n/2를 하며 진행함
  • 그렇게 나누어진 부분을 벡터에 init하고 다음 score값에다가 배열을 하나하나 넣는 방식으로 진행함
  • 그리고 team 부분에 continue 즉 방문을 한 것 우리팀인 것일 경우 continue를 진행하여 다음 for로 가게 만들어 진행함

14889코드

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int line;

int layer[20][20];

int lower_value = 500;

bool team[20];

void teamset(int mem, int fight_mem)
{
    vector<int> start;
    vector<int> link;

    int start_score = 0;

    int link_score = 0;
    
    if (fight_mem == line/2)
    {
        for(int i = 0 ; i < line ; i++)
        {
            if(team[i]==true)
            {
                start.push_back(i);
            }
            else
            {
                link.push_back(i);
            }
        }

        for(int i = 0 ; i<line/2 ; i++)
        {
            for(int j = 0 ; j<line/2 ; j++)
            {
               start_score+=layer[start[i]][start[j]];
               link_score+=layer[link[i]][link[j]];
            }
        }

        lower_value = min(lower_value,(abs(start_score-link_score)));
        return;
    }

    for(int i = mem ; i < line ; i++)
    {
        if(team[i])
        {
        continue;
        }
        else
        {
            team[i]=true;
            teamset(i,fight_mem+1);
            team[i]=false;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    cin>>line;

    for(int i = 0 ; i<line;i++)
    {
        for(int j = 0 ; j<line;j++)
        {
            cin>>layer[i][j];
        }
    }
    teamset(0,0);
    cout<<lower_value;
}

예제 입력 1
4
0 1 2 3
4 0 5 6
7 1 0 2
3 4 5 0
예제 출력 1
0
예제 입력 2
6
0 1 2 3 4 5
1 0 2 3 4 5
1 2 0 3 4 5
1 2 3 0 4 5
1 2 3 4 0 5
1 2 3 4 5 0
예제 출력 2
2
예제 입력 3
8
0 5 4 5 4 5 4 5
4 0 5 1 2 3 4 5
9 8 0 1 2 3 1 2
9 9 9 0 9 9 9 9
1 1 1 1 0 1 1 1
8 7 6 5 4 0 3 2
9 1 9 1 9 1 0 9
6 5 4 3 2 1 9 0
예제 출력 3
1

공부노트

  • 이번 문제는 전문제와 다르게 팀수가 제한이 없었음
  • 그래서 일단 모든팀을 true로 만든다음(우리팀으로 만든다음) 내부에 한팀이라도 size가 0이면 return을 하여 다음 함수로 가게 만듬
  • 그리고 link와 start팀은 수가 각각 다르다고 판단하여 size도 나누어 score 값을 추출하고자 함

15661코드

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int input_value[20][20];

int input_count;

int output_answer=50;

bool are_you[20];

void teamset(int count)
{
    vector<int> link;
    vector<int> start;

    int link_score = 0;

    int start_score = 0;


    if(count==input_count)
    {
        for(int i=0;i<input_count;i++)
        {
            if(are_you[i]==true)
                link.push_back(i);
            else
                start.push_back(i);
        }

        if(link.size()==0 || start.size()==0)
        {
            return;
        }

        for(int i = 0;i<link.size();i++)
            for(int j = 0;j<link.size();j++)
            {
                link_score+=input_value[link[i]][link[j]];
            }

        for(int i = 0;i<start.size();i++)
            for(int j = 0;j<start.size();j++)
            {
                start_score+=input_value[start[i]][start[j]];
            }
            output_answer = min(output_answer,abs(link_score-start_score));
            return;
    }

    are_you[count]=true;

    teamset(count+1);

    are_you[count]=false;

    teamset(count+1);

}

int main()
{
    cin>>input_count;

    for(int i=0;i<input_count;i++)
    {
        for(int j=0;j<input_count;j++)
        {
            cin>>input_value[i][j];
        }
    }

    teamset(0);

    cout<<output_answer;

}
profile
#자기공부 #틀린것도많음 #자기개발 여러분 인생이 힘들다 하더라도 그것을 깨는 순간 큰 희열감으로 옵니다~

0개의 댓글