Problem Links:
uva00861,Problem:
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 1015.
8 6
4 4
0 0
5599888
260
Solution:
Rotate the chess board 45 degree. Then no bishops should stay in the same row or same column.Using dynamic programming skills;
dp[i][j] representss the solution of j bishops filled in the first i rows.
dp[i][j] = dp[i-1][j] + dp[i-1][j-1] * (color[i] - (j-1))
Source Code:
//Tue Apr 5 00:46:15 CDT 2011#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
void init(int n, vector<int> &black, vector<int> &white)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if ((i + j) % 2)
white[(i + j) / 2]++;
else
black[(i + j) / 2]++;
}
}
}
void bishops(int n, int k, vector<vector<int> > &dp, vector<int> color)
{
for (int i = 0; i <= n; i++)
dp[i][0] = 1;
for (int i = 1; i <= k; i++)
dp[0][i] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= color[i] && j<=k; j++)
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] * (color[i] - j + 1);
}
int main(int argc, char* argv[])
{
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
int n, k;
while (cin >> n >> k)
{
if (n == 0 && k == 0) return 0;
vector<vector<int> > dp1(n + 1, vector<int>(k + 1, 0));
vector<vector<int> > dp2(n, vector<int>(k + 1, 0));
vector<int> white(n, 0);
vector<int> black(n + 1, 0);
init(n, black, white);
sort(black.begin() + 1, black.end());
sort(white.begin() + 1, white.end());
bishops(n, k, dp1, black);
bishops(n - 1, k, dp2, white);
int count = 0;
for (int i = 0; i <= k; i++)
count += dp1[n][i] * dp2[n-1][k - i];
cout << count << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
No comments :
Post a Comment