引爆地雷

来源:互联网 发布:我国网络舆论的特点是? 编辑:程序博客网 时间:2024/06/10 18:39
/*
作者:shaoshaoh
日期:2006-10-01
声明:欢迎转载,请勿删除此信息

Problem B: Mines

Many mines have been detected to exist in a small area of the Sahara desert, 
which is a severe threat to the habitants nearby. Those mines are quite 
special and called the "Super Chaining" ones because if any one of them is 
triggered and exploded, every mine within 5 meters from it would also be 
triggered. Those triggered mines explode and continue to trigger other 
mines in the same manner, a kind of chain reaction. So terrible it is!
With the help of a newly developed mine detector, Lieutenant John has 
recently been able to detect the exact positions of all the mines. And now, 
he is making a plan on how to eliminate all the mines by triggering all of 
them, an economic way as it is in the desert. As a young sergeant, your 
mission now is to help Lieutenant to evaluate the effect of triggering one 
single mine which Lieutenant chooses to be the first to trigger. That is, 
for a given mine, you must decide which mines will also be triggered as 
side effect.
 
Input Specification:
In the first line, an integer N(2<=N<=100) represents the number of mines. 
In the ith(i=1..N) of the following N lines, two integers Xi and 
Yi(-1000<=Xi,Yi<=1000) in meters represent the cartesian position of the 
ith mine. The final line includes one integer representing which mine to 
trigger first.

Output Specification:
In a single line, output the list of order numbers of mines that will also 
be triggered in the chain reaction (including the firstly triggered one). 
The order numbers must be in ascending order and separated by single spaces.

Hint:
l   You might consider either a depth-first search or a breadth first 
search of mines to solve the problem

Sample Input:
6
0 0
2 2
4 2
10 10
-1 6
-1 7
2

Sample Output:
1 2 3 5 6
Note:
In the above sample:
(2, 2) triggers (0, 0); (2, 2) triggers (4, 2); (2, 2) triggers (-1, 6) and 
(-1, 6) then triggers (-1, 7); (10, 10) cannot be triggered.

*/


#include 
<iostream>
#include 
<fstream>
using namespace std;

struct point  
//炸弹的坐标,并且标明有否爆炸
{
    
int x, y;
    bool ex;
}
;

point
* p;  //炸弹数组
void quick_sort(int* arr, int left, int right); //快速排序

void main(void)
{
    
int n,exploit;
    
int px,py;
    ifstream from(
"C:/hello.txt");

/*    cout<<"please insert data"<<endl;
    cin>>n;
*/

    from
>>n;

    
if (n<2 || n>100)
    
{
        cout
<<"Error!!"<<endl;
        
return;
    }


    p 
= new point[n];
    
for (int i = 0; i<n ; i++)
    
{
        
//cin>>px>>py;
        from>>px>>py;
        
if (px<-1000 || px>1000 || py<-1000||py>1000)
        
{
            cout
<<"Error!"<<endl;
            
return;
        }

        p[i].x 
= px;
        p[i].y 
= py;
        p[i].ex 
= false;
    }


    
//cin>>exploit;
    from>>exploit;

    exploit
--;
    p[exploit].ex 
= true;
    
/*以上数据输入部分结束*/


    
//接着广度优先搜索爆炸的炸弹
    int *exp=new int[n];
    
int index = 0;
    
    exp[index] 
= exploit;
    index 
++;
    i 
= 0
    
while (i<=index-1)
    
{
        
for (int j =0; j<n; j++)
        
{
            
int bom = exp[i];
            
if (j != bom && !p[j].ex)
            
{
                
if ((p[bom].x -p[j].x)*(p[bom].x -p[j].x) + (p[bom].y-p[j].y)*(p[bom].y-p[j].y)<=25)
                
{
                    p[j].ex
=true;
                    exp[index] 
= j;
                    index
++;                    
                }

            }

        }

        i
++;
    }


    
//搜索完毕后对引爆了的炸弹数组进行排序输出
    quick_sort(exp, 0, index-1);
    
for (i = 0; i<index; i++)
    
{
        cout
<<exp[i]+1<<" ";
    }

    cout
<<endl;

}

void swap_num(int& x, int& y)
{
    
int temp = x;
    x 
= y;
    y 
= temp;
}


int partation(int* arr, int left, int right)
{
    
int pos=left;
    
int temp = arr[left];
    
int j = left +1;
    
while(temp > arr[j] && ++pos !=j)
    
{
        swap_num(arr[pos], arr[j]);
        j
++;
    }

    swap_num(arr[left], arr[pos]);
    
return pos;
}


void quick_sort(int* arr, int left, int right)
{
    
if (left<right)
    
{
        
int pos = partation(arr, left, right);
        quick_sort(arr,left,pos
-1);
        quick_sort(arr,pos
+1,right);
    }

}