What is important in this question is the understanding of the meaning: A bomb causes other bombs on the same line (in the same horizontal or vertical line), and this bomb will trigger a chain effect. So use N times of extensive search. [Cpp] # include <iostream> # include <math. h> # include <stdio. h> # include <string. h >#include <queue >#include <algorithm> # include <stack> using namespace std; struct Point {int x; int y ;}; Point p [102]; int visited [102]; int main () {# ifndef ONLINE_JUDGE freopen ("in.txt", "r", stdin); # endif int n; while (scanf ("% d", & n )! = EOF & n! = 0) {int ans = 0; for (int I = 0; I <n; I ++) {scanf ("% d", & p [I]. x, & p [I]. y);} memset (visited, 0, sizeof (visited); for (int I = 0; I <n; I ++) {if (visited [I] = 0) {visited [I] = 1; queue <Point> qu; Point nex, temp; qu. push (p [I]); while (! Qu. empty () {temp = qu. front (); qu. pop (); for (int j = 0; j <n; j ++) {if (visited [j] = 0 & (temp. x = p [j]. x | temp. y = p [j]. y) {www.2cto.com visited [j] = 1; qu. push (p [j]) ;}}}ans ++ ;}} printf ("% d \ n", ans) ;}return 0 ;}