Posted on 2008-04-03 19:56
superman 阅读(198)
评论(0) 编辑 收藏 引用 所属分类:
ZOJ
1 /* Accepted 1047 C++ 00:00.00 852K */
2 #include <iostream>
3
4 using namespace std;
5
6 int n, m, cnt;
7 char grid[21][21];
8
9 inline bool canGo(int i, int j)
10 {
11 if(i >= 1 && i <= n && j >= 1 && j <= m && grid[i][j] == 'X')
12 return true;
13 return false;
14 }
15
16 void search(int i, int j)
17 {
18 grid[i][j] = '-';
19
20 if(i - 1 < 1 || grid[i - 1][j] == '.')
21 cnt++;
22 if(i + 1 > n || grid[i + 1][j] == '.')
23 cnt++;
24 if(j - 1 < 1 || grid[i][j - 1] == '.')
25 cnt++;
26 if(j + 1 > m || grid[i][j + 1] == '.')
27 cnt++;
28
29 if(canGo(i - 1, j))
30 search(i - 1, j);
31 if(canGo(i + 1, j))
32 search(i + 1, j);
33 if(canGo(i, j - 1))
34 search(i, j - 1);
35 if(canGo(i, j + 1))
36 search(i, j + 1);
37 if(canGo(i - 1, j - 1))
38 search(i - 1, j - 1);
39 if(canGo(i - 1, j + 1))
40 search(i - 1, j + 1);
41 if(canGo(i + 1, j - 1))
42 search(i + 1, j - 1);
43 if(canGo(i + 1, j + 1))
44 search(i + 1, j + 1);
45 }
46
47 int main()
48 {
49 while(cin >> n >> m)
50 {
51 if(n == 0 && m == 0)
52 break;
53 int sx, sy;
54 cin >> sx >> sy;
55 for(int i = 1; i <= n; i++)
56 for(int j = 1; j <= m; j++)
57 switch(cin.get())
58 {
59 case '.' : grid[i][j] = '.'; break;
60 case 'X' : grid[i][j] = 'X'; break;
61 default : j--;
62 }
63 cnt = 0;
64 search(sx, sy);
65 cout << cnt << endl;
66 }
67
68 return 0;
69 }
70