校招算法笔面试 | 校招笔面试真题-大家来扫雷

题目

题目链接

题解:

考察点: 搜索

题解:

如果最开始位置 ( x , y ) (x,y) (x,y)是炸弹,则不存在可扩展的可能性,一定输出 G G GG GG。否则其他情况一定是可以扩展的。采用广度优先搜索 ( b f s ) (bfs) (bfs)进行坐标的扩展。对于一个位置 ( x , y ) (x,y) (x,y),对其周围 8 8 8个方向进行遍历,如果有越界,或者不能扩展的位置,则剪掉;如果周围 8 8 8个方向存在数字为 0 0 0的,则将其加入队列,作为新的 ( x , y ) (x,y) (x,y)进行新一轮的扩展,直到所有的点都遇到数字边界或者地图边界。

#include "bits/stdc++.h"
using namespace std;
typedef pair<int,int> P;
const int maxn=1e3+10;
int n,m,x,y;
char s[maxn][maxn];
int dx[]={-1,-1,-1,0,0,1,1,1};
int dy[]={-1,0,1,-1,1,-1,0,1};
int Count(int x,int y){
    int cnt=0;
    for(int i=0;i&lt;8;i++){
        int nx=x+dx[i],ny=y+dy[i];
        if(s[nx][ny]=='*') cnt++;
    }
    s[x][y]=cnt+'0';
    return cnt;
}
void bfs(int x,int y){
    queue<p>que;
    que.push(make_pair(x,y));
    while(!que.empty()){
        P t=que.front();
        que.pop();
        for(int i=0;i&lt;8;i++){
            int nx=t.first+dx[i],ny=t.second+dy[i];
            if(nx&lt;1||nx&gt;n||ny&lt;1||ny&gt;m||s[nx][ny]!='.') continue;
            int num=Count(nx,ny);
            if(num==0) que.push(make_pair(nx,ny));
        }
    }
}
int main()
{
    cin&gt;&gt;n&gt;&gt;m&gt;&gt;x&gt;&gt;y;
    for(int i=1;i&lt;=n;i++)
        for(int j=1;j&lt;=m;j++)
            cin&gt;&gt;s[i][j];
    if(s[x][y]=='*') cout&lt;&lt;"GG"&lt;</p></int,int>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值