Skip to content

Commit 82c2eec

Browse files
authored
Create 2061.Number-of-Spaces-Cleaning-Robot-Cleaned.cpp
1 parent 19504e6 commit 82c2eec

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
int visited[300][300][4];
3+
int cleaned[300][300];
4+
public:
5+
int numberOfCleanRooms(vector<vector<int>>& room)
6+
{
7+
int m = room.size(), n = room[0].size();
8+
vector<pair<int,int>>dir = {{0,1},{1,0},{0,-1},{-1,0}};
9+
int x = 0, y = 0, d = 0;
10+
int ret = 0;
11+
while (visited[x][y][d]==0)
12+
{
13+
if (cleaned[x][y]==0)
14+
{
15+
ret++;
16+
cleaned[x][y] = 1;
17+
}
18+
19+
visited[x][y][d]=1;
20+
21+
int i = x+dir[d].first;
22+
int j = y+dir[d].second;
23+
if (i>=m||i<0||j>=n||j<0||room[i][j]==1)
24+
{
25+
d = (d+1)%4;
26+
}
27+
else
28+
{
29+
x = i;
30+
y = j;
31+
}
32+
}
33+
return ret;
34+
}
35+
};

0 commit comments

Comments
 (0)