Skip to content

Commit 16cb452

Browse files
committedDec 1, 2017
added week9
1 parent 40b02b0 commit 16cb452

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
 

‎Week9/2017-11-30/A.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//https://open.kattis.com/contests/cgq7ia/problems/coast
2+
#include<bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
int n, m;
7+
vector<vector<int> > grid;
8+
int ans = 0;
9+
10+
void dfs(int r, int c){
11+
vector<int> dr = {-1, 0, 1, 0};
12+
vector<int> dc = {0, 1, 0, -1};
13+
for(int i = 0; i < 4; i++){
14+
//explore all 4 immediate neighbors
15+
int nr = r + dr[i];
16+
int nc = c + dc[i];
17+
if(nr >= 0 && nr <= n+1 && nc >= 0 && nc <= m+1){
18+
if(grid[nr][nc] == 0){
19+
grid[nr][nc] = 2;
20+
//if it's water, then we DFS there
21+
//but we'll set it to 2 so that we know it's been visited
22+
dfs(nr, nc);
23+
} else if(grid[nr][nc] == 1){
24+
//if it's 1, then we've found an edge to land. so we increment ans by 1.
25+
ans++;
26+
}
27+
}
28+
}
29+
}
30+
31+
int main(){
32+
cin >> n >> m;
33+
grid.push_back(vector<int>(m+2, 0));
34+
for(int i = 1; i < n+1; i++){
35+
grid.push_back(vector<int>(m+2, 0));
36+
for(int j = 1; j < m+1; j++){
37+
char a;
38+
cin >> a;
39+
if(a == '1'){
40+
grid[i][j] = 1;
41+
} else {
42+
grid[i][j] = 0;
43+
}
44+
}
45+
}
46+
grid.push_back(vector<int>(m+2));
47+
//debug code to print out the grid:
48+
// for(int i = 0; i < n+2; i++){
49+
// for(int j = 0; j < m+2; j++){
50+
// cout << grid[i][j];
51+
// } cout << endl;
52+
// }
53+
grid[0][0] = 2;
54+
dfs(0, 0);
55+
cout << ans << endl;
56+
return 0;
57+
}

0 commit comments

Comments
 (0)
Please sign in to comment.