Checker Challenge(Quest + DFS)

4335 단어 check

Checker Challenge


Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more than one in any diagonal. (Diagonals run from southeast to northwest and southwest to northeast and include all diagonals, not just the major two.)
 
          Column
    1   2   3   4   5   6
  -------------------------
1 |   | O |   |   |   |   |
  -------------------------
2 |   |   |   | O |   |   |
  -------------------------
3 |   |   |   |   |   | O |
  -------------------------
4 | O |   |   |   |   |   |
  -------------------------
5 |   |   | O |   |   |   |
  -------------------------
6 |   |   |   |   | O |   |
  -------------------------

The solution shown above is described by the sequence 2 4 6 1 3 5, which gives the column positions of the checkers for each row from 1 to 6:
ROW






COLUMN






This is one solution to the checker challenge. Write a program that finds all unique solution sequences to the Checker Challenge (with ever growing values of N). Print the solutions using the column notation described above. Print the the first three solutions in numerical order, as if the checker positions form the digits of a large number, and then a line with the total number of solutions.
Special note: the larger values of N require your program to be especially efficient. Do not precalculate the value and print it (or even find a formula for it); that's cheating. Work on your program until it can solve the problem properly. If you insist on cheating, your login to the USACO training pages will be removed and you will be disqualified from all USACO competitions. YOU HAVE BEEN WARNED.

TIME LIMIT: 1 CPU second


PROGRAM NAME: checker


INPUT FORMAT


A single line that contains a single integer N (6 <= N <= 13) that is the dimension of the N x N checkerboard.

SAMPLE INPUT (file checker.in)

6

OUTPUT FORMAT


The first three lines show the first three solutions found, presented as N numbers with a single space between them. The fourth line shows the total number of solutions found.

SAMPLE OUTPUT (file checker.out)

2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4

 
제목:
황후 문제.N을 제시하면 N행 N열의 행렬이 있음을 나타냅니다. N개의 바둑돌을 놓아야 합니다. 임의의 두 바둑돌은 같은 행의 같은 대각선(정방향이든 반방향이든)에 있지 않습니다.
 
생각:
DFS를 사용하거나 비트로 연산할 수 있습니다.DFS는 세 방향(세로, 주 대각선, 반대 대각선)을 표시하고 각 행을 검색합니다.주 대각선은 가로 좌표차의 절대값이 같고 반대 대각선은 화등하다는 것을 주의해라.
 
   AC:
/*   
TASK:checker   
LANG:C++   
ID:sum-g1   
*/ 
#include<stdio.h>
#include<string.h>
int n;
int c[15],s[100],bs[100];
int fin[15],ans = 0;

void dfs(int x,int y)
{
    c[y] = 1;
    s[x - y + 15] = 1;   // 
    bs[x + y] = 1;       // 
    fin[x] = y;
    if(x == n) ans++;
    if(ans <= 3 && x == n)
    {
        for(int i = 1;i <= n;i++)
        {
            printf("%d",fin[i]);
            i == n ? printf("
") : printf(" "); } } for(int i = 1;i <= n;i++) { if(i == y) continue; if(!c[i] && !s[x + 1 - i + 15] && !bs[x + 1 + i]) { dfs(x + 1,i); c[i] = 0; s[x + 1 - i + 15] = 0; bs[i + x + 1] = 0; } } } int main() { freopen("checker.in","r",stdin); freopen("checker.out","w",stdout); scanf("%d",&n); for(int i = 1;i <= n;i++) { memset(c,0,sizeof(c)); memset(s,0,sizeof(s)); memset(bs,0,sizeof(bs)); dfs(1,i); } printf("%d
",ans); return 0; }

 
   

좋은 웹페이지 즐겨찾기