题目
题解:
考察点: 搜索
题解:
如果最开始位置 ( 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<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<8;i++){
int nx=t.first+dx[i],ny=t.second+dy[i];
if(nx<1||nx>n||ny<1||ny>m||s[nx][ny]!='.') continue;
int num=Count(nx,ny);
if(num==0) que.push(make_pair(nx,ny));
}
}
}
int main()
{
cin>>n>>m>>x>>y;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>s[i][j];
if(s[x][y]=='*') cout<<"GG"<</p></int,int>
1060

被折叠的 条评论
为什么被折叠?



