• NYOJ 492 King (状态压缩)



    做题感悟:做完这题发现状态压缩有很多须要优化的地方。

    解题思路:状态压缩

                   開始自己用的一般的思路,就和炮兵阵地,郑厂长等题类似的方法做的,開始超时,然后把数组开到了最小的极限就险过。然后看了别人的代码感觉须要优化(注意)的地方太多了。

                   首先我们这题能够预处理出来上下两行相应的合法状态,这样我们就确定下来上下两行相应的状态了。这是第一步的优化 。由于当前行仅仅与上一行有关(这里仅仅考虑当前行对上一行的影响就能够)。我们能够把 dp 第一维开成 2 的就能够了,这是第二步的优化,尽管这步作用有点小。最后 ,我们还能够抛弃一些不合法的输入 ,假如是 n * n 的棋盘,一行最多能够防止(n+1)/ 2 个,然后 n 行最多能够放置 ( n + 1 ) / 2 * ( n + 1 ) / 2 个。 为什么是 ( n + 1 ) / 2 行放置呢 ? 由于每一个格子攻击相邻的八个格子。不要用n / 2 ,由于假设 奇数行的话就少算了一行。

                   这题也能够用DFS递推和铺方格差点儿相同。这样也相当于预处理出上下两行相应的状态。

    代码:

    #include<iostream>
    #include<sstream>
    #include<map>
    #include<cmath>
    #include<fstream>
    #include<queue>
    #include<vector>
    #include<sstream>
    #include<cstring>
    #include<cstdio>
    #include<stack>
    #include<bitset>
    #include<ctime>
    #include<string>
    #include<cctype>
    #include<iomanip>
    #include<algorithm>
    using namespace std  ;
    #define INT long long  int
    #define L(x)  (x * 2)
    #define R(x)  (x * 2 + 1)
    const int INF = 0x3f3f3f3f ;
    const double esp = 0.0000000001 ;
    const double PI = acos(-1.0) ;
    const int mod = 1e9 + 7 ;
    const int MY = 1400 + 5 ;
    const int MX = 150 ;
    int n ,m ,num ,top ;
    INT dp[2][MX][101] ;
    int key[MX] ,h[MX] ;
    struct node
    {
        int x ,y ,c ;
    }P[MY] ;
    void init() // 预处理合法状态
    {
        for(int S = 0 ;S < (1<<n) ; ++S)
          if(!(S&(S>>1)) && !(S&(S<<1)))
          {
              int nx = 0 ;
              for(int i = 0 ;i < n ; ++i)
                if(S&(1<<i))
                   nx++ ;
              key[num] = S ;
              h[num++] = nx ;
          }
    }
    int main()
    {
        while(~scanf("%d%d" ,&n ,&m))
        {
            if(((n+1)/2)*((n+1)/2) < m)  // 抛弃不合法的状态
            {
                puts("0") ;
                continue ;
            }
            num = 0 ;  top = 0 ;
            init() ; // 预处理合法状态
            for(int i = 0 ;i < num ; ++i)  // 预处理上下两行相应的合法状态
              for(int j = 0 ;j < num ; ++j)
                if(!(key[i]&(key[j]>>1)) && !(key[i]&(key[j]<<1)) && !(key[i]&key[j]))
                {
                    P[top].x = i ;
                    P[top].y = j ;
                    P[top++].c = h[j] ;
                }
            memset(dp[0] ,0 ,sizeof(dp[0])) ;
            dp[0][0][0] = 1 ;
            for(int i = 1 ;i <= n ; ++i)
            {
                memset(dp[i&1] ,0 ,sizeof(dp[i&1])) ;
                for(int j = 0 ;j < top ; ++j)        // 上一行的合法状态
                for(int t = 0 ;t <= m ; ++t)        // 安放的个数
                  if(t + P[j].c <= m)              // 个数限制
                  dp[i&1][P[j].y][t+P[j].c] += dp[(i+1)&1][P[j].x][t] ;
            }
           INT ans = 0 ;
           for(int i = 0 ;i < num ; ++i)
               ans += dp[n&1][i][m] ;
           printf("%lld
    " ,ans) ;
        }
        return 0 ;
    }
    


  • 相关阅读:
    centos7.x网卡bond配置
    twemproxy源码解析系列三Twemproxy配置文件解析及相关组件初始化过程
    twemproxy源码解析系列二关键数据结构分析
    Nginx 变量漫谈(一)(转)
    twemproxy源码解析系列一特性及启动流程分析
    理解 Linux 的处理器负载均值
    man命令使用
    awk 查找文件中数字 字符串 email
    非阻塞socket调用connect, epoll和select检查连接情况示例
    Mysql日期类型大小比较拉取给定时间段的记录
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7270043.html
Copyright © 2020-2023  润新知