poj-3254 Corn Fields dp问题

本文介绍了一个经典的组合计数问题——如何在限定条件下选择种植玉米的位置。Farmer John 的牧场由 M x N 的网格组成,部分地块无法种植。文章探讨了避免选择相邻地块的方法,并提供了完整的算法实现。
Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18317 Accepted: 9645

Description

Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

Input

Line 1: Two space-separated integers:  M and  N 
Lines 2.. M+1: Line  i+1 describes row  i of the pasture with  N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)

Output

Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

Sample Input

2 3
1 1 1
0 1 0

Sample Output

9

Hint

Number the squares as follows:
1 2 3
  4  

There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.

Source


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define mod 100000000
int M,N,top = 0;
///top表示每行最多的状态数


int state[600];
///state存放每行所有的可行状态(即没有相邻的状态)


int dp[20][600];
///dp[i][j]:对于前i行数据,每行有前j种可能状态时的解
int cur[20];
///cur[i]表示的是第i行整行的情况,就是能种与不能种


bool ok(int x)   ///判断状态x是否可行
{
    if(x&x<<1) return false;///若存在相邻两个格子都为1,则该状态不可行
    return true;
}
void init()             ///遍历所有可能的状态
{
    top = 0;
    int total = 1 << N; ///遍历状态的上界,也就是情况的最多种数
    // printf("total==%d\n",total);
    for(int i = 0; i < total; ++i)
    {
        if(ok(i))state[++top] = i;
    }
//    printf("top==%d\n",top);
//    for(int i= 1; i<=top; i++)
//    {
//        printf("state[%d]==%d\n",i,state[i]);
//    }
}
bool fit(int x,int k)  ///判断状态x 与第k行的实际状态的逆是否有‘重合’
{


    if(x&cur[k])return false; ///若有重合,(即x不符合要求)
    printf("x==%d cur[%d]==%d\n",x,k,cur[k]);
    return true;  ///若没有,则可行
}


int main()
{
    while(scanf("%d%d",&M,&N)!= EOF)
    {
        init();
        memset(dp,0,sizeof(dp));
        for(int i = 1; i <= M; ++i)
        {
            cur[i] = 0;
            int num;
            for(int j = 1; j <= N; ++j)   ///输入时就要按位来存储,cur[i]表示的是第i行整行的情况,每次改变该数字的二进制表示的一位
            {
                scanf("%d",&num);  ///表示第i行第j列的情况(0或1)
                if(num == 0) ///若该格为0
                {
                    cur[i] +=(1<<(N-j));///则将该位置为1(注意要以相反方式存储,即1表示不可放牧
                }
            }
        }
//        for(int i=1;i<=M;i++)
//        {
//            printf("cur[%d]==%d\n",i,cur[i]);
//        }
        for(int i = 1; i <= top; i++)
        {
            if(fit(state[i],1))   ///判断所有可能状态与第一行的实际状态的逆是否有重合
            {
                dp[1][i] = 1;  ///若第1行的状态与第i种可行状态吻合,则dp[1][i]记为1
            }
        }
//        printf("----------------\n");


        /*
        状态转移过程中,dp[i][k] =Sigma dp[i-1][j] (j为符合条件的所有状态)
         */
        for(int i = 2; i <= M; ++i)   ///i索引第2行到第M行
        {
            for(int k = 1; k <= top; ++k)  ///该循环针对所有可能的状态,找出一组与第i行相符的state[k]
            {
                if(!fit(state[k],i))continue; ///判断是否符合第i行实际情况
                printf("state[%d]==%d cur[%d]==%d\n",k,state[k],i,cur[i]);
                for(int j = 1; j <= top ; ++j) ///找到state[k]后,再找一组与第i-1行符合,且与第i行(state[])不冲突的状态state[j]
                {
                    if(!fit(state[j],i-1))continue;  ///判断是否符合第i-1行实际情况
                    if(state[k]&state[j])continue;  ///判断是否与第i行冲突
                    dp[i][k] = (dp[i][k] +dp[i-1][j])%mod;  ///若以上皆可通过,则将'j'累加到‘k'上
                }
            }
        }
        int ans = 0;
        for(int i = 1; i <= top; ++i)  ///累加最后一行所有可能状态的值,即得最终结果
        {
            ans = (ans + dp[M][i])%mod;
        }
        printf("%d\n",ans);
    }
}

内容概要:本文详细介绍了一种基于Simulink的表贴式永磁同步电机(SPMSM)有限控制集模型预测电流控制(FCS-MPCC)仿真系统。通过构建PMSM数学模型、坐标变换、MPC控制器、SVPWM调制等模块,实现了对电机定子电流的高精度跟踪控制,具备快速动态响应和低稳态误差的特点。文中提供了完整的仿真建模步骤、关键参数设置、核心MATLAB函数代码及仿真结果分析,涵盖转速、电流、转矩和三相电流波形,验证了MPC控制策略在动态性能、稳态精度和抗负载扰动方面的优越性,并提出了参数自整定、加权代价函数、模型预测转矩控制和弱磁扩速等优化方向。; 适合人群:自动化、电气工程及其相关专业本科生、研究生,以及从事电机控制算法研究与仿真的工程技术人员;具备一定的电机原理、自动控制理论和Simulink仿真基础者更佳; 使用场景及目标:①用于永磁同步电机模型预测控制的教学演示、课程设计或毕业设计项目;②作为电机先进控制算法(如MPC、MPTC)的仿真验证平台;③支撑科研中对控制性能优化(如动态响应、抗干扰能力)的研究需求; 阅读建议:建议读者结合Simulink环境动手搭建模型,深入理解各模块间的信号流向与控制逻辑,重点掌握预测模型构建、代价函数设计与开关状态选择机制,并可通过修改电机参数或控制策略进行拓展实验,以增强实践与创新能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值