1 条题解
-
1
公式bfs 详细见代码
#include<bits/stdc++.h> using namespace std; #define int long long int n,m,ans,sx,sy; char a[510][510];//地图 int d[510][510];//标记是否到过 int dx[]={-2,-2,-1,-1,1, 1,2, 2};//马的偏移 int dy[]={1, -1, 2,-2,2,-2,1,-1}; struct grid{//结构体记录x,y int x,y; }; void bfs(int x,int y){ queue<grid>q; q.push({x,y}); d[x][y]=0;//标记 while(!q.empty()){ int tx=q.front().x,ty=q.front().y; q.pop();//拿到队首并出队 for(int i=0;i<8;i++){ int nx=tx+dx[i],ny=ty+dy[i]; if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&a[nx][ny]!='x'&&d[nx][ny]==-1){//没出界&&不是湿地&&没到过 if(a[nx][ny]=='b')ans++;//吃东西的话 由于只会到一次 那么到了就++即可 d[nx][ny]=1;//标记到过了 q.push({nx,ny});//入队 } } } } signed main(){ cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ d[i][j]=-1; cin>>a[i][j]; if(a[i][j]=='S')sx=i,sy=j;//起始 } } bfs(sx,sy);//别忘了启动bfs cout<<ans; }
信息
- ID
- 7
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- 递交数
- 84
- 已通过
- 16
- 上传者