-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path75-Maximum Students Taking Exam.cpp
More file actions
71 lines (68 loc) · 1.66 KB
/
75-Maximum Students Taking Exam.cpp
File metadata and controls
71 lines (68 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<bits/stdc++.h>
using namespace std;
int m,n;
vector<vector<int>> g;
vector<vector<int>> dir={{0,-1},{-1,-1},{1,-1},{0,1},{-1,1},{1,1}};
int S,T;
int flow;
void bfs(vector<int> &pre){
queue<int> q;
q.push(S);
vector<int> visited(m*n+2,0);
visited[S]=1;
while(!q.empty()){
int cur=q.front();
q.pop();
for(int i=0;i<m*n+2;++i){
if(visited[i]==0 && g[cur][i]==1){
visited[i]=1;
q.push(i);
pre[i]=cur;
}
}
}
}
void EK(){
while(true){
vector<int> pre(m*n+2,-1);
bfs(pre);
if(pre[T]==-1) break;
int v=T;
while(true){
int u=pre[v];
g[u][v]--;
g[v][u]++;
v=u;
if(v==S) break;
}
flow++;
}
}
int maxStudents(vector<vector<char>> seats) {
m=seats.size();
n=seats[0].size();
S=m*n;
T=m*n+1;
g=vector<vector<int>>(m*n+2,vector<int>(m*n+2,0)); // residual capacity
int seatcnt=0;
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
if(seats[i][j]=='#') continue;
seatcnt++;
if(j%2==0){
g[S][i*n+j]=1;
for(int d=0;d<6;++d){
int ni=i+dir[d][0];
int nj=j+dir[d][1];
if(ni<0 || ni>=m || nj<0 || nj>=n || seats[ni][nj]=='#') continue;
g[i*n+j][ni*n+nj]=1;
}
}else{
g[i*n+j][T]=1;
}
}
}
flow=0;
EK();
return seatcnt-flow;
}