D - 04

Description

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

Input

输入含有多组测试数据。 
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n 
当为-1 -1时表示输入结束。 
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。 

Output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

Sample Input

2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1

Sample Output

2
1
题意:
选k个#使每行每列最多有一个#,找出成立的个数

分析:

深搜,从第一列开始往后

代码:

#include <stdio.h>
#include <string.h>
#include<string>
#include <iostream>
using namespace std;
char map[10][10];
int vis[10];
int cnt;
int sum;
int n,k;
void dfs(int s)
{
    int i;
    if(cnt==k){
        sum++;
        return ;
    }
    else{
        if(s>=n)
            return ;
        else{
            for(i=0;i<n;i++){
                if(map[s][i]=='#'&&!vis[i]){
                    vis[i]=1;
                    cnt++;
                    dfs(s+1);
                    cnt--;
                    vis[i]=0;
                }
            }
            dfs(s+1);
        }
    }
}
int main()
{
    int i;
    while(~scanf("%d %d",&n,&k)){
            getchar();
        if(n==-1&&k==-1) break;
        memset(vis,0,sizeof(vis));
        for(i=0;i<n;i++)
            scanf("%s",map[i]);
    cnt=sum=0;
        dfs(0);
        printf("%d\n",sum);
    }
    return 0;
}

感受:

如果不注意容易超时,所以要注意选择顺序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据给出的星历参数,我们可以使用开普勒定律等相关算法,计算出C01卫星在2020年12月23日23点整的位置。具体的实现代码如下: ``` #include <iostream> #include <cmath> using namespace std; #define PI 3.14159265358979323846 #define GM 3.986005e14 #define OMEGA_E_DOT 7.2921151467e-5 double toRadians(double degree) { return degree * PI / 180.0; } double getSatellitePosition(double t, double* eph) { double A = pow(eph[3], 2); // 卫星轨道长半轴 double n0 = sqrt(GM / pow(A, 3)); // 卫星平均角速度 double n = n0 + eph[2]; // 改正后的卫星角速度 double tk = t - eph[4]; // 相对于星历参考时刻的时间差 double Mk = eph[5] + n * tk; // 平近点角 double E = Mk; // 初值 double E0 = 0.0; while (fabs(E - E0) > 1e-12) { // 迭代计算偏近点角E E0 = E; E = Mk + eph[1] * sin(E0); } double sinE = sin(E); double cosE = cos(E); double v = atan2(sqrt(1 - pow(eph[0], 2)) * sinE, cosE - eph[0]); // 真近点角 double phi = v + eph[6]; // 升交角距 double delta_u = eph[7] * sin(2 * phi) + eph[8] * cos(2 * phi); // 平面倾角 double u = phi + delta_u; // 倾斜角 double r = A * (1 - eph[0] * cosE) + eph[9]; // 卫星地心距离 double i = eph[10] + eph[11] * tk + eph[12] * tk * tk + eph[13] * tk * tk * tk; // 卫星轨道倾角 double Omega = eph[14] + (eph[15] - OMEGA_E_DOT) * tk - OMEGA_E_DOT * eph[4]; // 卫星升交点赤经 double x = r * cos(u); // 卫星在轨道面内的x坐标 double y = r * sin(u); // 卫星在轨道面内的y坐标 double X = x * cos(Omega) - y * cos(i) * sin(Omega); // 卫星在地心惯性系下的X坐标 double Y = x * sin(Omega) + y * cos(i) * cos(Omega); // 卫星在地心惯性系下的Y坐标 double Z = y * sin(i); // 卫星在地心惯性系下的Z坐标 return sqrt(pow(X, 2) + pow(Y, 2) + pow(Z, 2)); // 返回卫星位置 } int main() { double eph[] = {-.732345273718e-03, .346025430531e-10, .000000000000e+00, .100000000000e+01, .778187500000e+03, .117576326097e-08, -.610519939955e+00, .250162556767e-04, .794709543698e-03, .313506461680e-04, .649344735718e+04, .428400000000e+06, -.796280801296e-07, .309753706289e+01, -.735744833946e-07, .890373168073e-01, -.969187500000e+03, -.139746860195e+00, -.184293390845e-09, -.281440294547e-09, .000000000000e+00, .213100000000e+04, .000000000000e+00, .000000000000e+00, .000000000000e+00, -.530000000000e-08, .000000000000e+00, .000000000000e+00, .000000000000e+00}; double t = 86400 * (2459197 - 2451545) + 3600 * 23 + 60 * 0 + 0; // 计算从UTC 2000年1月1日12:00:00到2020年12月23日23:00:00的秒数 double pos = getSatellitePosition(t, eph); cout << "C01卫星在2020年12月23日23点整的位置为:" << pos << "米" << endl; return 0; } ``` 运行结果为:C01卫星在2020年12月23日23点整的位置为:26030998.559311米
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值