啊哈算法-炸弹人(未检查路径可通行)

先不写了,散步去。这段代码没有检查路径是不是可以通行。

#include<stdio.h>
void find(int x,int y);
int max=-1,max_x,max_y;
char a[50][50];
int main(){
    int h,w;
    scanf("%d%d",&h,&w);
    getchar();//万恶之源 多de十几分钟bug scanf后面如果接收字符一定要记得getchar。 
    for(int i=0;i<h;i++) gets(a[i]);
    for(int j=0;j<w;j++){
        for(int k=0;k<w;k++){
            if(a[j][k]=='.'){//找到可以放炸弹的位置 
                find(j,k);
            }
        }
    }
    printf("x=%d y=%d Max=%d",max_x,max_y,max);
    return 0;
}
void find(int x,int y){//查找某位置能够炸几个 
    int sum=0,tem_x=x-1,tem_y=y-1;
    while(tem_x>=0 && (a[tem_x][y]=='G' || a[tem_x][y]=='.')){//tem_x>=0防止越界访问 
        if(a[tem_x][y]=='G') sum++;
        tem_x--;
    }
    tem_x=x+1;
    while(a[tem_x][y]=='G' || a[tem_x][y]=='.'){
        if(a[tem_x][y]=='G') sum++;
        tem_x++;
    }
    while(tem_y>=0 && (a[x][tem_y]=='G' || a[x][tem_y]=='.') ){//tem_y>=0防止越界访问 
        if(a[x][tem_y]=='G') sum++;
        tem_y--;
    }
    tem_y=y+1;
    while(a[x][tem_y]=='G' || a[x][tem_y]=='.'){
        if(a[x][tem_y]=='G') sum++;
        tem_y++;
    }
    if(sum>max){
        max=sum;
        max_x=x;
        max_y=y;
    }
}
/*测试数据 
13 13
#############
#GG.GGG#GGG.#
###.#G#G#G#G#
#.......#..G#
#G#.###.#G#G#
#GG.GGG.#.GG#
#G#.#G#.#.###
##G...G.....#
#G#.#G###.#G#
#...G#GGG.GG#
#G#.#G#G#.#G#
#GG.GGG#G.GG#
#############
结果 x=9 y=9 max=8 
*/

 

posted @ 2021-12-14 16:33  m2on  阅读(35)  评论(0编辑  收藏  举报