카테고리 없음

[백준 15650번 c#] N과 M(2)

Heeyeon Choi 2022. 10. 6. 10:02
728x90
728x90

- 앞서 풀었던 N과 M의 응용버전입니다.

- 해당 문제에에서는 1,2 와 2,1 을 중복으로 보고 출력하지 않습니다.

- 이 뜻은 visit 을 자기 자신보다 앞 인덱스인 것을 모두 false로 돌리면 된다는 뜻입니다.

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project2
{
    class Class1
    {
        static StringBuilder sb = new StringBuilder();

        static int[] arr;
        static bool[] visit;
        static void Main(string[] args)
        {
            StreamWriter writer = new StreamWriter(Console.OpenStandardOutput());
            StreamReader reader = new StreamReader(Console.OpenStandardInput());

            //[1]입력
            string[] str = reader.ReadLine().Split();
            int num = int.Parse(str[0]);
            int count = int.Parse(str[1]);

            arr = new int[count];
            visit = new bool[num];
            dfs(num, count, 0);
            writer.WriteLine(sb.ToString());
            writer.Close();
            reader.Close();

        }

        static void dfs(int a, int b, int dep)
        {
            if (dep == b)
            {
                foreach (int val in arr)
                {
                    sb.Append(val + " ");
                }
                sb.AppendLine();
                return;
            }

            for (int i = 0; i < a; i++)
            {
                if (!visit[i])
                {
                    visit[i] = true;
                    for (int j=0; j<i; j++)
                    {
                        visit[j] = true;
                    }
                    
                    arr[dep] = i + 1;
                    dfs(a, b, dep + 1);
                    visit[i] = false;
                    for (int j = 0; j < i; j++)
                    {
                        visit[j] = false;
                    }
                }
            }
        }

    }

}
728x90