forked from mahendrarathore1742/leetcode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poj1979-red-and-black.cpp
47 lines (42 loc) · 1.05 KB
/
poj1979-red-and-black.cpp
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
/*
* Copyright (C) 2018 all rights reserved.
*
* Author: Houmin Wei <[email protected]>
*
* Source: http://poj.org/problem?id=1979
*/
#include <iostream>
#include <cstring>
using namespace std;
char board[20][20];
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int dfs(int w, int n, int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= w) return 0;
if (board[x][y] == '#') return 0;
int cnt = 1;
for (int i = 0; i < 4; i++) {
int x1 = x+dir[i][0], y1 = y+dir[i][1];
board[x][y] = '#';
cnt += dfs(w, n, x1, y1);
}
return cnt;
}
int main() {
int w, h;
while (cin >> w >> h) {
if (w == 0 && h == 0) return 0;
int startX, startY;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> board[i][j];
if (board[i][j] == '@') {
startX = i;
startY = j;
}
}
}
int cnt = dfs(w, h, startX, startY);
cout << cnt << endl;
}
return 0;
}