http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1047
简单题,唯一要注意的地方就是计算周长的方法.
 1 #include <iostream>
 2 #include <cmath>
 3 #include <stdio.h>
 4 #include <vector>
 5 #include <string.h>
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int row, col, mouse_x, mouse_y;
11     char m[20][20];
12     vector< pair<intint> > v;
13     while(cin >> row >> col >> mouse_x >> mouse_y && row != 0 && col != 0 && mouse_x != 0 && mouse_y != 0)
14     {
15         for(int i = 0; i < row; i++)
16             for(int j = 0; j < col; j++)
17                 cin >> m[i][j];
18 
19         vector< pair<intint> > q;
20         q.push_back(make_pair(mouse_x-1, mouse_y-1));
21         while(q.size() > 0)
22         {
23             int x = q[0].first, y = q[0].second;
24             v.push_back(q[0]);
25             m[x][y] = 'o';
26             // 8 directions
27             if(x - 1 >= 0)
28             {
29                 if(m[x-1][y] == 'X')
30                 {
31                     q.push_back(make_pair(x-1, y)); // up
32                     m[x-1][y] = 'o';
33                 }
34                 if(y-1 >= 0 && m[x-1][y-1== 'X')
35                 {
36                     q.push_back(make_pair(x-1, y-1)); // up-left
37                     m[x-1][y-1= 'o';
38                 }
39                 if(y+1 < col && m[x-1][y+1== 'X')
40                 {
41                     q.push_back(make_pair(x-1, y+1)); // up-right
42                     m[x-1][y+1= 'o';
43                 }
44             }
45             if(y-1 >= 0 && m[x][y-1== 'X')
46             {
47                 q.push_back(make_pair(x, y-1)); // left
48                 m[x][y-1= 'o';
49             }
50             if(y+1 < col && m[x][y+1== 'X')
51             {
52                 q.push_back(make_pair(x, y+1)); // right
53                 m[x][y+1= 'o';
54             }
55             if(x + 1 < row)
56             {
57                 if(m[x+1][y] == 'X')
58                 {
59                     q.push_back(make_pair(x+1, y)); // down
60                     m[x+1][y] = 'o';
61                 }
62                 if(y-1 >= 0 && m[x+1][y-1== 'X')
63                 {
64                     q.push_back(make_pair(x+1, y-1)); // down-left
65                     m[x+1][y-1= 'o';
66                 }
67                 if(y+1 < col && m[x+1][y+1== 'X')
68                 {
69                     q.push_back(make_pair(x+1, y+1)); // down-right
70                     m[x+1][y+1= 'o';
71                 }
72             }
73             // remove first one
74             q.erase(q.begin());
75         }
76 
77         // calculate perimeters
78         int perimeters = 0;
79         for(int i = 0; i < v.size(); i++)
80         {
81             if(v[i].first == 0 || m[v[i].first-1][v[i].second] == '.'// up
82                 perimeters++;
83             if(v[i].second == 0 || m[v[i].first][v[i].second-1== '.'// left
84                 perimeters++;
85             if(v[i].first == row-1 || m[v[i].first+1][v[i].second] == '.'// down
86                 perimeters++;
87             if(v[i].second == col-1 || m[v[i].first][v[i].second+1== '.'// right
88                 perimeters++;
89         }
90         cout << perimeters << endl;
91         v.clear();
92     }
93 }