LC 1349. Maximum Students Taking Exam
Posted feiief
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LC 1349. Maximum Students Taking Exam相关的知识,希望对你有一定的参考价值。
Solution 0:
class Solution { public: int m,n; int maxStudents(vector<vector<char>>& seats) { m=seats.size(); n=seats[0].size(); vector<vector<int>> dp(m*n+2,vector<int>(1<<(2*n),-1)); return dfs(seats,0,0,dp); } int dfs(vector<vector<char>>& seats, int pos, int state, vector<vector<int>> &dp){ if(dp[pos][state]!=-1) return dp[pos][state]; if(pos==m*n) return 0; int x=pos/n; int y=pos%n; if(y==0){ state=state>>n; } int take=0,untake=0; untake=dfs(seats,pos+1,state,dp); int flag=0; if(seats[x][y]==‘#‘) flag=1; if(y-1>=0 && (state&(1<<(y-1+n)))>0) flag=1; if(y+1<n && (state&(1<<(y+1+n)))>0) flag=1; if(y-1>=0 && x>0 && (state&(1<<(y-1)))>0) flag=1; if(y+1<n && x>0 && (state&(1<<(y+1)))>0) flag=1; if(flag==0) take=1+dfs(seats,pos+1,state|(1<<(y+n)),dp); return dp[pos][state]=max(take,untake); } };
Solution 1 :
class Solution { public: int m,n; int maxStudents(vector<vector<char>>& seats) { m=seats.size(); n=seats[0].size(); vector<vector<int>> dp(m+1,vector<int>(1<<(2*n),-1)); return dfs(seats,0,0,dp); } int dfs(vector<vector<char>>& seats, int row, int state, vector<vector<int>> &dp){ if(row==m) return 0; if(dp[row][state]!=-1) return dp[row][state]; int ns=state>>n; // when we want to skip to next row, need to change the state int tmp1= dfs(seats,row+1,ns,dp); int tmp2=0; for(int i=0;i<n;++i){ // check whether we can fill seats[row][i] if(seats[row][i]==‘#‘) continue; if((state&(1<<(n+i)))>0) continue; if(i-1>=0 && (state&(1<<(n+i-1)))>0) continue; // left if(i+1<n && (state&(1<<(n+i+1)))>0) continue; // right if(row>0 && i-1>=0 && (state&(1<<(i-1)))>0) continue; //topleft if(row>0 && i+1<n && (state&(1<<(i+1)))>0) continue; //topright tmp2=max(tmp2,1+dfs(seats,row,state|(1<<i+n),dp)); } return dp[row][state]=max(tmp1,tmp2); } };
Solution 2:
class Solution { public: int m,n; int maxStudents(vector<vector<char>>& seats) { m=seats.size(); n=seats[0].size(); vector<vector<int>> dp(m+1,vector<int>(1<<n,-1)); return dfs(seats,0,0,dp); } int dfs(vector<vector<char>>& seats, int row, int prestate, vector<vector<int>> &dp){ if(row==m) return 0; if(dp[row][prestate]!=-1) return dp[row][prestate]; vector<int> curstates; dfs1(seats[row],0,prestate,curstates,0); int res=0; for(int sta:curstates){ int cnt=getcnt(sta); res=max(res,cnt+dfs(seats,row+1,sta,dp)); } return dp[row][prestate]=res; } int getcnt(int sta){ int res=0; for(int i=0;i<n;++i){ if((sta&(1<<i))>0) res++; } return res; } void dfs1(vector<char> seat, int col, int prestate, vector<int>& curstates, int curstate){ if(col==n) { curstates.push_back(curstate); return; } dfs1(seat,col+1,prestate,curstates,curstate); int flag=0; if(seat[col]==‘#‘) return; if(col-1>=0 && (((1<<(col-1))&curstate)>0 || ((1<<(col-1))&prestate)>0)) return; if(col+1<n && ((1<<(col+1))&prestate)>0) return; dfs1(seat,col+1,prestate,curstates,curstate|(1<<col)); } };
以上是关于LC 1349. Maximum Students Taking Exam的主要内容,如果未能解决你的问题,请参考以下文章
[LC] 152. Maximum Product Subarray
LC.104. Maximum Depth of Binary Tree
[LC] 1219. Path with Maximum Gold