Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- void dfs(int r,int c, vector<vector<int>>& visited,vector<vector<int>>& stones) {
- if(visited[r][c]) return;
- visited[r][c]=1;
- for(int cn=0;cn<stones[r].size();cn++){
- if(visited[r][cn]==0) {
- dfs(r,cn,visited,stones);
- }
- }
- for(int rn=0;rn<stones.size();rn++){
- if(visited[rn][c]==0) {
- dfs(rn,c,visited,stones);
- }
- }
- }
- int removeStones(vector<vector<int>>& stones) {
- int count=0;
- int mRow=0;
- int mCol=0;
- for(auto stone:stones) { mRow = max(mRow,stone[0]);
- mCol = max(mCol,stone[1]);
- }
- vector<vector<int>> visited(mRow+1,vector<int>(mCol+1,0));
- vector<vector<int>> stonex(mRow+1,vector<int>(mCol+,0));
- for(auto stone:stones){ stonex[stone[0]][stone[1]]=1;}
- for(int r=0;r<stonex.size();r++) {
- for(int c=0;c<stonex[r].size();c++) {
- if(visited[r][c]==0) {
- dfs(r,c,visited,stonex);
- count++;
- }
- }
- }
- return stones.size()-count;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement