这是组合数学还是计算机问题?

本文探讨了一个微软校园招聘题目,即如何在限定条件下计算不同颜色的国际象棋车在棋盘上的有效布局方案数量。文中提供了一段C++实现代码,用于解决特定情况下(每种颜色仅有一个棋子)的有效布局方案计数问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

据说微软校园招聘题:

Problem Statement for RooksParty 
 
 
Problem Statement 
  The black and white chess rooks were bored, so they decided to invite their colorful friends to a party. However, as the evening progressed, many pairs of rooks of different colors started arguing and threatening each other. 

To prevent a massacre, you now need to place all the rooks in such a way that no two rooks of different colors attack each other. 

You are given the dimensions of the chessboard: ints rows and columns. You are also given a int[] counts, where counts[i] is the number of rooks of the i-th color you have. 

Compute and return the value (X mod 1,000,000,009), where X is the number of valid arrangements of all the given rooks on the given chessboard. No square of the chessboard may contain more than one rook. Rooks of the same color are undistinguishable. 
 
  
Definition 
  Class: RooksParty 
Method: countArrangements 
Parameters: int, int, int[] 
Returns: int 
Method signature: int countArrangements(int rows, int columns, int[] counts) 
(be sure your method is public) 
 
   
 
  
Notes 
- Two rooks attack each other if they are either in the same row or in the same column, and all squares between them are empty. 
  
Constraints 
- rows will be between 1 and 30, inclusive. 
- columns will be between 1 and 30, inclusive. 
- counts will contain between 1 and 10 elements, inclusive. 
- Each element of counts will be positive. 
- The sum of all elements of counts will not exceed rows*columns. 
  
Examples 
0)  
  2
 
3
 
{1,1}
 
 
Returns: 12
 
Here are all 12 valid placements:

1.. 1.. .1. .1. ..1 ..1
.2. ..2 2.. ..2 2.. .2.

.2. ..2 2.. ..2 2.. .2.
1.. 1.. .1. .1. ..1 ..1

 
 
 
1)  
  5
 
2
 
{3}
 
 
Returns: 120
 
As all three rooks have the same color, we can put them on any three squares. 
 
 
2)  
  5
 
2
 
{1,1,1}
 
 
Returns: 0
 
It is impossible to place these rooks correctly. 
 
 
3)  
  8
 
8
 
{1,1,1,1,1,1,1,1}
 
 
Returns: 625702391
 
Here the answer is (8! * 8!) modulo 1,000,000,009. 
 
 
4)  
  4
 
2
 
{3,1}
 
 
Returns: 8
 
 
 
 

--------------------------------------------------------------------------------

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved. 

 

一个棋盘上,最多有10种颜色的棋子,每种颜色棋子数不定,行列中不能有不同颜色棋子,求出可实行的方案数。

采用遍历算法是不可能的,计算时间不可接受,我这里只讨论简单的情况,也就是每种颜色只有一个棋子,其他情况可能需要组合数学知识。

以下是我的代码,请多多指教。

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
int countArrangements(int rows, int columns, int counts[])
{
 int nResult = 0;
 int nColor = 0;
 int nfilter = 0;
 int nNext = 0;
 int ntemp = 0;
 for (int i = 0; i < 10; ++i)
 {
  if (counts[i] > 0)
  {
   nColor++;
  }
 }
 if (nColor > rows || nColor > columns)
 {
  return nResult;
 }
 nResult = rows*columns;
 for(int i = 1;i < nColor; ++i)
 {
  nNext = rows * columns - i*(rows + columns) + i*i;
  nfilter = 1000000009/nNext;
  if (nResult <= nfilter)
  {
   nResult = nResult * nNext;
  }else
  {
   ntemp = nResult;
   for (int j = 1; j < nNext; j++)
   {
    nResult += ntemp;
    if (nResult > 1000000009)
    {
     nResult -= 1000000009;
    }
   }
  }
 }
 return nResult;
}
int main()
{
 clock_t start,finish;
 start = clock();
 int a[] ={1,1,1,1,1,1,1,1,1,1};
 int nresult = 0;
 nresult = countArrangements(30,30,a);
 cout << nresult << endl;
 finish = clock();
 cout << (double)(finish - start)/CLOCKS_PER_SEC << endl;
 return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值