1. Distinct Numbers (1)
  2. Apartments  (1)
  3. Ferris Wheel  (1)
  4. Concert Tickets  (1)
  5. Restaurant Customers  (1)
  6. Movie Festival  (1)
  7. Sum of Two Values (1)
  8. Maximum Subarray Sum (1)
  9. Stick Lengths (1)
  10. Missing Coin Sum
  11. Collecting Numbers
  12. Collecting Numbers II
  13. Playlist (1)
  14. Towers (1)
  15. Traffic Lights (1
  16. Josephus Problem I
  17. Josephus Problem II
  18. Nested Ranges Check
  19. Nested Ranges Count
  20. Room Allocation (1)
  21. Factory Machines (1)
  22. Tasks and Deadlines (1)
  23. Reading Books (1)
  24. Sum of Three Values (1)
  25. Sum of Four Values (1)
  26. Nearest Smaller Values (1)
  27. Subarray Sums I (1)
  28. Subarray Sums II (1)
  29. Subarray Divisibility (1
  30. Subarray Distinct Values
  31. Array Division (1)
  32. Sliding Median (1)
  33. Sliding Cost (1)
  34. Movie Festival II (1)
  35. Maximum Subarray Sum II (1

 

CSES Problem Set ( link

'CSES' 카테고리의 다른 글

CSES 2. Apartments (1084)  (0) 2019.09.24
CSES 2. Distinct Numbers (1621)  (0) 2019.09.24
CSES 1. Chessboard and Queens (1624)  (0) 2019.09.23
CSES 1. Apple Division (1623)  (0) 2019.09.23
CSES 1. Creating Strings I (1622)  (0) 2019.09.23

출처: https://cses.fi/problemset/task/1624


문제 설명

Your task is to place eight queens on a chessboard so that no two queens are attacking each other. As an additional challenge, each square is either free or reserved, and you can only place queens on the free squares. However, the reserved squares do not prevent queens from attacking each other.

How many possible ways are there to place the queens?

입력

The input has eight lines, and each of them has eight characters. Each square is either free (.) or reserved (*).

출력

Print one integer: the number of ways you can place the queens.

입력 예

........
........
..*.....
........
........
.....**.
...*....
........

출력 예

65


문제 풀이 

NxN보드에서 N개의 Queen을 서로 공격하지 않도록 배열하는 N-Queen 문제이다. 첫번째 열부터 퀸을 하나 배치하고, 공격 받을 수 있는 위치들을 표시하고 8번째 열에 도착하면 경우의 수를 기록한다.

프로그램 내용

더보기
/// n-th queen location check
int backtrack(vector<string> board, int nQueen )
{
	if(nQueen == 8)
        return 1;

	int ans=0;
	for (int i = 0; i < 8; ++i)
	{
		if(board[nQueen][i]=='.'){
			vector<string> n_board = board;
			for (int j = nQueen; j < 8; ++j) /// only check below rows
			{
				n_board[j][i]='*';
				if(i-j+nQueen>=0)
					n_board[j][i-(j-nQueen)]='*'; /// diagonal
				if(i+j-nQueen<8)
					n_board[j][i+(j-nQueen)]='*'; /// diagonal
			}
			ans += backtrack(n_board,nQueen+1);   /// updated board , next row
		}
	}
	return ans;
}

int main()
{
    vector <string> board;

    for (int i = 0; i < 8; i++)
    {
        string tmp;
        cin >> tmp;
        board.push_back(tmp);
    }

    int ways = backtrack(board, 0);

 

Introductory problems ( link )

'CSES' 카테고리의 다른 글

CSES 2. Distinct Numbers (1621)  (0) 2019.09.24
CSES 2. Sorting and Searching  (0) 2019.09.24
CSES 1. Apple Division (1623)  (0) 2019.09.23
CSES 1. Creating Strings I (1622)  (0) 2019.09.23
CSES 1. Palindrome Reorder (1755)  (0) 2019.09.23

출처: https://cses.fi/problemset/task/1623


문제 설명

There are n apples with known weights. Your task is to divide the apples into two groups so that the difference between the weights of the groups is minimal.

입력

The first input line has an integer n: the number of apples.

The next line has n integers p_1,p_2,,p_n: the weight of each apple.

출력

Print one integer: the minimum difference between the weights of the groups.

입력 예

5
3 2 7 4 1

출력 예

1

제약조건

  • 1 <= n <= 20
  • 1 <= p_i <= 10^9

문제 풀이

입력받은 사과들을 양쪽에 각각 넣어보고 무게 차의 최소 값을 찾는다.

프로그램 내용

더보기
long ans = 1e18;

void search_diff(int a, long b, vector<int> weight)
{
    if (a == weight.size()) {
        ans = min(ans, abs(b));
        return;
    }
    search_diff(a+1,b+weight[a], weight);	/// add apple to side A
    search_diff(a+1,b-weight[a], weight);	/// add apple to side B
}

int main()
{
    int N;		/// number of apple

    vector<int> weight(N,0);
    for(int idx=0; idx<N ; ++idx)
        cin >> weight[idx];

    search_diff(0,0, weight);

    cout << ans << endl;

 

Introductory problems ( link )

'CSES' 카테고리의 다른 글

CSES 2. Sorting and Searching  (0) 2019.09.24
CSES 1. Chessboard and Queens (1624)  (0) 2019.09.23
CSES 1. Creating Strings I (1622)  (0) 2019.09.23
CSES 1. Palindrome Reorder (1755)  (0) 2019.09.23
CSES 1. Coin Piles (1754)  (0) 2019.09.23

+ Recent posts