啊哈算法搜索应用之再解炸弹人(DFS与BFS)

//广搜
#include 
#include 
#include 
struct node{//存此点的坐标,消灭敌人数
    int x;
    int y;
    int sum;
};
struct node queue[20];//队列实现BFS
int head=0;
int tail=0;
char map[21][21];//存图
int book[21][21];//标记走过的点
int n;//图的规模
int max=-1;//消灭敌人最多的数量
int xmax,ymax;//消灭敌人最多数量的坐标
int next[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int getsum(int x,int y)//得到此点放炸弹消灭的敌人数
{
    int i,j;
    int sum=0;
    //向上统计
    i=x-1;
    j=y;
    while(map[i][j]!='#'){
        if(map[i][j]=='G')
            sum++;
        i--;
    }
    //向下统计
    i=x+1;
    j=y;
    while(map[i][j]!='#'){
        if(map[i][j]=='G')
            sum++;
        i++;
    }
    //向左统计
    i=x;
    j=y-1;
    while(map[i][j]!='#'){
        if(map[i][j]=='G')
            sum++;
        j--;
    }
    //向右统计
    i=x;
    j=y+1;
    while(map[i][j]!='#'){
        if(map[i][j]=='G')
            sum++;
        j++;
    }
    return sum;
}
void bfs(int x,int y)
{
    if(head>=tail)
        return;
    int xnext;
    int ynext;
    for(int i=0;i<4;i++){
        xnext=next[i][0]+x;
        ynext=next[i][1]+y;
        if(map[xnext][ynext]=='.'&&book[xnext][ynext]==0){
            book[xnext][ynext]=1;
            queue[tail].x=xnext;
            queue[tail].y=ynext;
            queue[tail].sum=getsum(xnext,ynext);
            if(queue[tail].sum>max){
                max=queue[tail].sum;
                xmax=xnext;
                ymax=ynext;
            }
            tail++;

        }
    }
    head++;
    bfs(queue[head].x,queue[head].y);

}
int main()
{
    scanf("%d",&n);
    getchar();
    for(int i=0;i

//深搜
#include 
#include 
#include 
struct node{//存此点的坐标,消灭敌人数
    int x;
    int y;
    int sum;
};
char map[21][21];//存图
int book[21][21];//标记走过的点
int n;//图的规模
int next[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int max=-1;//消灭敌人最多的数量
int xmax,ymax;//消灭敌人最多数量的坐标
int getsum(int x,int y)//得到此点放炸弹消灭的敌人数
{
    int i,j;
    int sum=0;
    //向上统计
    i=x-1;
    j=y;
    while(map[i][j]!='#'){
        if(map[i][j]=='G')
            sum++;
        i--;
    }
    //向下统计
    i=x+1;
    j=y;
    while(map[i][j]!='#'){
        if(map[i][j]=='G')
            sum++;
        i++;
    }
    //向左统计
    i=x;
    j=y-1;
    while(map[i][j]!='#'){
        if(map[i][j]=='G')
            sum++;
        j--;
    }
    //向右统计
    i=x;
    j=y+1;
    while(map[i][j]!='#'){
        if(map[i][j]=='G')
            sum++;
        j++;
    }
    return sum;
}
void dfs(int x,int y)
{
    int xnext,ynext;
    for(int i=0;i<4;i++){
        xnext=x+next[i][0];
        ynext=y+next[i][1];
        if(map[xnext][ynext]=='.'&&book[xnext][ynext]==0){
            book[xnext][ynext]=1;
            int sum=getsum(xnext,ynext);
            if(sum>max){
                xmax=xnext;
                ymax=ynext;
                max=sum;
            }
            dfs(xnext,ynext);
            //book[xnext][ynext]=0; //深搜不一定有此句,根据需求定
        }
    }
    return;
}
int main()
{
    scanf("%d",&n);
    getchar();
    for(int i=0;i<21;i++)
        memset(book[i],0,sizeof(book[i]));
    //读入图
    for(int i=0;i

你可能感兴趣的:(搜索(深搜/广搜/...),啊哈算法)