首页 > ACM题库 > HDU-杭电 > HDU 1820 Little Bishops[解题报告]
2013
12-23

HDU 1820 Little Bishops[解题报告]

Little Bishops

问题描述 :

A bishop is a piece used in the game of chess which is played on a board of square grids. A bishop can only move diagonally from its current position and two bishops attack each other if one is on the path of the other. In the following figure, the dark squares represent the reachable locations for bishop B1 form its current position. The figure also shows that the bishops B1 and B2 are in attacking positions whereas B1 and B3 are not. B2 and B3 are also in non-attacking positions.

Now, given two numbers n and k, your job is to determine the number of ways one can put k bishops on an n × n chessboard so that no two of them are in attacking positions.

输入:

The input file may contain multiple test cases. Each test case occupies a single line in the input file and contains two integers n (1 ≤ n ≤ 8) and k (0 ≤ k ≤ n2).

A test case containing two zeros for n and k terminates the input and you won’t need to process this particular input.

输出:

For each test case in the input print a line containing the total number of ways one can put the given number of bishops on a chessboard of the given size so that no two of them are in attacking positions. You may safely assume that this number will be less than 10^15.

样例输入:

8 6
4 4
0 0

样例输出:

5599888
260

 

Imagine that a chessboard with black and white squares. If a bishop is on a white square, can it attack bishops on black squares? No.
So you can divide the chessboard into two independent ones. Then rotate each board for PI/4 and now the diagonal line becomes vertical. Now the problem of bishops becomes that of rooks.

 

We denote the number of cells of the i’th row contains with r[i]. In fact, the order of rows does not matter, so we can sort the rows with r[i]. We use c[i][j] to represent the number of cases to put j rooks in the first i rows. The we have the following recurrence,

      c[i][j] = c[i-1][j] + c[i-1][j-1] * (r[i] – (j-1))

with the boundary conditions,

      c[i][0] = 1, 0 <= i <= n, and

      c[0][j] = 0, 1 <= j <= k

 

Now, we have got the results of the two boards. A simple accumulation would give us the final results.

 

Code:

/***************************************************************************
 *   Copyright (C) 2008 by Liu Kaipeng                                     *
 *   LiuKaipeng at gmail dot com                                           *
 ***************************************************************************/
 
/* @JUDGE_ID 00000 861 C++ "Little Bishops" */
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
     
long long bishops(int n, int k)
{
  long long r1[9];
  long long r2[9];
  for (int i = 1; i <= n; ++i)
    r1[i] = i % 2 != 0 ? i : r1[i-1];
  for (int i = 1; i <= n-1; ++i)
    r2[i] = i % 2 != 0 ? i+1 : r2[i-1];
  long long c1[9][65] = {{0}};
  long long c2[9][65] = {{0}};
  for (int i = 0; i <= n; ++i)
    c1[i][0] = 1;
  for (int j = 1; j <= k; ++j)
    c1[0][j] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= k && j <= i; ++j)
      c1[i][j] = c1[i-1][j] + c1[i-1][j-1]*(r1[i] - j + 1);
  
  for (int i = 0; i <= n-1; ++i)
    c2[i][0] = 1;
  for (int j = 1; j <= k; ++j)
    c2[0][j] = 0;
  for (int i = 1; i <= n-1; ++i)
    for (int j = 1; j <= k && j <= i; ++j)
      c2[i][j] = c2[i-1][j] + c2[i-1][j-1]*(r2[i] - j + 1);
 
  long long r = 0;
  for (int i = 0; i <= k; ++i)
    r += c1[n][i] * c2[n-1][k-i];
  return r;
}
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
  freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
  freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
#endif
  for (int n, k; cin >> n >> k && !(n == 0 && k == 0); )
    cout << bishops(n, k) << endl;
  return 0;
}

解题报告转自:http://blog.csdn.net/liukaipeng/article/details/3901412


  1. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的