2806. N-Queen

SW 업무 관련/SW Expert Academy

2806. N-Queen

WillBe_ 2018. 7. 22. 23:10

D2, D3, D4라고 써있는 문제의 난이도 기준을 모르겠다.

내가 잘 안풀어보던 유형이라 어려워 보이는건가...ㅜ,ㅠ


이건 퀸을 놓기 전에 그 자리기 겹치는 퀸이 있는지 보고 없으면 놓는 방식인데...테케가 딸랑 2개라 맞는지 모르겠다!!!




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <string.h>
 
int map[10][10];
int count, len;
 
int check(int y, int x)
{
    int a = 0;
 
    for (int i = 0; i < len; i++)
    {
        if (map[y][i] || map[i][x])//가로, 세로
            return 0;
 
        if ((map[y - i][x + a] == 1&& (y - i >= 0 && x + a < len))
            return 0;
 
        if ((map[y + i][x + a] == 1&& (y + i < len && x + a < len))
            return 0;
 
        if ((map[y - i][x - a] == 1&& (y - i >= 0 && x - a >= 0))
            return 0;
 
        if ((map[y + i][x - a] == 1&& (y + i < len &&- a >= 0))
            return 0;
 
        a++;
    }
    return 1;
}
 
int Q(int y)
{
    for (int i = 0; i < len; i++)
    {
        int re = check(y, i);
        if (re == 1 && y == len - 1)
        {
            count++;
            return;
        }
        else if (re == 1)
        {            
            map[y][i] = 1;
            Q(y + 1);
            map[y][i] = 0;
        }
    }
    return 0;
}
 
int main()
{
    int Test;
    scanf("%d"&Test);
 
    for (int i = 1; i <= Test; i++)
    {
        count = 0;
        memset(map, 0sizeof(map));
        scanf("%d"&len);
 
        Q(0);
 
        printf("%d %d\n", i, count);
    }
    return 0;
}
cs