前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1349. Maximum Students Taking Exam(DP,状态压缩)

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

作者头像
ShenduCC
发布2020-02-17 11:47:03
4990
发布2020-02-17 11:47:03
举报
文章被收录于专栏:算法修养算法修养算法修养

题目

  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;
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档