1349. Maximum Students Taking Exam(DP,状态压缩)

题目

  1. Maximum Students Taking Exam

Add to List

Share
Given a m * n matrix seats that represent seats distributions in a classroom. If a seat is broken, it is denoted by ‘#‘ character otherwise it is denoted by a ‘.‘ character.

Students can see the answers of those sitting next to the left, right, upper left and upper right, but he cannot see the answers of the student sitting directly in front or behind him. Return the maximum number of students that can take the exam together without any cheating being possible..

Students must be placed in seats in good condition.

Example 1:

Input: seats = [["#",".","#","#",".","#"],
[".","#","#","#","#","."],
["#",".","#","#",".","#"]]
Output: 4
Explanation: Teacher can place 4 students in available seats so they don‘t cheat on the exam.
Example 2:

Input: seats = [[".","#"],
["#","#"],
["#","."],
["#","#"],
[".","#"]]
Output: 3
Explanation: Place all students in available seats.

Example 3:

Input: seats = [["#",".",".",".","#"],
[".","#",".","#","."],
[".",".","#",".","."],
[".","#",".","#","."],
["#",".",".",".","#"]]
Output: 10
Explanation: Place students in available seats in column 1, 3 and 5.

题意:上图中,#表示坏掉的椅子,.表示好的椅子,在椅子上安排考试,不能让学生作弊

题解:用二进制,表示每一行的每种安排的可能性。然后再进行动态规划,dp[i][j] = max{dp[i-1][k]+num[j]} 其中j 表示第i行的某个安排的二进制数,k同理是i-1行的某个安排,k和j满足不作弊的条件。num[j]表示j安排下,坐了多少学生。由于数组长度最多为8,所以状态数组的第二个维度最多为2^8-1,

class Solution {
public:

    int n,m;
    int res;
    vector<vector<int>> map1;
    vector<vector<int>> map2;
    int dp[10][1025];
    int maxStudents(vector<vector<char>>& seats) {

        n = seats.size();
        m = seats[0].size();

        for(int i=0;i<n;i++)
        {
            vector<int> x;
            vector<int> y;
            fun(seats,i,0,x,y,0,0);

            map1.push_back(x);
            map2.push_back(y);
        }

        for(int i=0;i<n;i++)
        {
            for(int j=0;j<map1[i].size();j++)
            {
                if(i==0)
                    dp[i][map1[i][j]]=map2[i][j];
                else
                {
                    for(int k=0;k<map1[i-1].size();k++)
                    {
                        if(((map1[i][j]<<1) & map1[i-1][k])||((map1[i][j]>>1) & map1[i-1][k]))
                        {
                             continue;
                        }

                        dp[i][map1[i][j]]=max(dp[i][map1[i][j]],dp[i-1][map1[i-1][k]]+map2[i][j]);
                    }
                }
            }
        }

        for(int i=0;i<map1[n-1].size();i++)
        {
            res=max(res,dp[n-1][map1[n-1][i]]);
        }

        return res;

    }

    void fun(vector<vector<char>>& seats,int pos,int tag,vector<int>& s1,vector<int>& s2,int seat1,int seat2)
    {
        if(tag==seats[pos].size())
        {
            s1.push_back(seat1);
            s2.push_back(seat2);
            return;
        }

        seat1 <<= 1;
        if(seats[pos][tag]=='.')
        {
            if(tag==0||seats[pos][tag-1]!='.')
            {
                fun(seats,pos,tag+1,s1,s2,seat1+1,seat2+1);
            }
        }

        char c = seats[pos][tag];
        seats[pos][tag]='#';
        fun(seats,pos,tag+1,s1,s2,seat1,seat2);
        seats[pos][tag]=c;
    }
};

原文地址:https://www.cnblogs.com/dacc123/p/12288336.html

时间: 2024-10-20 06:22:23

1349. Maximum Students Taking Exam(DP,状态压缩)的相关文章

dp状态压缩

dp状态压缩 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的就是那种状态很多,不容易用一般的方法表示的动态规划问题,这个就更加的难于把握了.难点在于以下几个方面:状态怎么压缩?压缩后怎么表示?怎么转移?是否具有最优子结构?是否满足后效性?涉及到一些位运算的操作,虽然比较抽象,但本质还是动态规划.找准动态规划几个方面的问题,深刻理解动态规划的原理,开动脑筋思考问题.这才是掌握动态规划的关键. 动态规划最关键的要处理的问题就是位运算的操作,容易出错,状态的设计也直

HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由于得到每张卡片的状态不知道,所以用状态压缩,dp[i] 表示这个状态时,要全部收齐卡片的期望. 由于有可能是什么也没有,所以我们要特殊判断一下.然后就和剩下的就简单了. 另一个方法就是状态压缩+容斥,同样每个状态表示收集的状态,由于每张卡都是独立,所以,每个卡片的期望就是1.0/p,然后要做的就是要去重,既然

uva 11367 dijkstra+dp状态压缩

题意:给出n个地点 和 每个地点的油价 ,有 m 条边 , 并给出每条边长度 .1单位汽油可以走1千米  , 油箱的容量为 c , 在初始点 s 时 , 油箱中的油为 0 , 求s 到 t 的最小花费 . 解法: 定义 状态 d[i][j] 表示到达 地点 i 且油箱中有 j 单位油时的最小 花费. 对于状态的转移时 , 有两种方法: 1.把每个点的所有状态都求出 2.不把每个点的状态都求出 , 而是一单位一单位的加油. 对于第一种方法 , 会超时 , 因为每个点的状态太多 , 但是能用的状态就

hdu 4352 数位dp + 状态压缩

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2265    Accepted Submission(s): 927 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

HDU4352XHXJ&#39;s LIS(dp状态压缩)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 809    Accepted Submission(s): 308 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

【HDU 4352】 XHXJ&#39;s LIS (数位DP+状态压缩+LIS)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

Uva 10817 Headmaster&#39;s Headache (DP+ 状态压缩)

Problem D: Headmaster's Headache Time limit: 2 seconds The headmaster of Spring Field School is considering employing some new teachers for certain subjects. There are a number of teachers applying for the posts. Each teacher is able to teach one or

[数位dp+状态压缩] hdu 4352 XHXJ&#39;s LIS

题意: 给x.y.k,在[x,y] 范围内最长上升子序列长度是k的数有几个 思路: 模仿 LIS nlogn的想法,这里就只有10个数,进行状压 然后直接搜就好了不用二分 然后按位dp下去就ok了! 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"queue" #include"al

(hiho1048)POJ2411Mondriaan&#39;s Dream(DP+状态压缩 or 轮廓DP)

问题: Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangle