UVA - 10237 Bishops

來源:互聯網
上載者:User

標籤:style   http   color   os   io   strong   for   ar   art   

A bishop is a piece used in thegame of chess which is played on a board of square grids. A bishop can only movediagonally from its current position and two bishops attack each other if oneis on the path of the other. In the following figure, the dark squaresrepresent the reachable locations for bishop B1 formits current position.  The figure also shows that the bishopsB1and B2 are in attacking positions whereasB1and B3 are not.B2 and B3are also in non-attacking positions.

 

 

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

 

Input

 The input file maycontain multiple test cases. Each test case occupies a single line in the inputfile and contains two integersn (1 ≤ n ≤ 30) and k (0 ≤ k ≤ n2).

 

A test case containing two zerosfor n and k terminates the input and youwon’t need to process this particular input.

 

Output

For each test case in the inputprint a line containing the total number of ways one can put the given numberof bishops on a chessboard of the given size so that no two of them are inattacking positions. You may safely assume that this number will be less than 1015.

 

Sample Input
8 6
4 4
20 40
30 5
0 0

 

Sample Output
5599888
260
0

3127859642656

題意:兩個象互不攻擊,若且唯若它們不出於同一斜線,一個n*n的棋盤上放k個互不攻擊的象有多少種方法。

思路:把棋盤染色成國際象棋的棋盤,那麼只有同一顏色上的才會互相攻擊,而且不同顏色的互不影響,那麼我們分別算不同顏色的

方法。首先將黑白顏色的格子都取出來,那麼我們可以考慮第i行放或者不放,如果不放的話結果不變,如果放的話,只有

(len[i]-j+1)的位置可以選,因為前面有(j-1)個會攻擊到,那麼狀態轉移是:

dp[i][j] = dp[i-1][j]+(dp[i-1][j-1]*(len[i]-j+1)){len[i]代表第i行的格子個數}因為len[i]不是遞增的,其實我們將一半行後面的也拿到前面,結果是不影響的

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>typedef long long ll;using namespace std;const int maxn = 35;ll dp1[maxn][1005], dp2[maxn][1005];ll len1[maxn], len2[maxn];int n, k;void init() {len1[1] = len1[2] = 1;len2[1] = len2[2] = 2;for (int i = 3; i < maxn-2; i += 2) {len1[i+1] = len1[i] = len1[i-1] + 2;len2[i+1] = len2[i] = len2[i-1] + 2;}}int main() {init();while (scanf("%d%d", &n, &k) != EOF && n+k) {memset(dp1, 0, sizeof(dp1));memset(dp2, 0, sizeof(dp2));for (int i = 0; i <= n; i++) dp1[i][0] = dp2[i][0] = 1;for (int i = 1; i <= n; i++)for (int j = 1; j <= k; j++) {dp1[i][j] = (len1[i] - j + 1) * dp1[i-1][j-1] + dp1[i-1][j];if (i != n) dp2[i][j] = (len2[i] - j + 1) * dp2[i-1][j-1] + dp2[i-1][j];}ll ans = 0;for (int i = 0; i <= k; i++) ans += dp1[n][i] * dp2[n-1][k-i];printf("%lld\n", ans);} return 0;}


UVA - 10237 Bishops

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.