1005 - Rooks(规律)

本文探讨了如何利用组合数学原理解决N×N国际象棋上放置k个骑士(Rooks)的问题,避免两骑士相互攻击。通过深搜与组合数学方法,解决了输入为N和k时,计算放置k个骑士在棋盘上的合法位置数量,最终输出每组测试用例的解,并提供了一种超时深搜的备选算法。

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

1005 - Rooks
Time Limit: 1 second(s)Memory Limit: 32 MB

A rook is a piece used in the game of chess which is played on a board of square grids. A rook can only move vertically or horizontally from its current position and two rooks attack each other if one is on the path of the other. In the following figure, the dark squares represent the reachable locations for rook R1 from its current position. The figure also shows that the rook R1 and R2 are in attacking positions where R1 and R3 are not. R2 and R3 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 rooks on an n x n chessboard so that no two of them are in attacking positions.

Input

Input starts with an integer T (≤ 350), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 30) and k (0 ≤ k ≤ n2).

Output

For each case, print the case number and total number of ways one can put the given number of rooks 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 1017.

Sample Input

Output for Sample Input

8

1 1

2 1

3 1

4 1

4 2

4 3

4 4

4 5

Case 1: 1

Case 2: 4

Case 3: 9

Case 4: 16

Case 5: 72

Case 6: 96

Case 7: 24

Case 8: 0

 题解:这个题感触好深,自己也就是个傻叉,从开始就想着深搜,搜啊搜,搜了好久,搜出来了,超时了。。。傻叉到极致,30的数据量每列还是30,自己真敢想啊;这要搜下去到何年何月。。。其实就是个规律,从n行里面选m行放棋子,这个无序,从n列选m列放旗子,这个有序。。。就得出来规律了,C(n,m)*A(n,m);

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<map>
 7 #include<vector>
 8 #include<stack>
 9 #include<queue>
10 using namespace std;
11 const int INF=0x3f3f3f3f;
12 int main(){
13     int n,m,T,flot=0;
14     scanf("%d",&T);
15     while(T--){
16         long long ans=1;
17         scanf("%d%d",&n,&m);
18         for(int i=n;i>(n-m);i--)ans*=i;
19         for(int i=1;i<=m;i++)ans/=i;
20         for(int i=n;i>(n-m);i--)ans*=i;
21         printf("Case %d: %lld\n",++flot,ans);
22     }
23     return 0;
24 }

见见我的逗比超时dfs:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<map>
 7 #include<vector>
 8 #include<stack>
 9 #include<queue>
10 using namespace std;
11 const int INF=0x3f3f3f3f;
12 const int MAXN=1010;
13 int vis[MAXN];
14 int n,k;
15 long long ans;
16 void dfs(int x,int num){
17     if(num==k){
18         ans++;
19         return;
20     }
21     if(x>=n)return;
22         for(int i=0;i<n;i++){
23             if(vis[i])continue;
24             vis[i]=1;
25             dfs(x+1,num+1);
26             vis[i]=0;
27         }
28         if(x+1<n&& num<=k)dfs(x+1,num);
29 }
30 int main(){
31     int T,flot=0;
32     scanf("%d",&T);
33     while(T--){
34         memset(vis,0,sizeof(vis));
35         scanf("%d%d",&n,&k);
36         if(k>n){
37             puts("0");continue;
38         }
39         if(n==k){
40             long long t=1;
41             for(int i=1;i<=n;i++)t*=i;
42             printf("%I64d\n",t);
43             continue;
44         }
45         ans=0;
46         dfs(0,0);
47         printf("Case %d: %lld\n",++flot,ans);
48     }
49     return 0;
50 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值