USACO 1.5.4 Checker Challenge ———— DFS

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 1 2 3 4 5 6
COLUMN 2 4 6 1 3 5

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

HINTS (use them carefully!)

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

八皇后直接按白书的思路写了

/*
     ID: xinming2
     PROG: checker
     LANG: C++
*/
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,n) for(i=0;i<(n);++i)
#define FOR(i,l,h) for(i=(l);i<=(h);++i)
#define FORD(i,h,l) for(i=(h);i>=(l);--i)

typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
int a[300];
int vis[3][300];
int tot;
int n;
int dfs(int cur)
{
    if(cur == n)
    {
        tot++;
        if(tot < 4)
        {
            for(int i = 0 ; i < n ; i++)
            {
                if(!i)printf("%d" , a[i]);
                else printf(" %d" , a[i]);
            }
            puts("");
        }
    }
    else
    {
        for(int i = 0 ; i < n ; i++)
        {
            if(!vis[0][i] && !vis[1][cur + i] && !vis[2][cur - i + n])
            {
                a[cur] = i + 1;
                vis[0][i] = vis[1][cur + i] = vis[2][cur - i + n] = 1;
                dfs(cur + 1);
                vis[0][i] = vis[1][cur + i] = vis[2][cur - i + n] = 0;
            }
        }
    }

}
int main()
{
    freopen("checker.in" , "r" , stdin);
    freopen("checker.out" , "w" , stdout);
    while(~scanf("%d", &n))
    {
        tot = 0;
        memset(vis , 0 , sizeof(vis));
        memset(a , 0 , sizeof(a));
        dfs(0);
        cout << tot << endl;
    }
    return 0;
}


你可能感兴趣的:(ACM,DFS)