题目:http://poj.org/problem?id=1111
给出你个 n * m的图形,然后给出你鼠标点击的位置(鼠标只可以点击 X),鼠标可以从点击的点往八个方向走,问鼠标可以点击的区域的周长
View Code
1 typedef long long ll; 2 const int N = 22; 3 bool vis[N][N]; 4 char map[N][N]; 5 int ans; 6 int n,m; 7 int move[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,1},{1,-1}}; 8 struct node 9 { 10 int x,y; 11 }s; 12 int cal(node t) 13 { 14 int sum = 0; 15 if(map[t.x - 1][t.y] == '.') sum ++; 16 if(map[t.x + 1][t.y] == '.') sum ++; 17 if(map[t.x][t.y + 1] == '.') sum ++; 18 if(map[t.x][t.y - 1] == '.') sum ++; 19 return sum; 20 } 21 bool juge(int x,int y) 22 { 23 if(x <= n && x > 0 && y <= m && y > 0 && map[x][y] == 'X' && !vis[x][y]) 24 return true; 25 else return false; 26 } 27 void bfs() 28 { 29 queue<node>qu; 30 node tem,kem; 31 int i,j; 32 qu.push(s); 33 while(!qu.empty()) 34 { 35 tem = qu.front(); 36 qu.pop(); 37 for(i = 0; i < 8; i++) 38 { 39 kem.x = tem.x + move[i][0]; 40 kem.y = tem.y + move[i][1]; 41 if(juge(kem.x,kem.y)) 42 { 43 qu.push(kem); 44 ans += cal(kem); 45 //cout<<"kem = "<<kem.x<<" "<<kem.y<<endl; 46 //cout<<"ans = "<<ans<<endl; 47 vis[kem.x][kem.y] = true; 48 } 49 } 50 } 51 } 52 int main() 53 { 54 int i,j,k; 55 //freopen("data.txt","r",stdin); 56 while(cin>>n>>m>>s.x>>s.y) 57 { 58 if(!n && !m && !s.x && !s.y) break; 59 for(i = 0; i <= n + 1; i++) 60 { 61 for(j = 0; j <= m + 1; j++) 62 map[i][j] = '.'; 63 } 64 for(i = 1; i <= n; i++) 65 { 66 for(j = 1; j <= m; j++) 67 cin>>map[i][j]; 68 } 69 ans = cal(s); 70 _clr(vis,0); 71 vis[s.x][s.y] = true; 72 bfs(); 73 printf("%d\n",ans); 74 } 75 return 0; 76 }