# 1349. Maximum Students Taking Exam

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.

Constraints:

• `seats` contains only characters `'.' and``'#'.`
• `m == seats.length`
• `n == seats[i].length`
• `1 <= m <= 8`
• `1 <= n <= 8`

``````class Solution {
public:
int maxStudents(vector<vector<char>>& seats) {
int m = seats.size(), n = seats[0].size(), res = 0, size = (1 << 8);
vector<vector<int>> dp(9, vector<int>(size, -1));
vector<int> avail(9);
for (int i = 1; i <= m; ++i) {
for (int j = 0; j < n; ++j) {
if (seats[i - 1][j] == '.') {
avail[i] |= (1 << j);
}
}
}
}
for (int i = 1; i <= m; ++i) {
if (dp[i - 1][premask] == -1) continue;
}
}
}
}
return res;
}
int res = 0;
}
return res;
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1349

https://leetcode.com/problems/maximum-students-taking-exam/

LeetCode All in One 题目讲解汇总(持续更新中…)

（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，快快加入吧～）

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation