[洛谷]P1332 血色先锋队

用到bfs扩散思想

ACcode:


#include
using namespace std;
#define int long long 
#define inf 0x3f3f3f3f
const int N=5e2+10;
int mp[N][N],n,m,a,b;
int idx[]={1,0,-1,0},idy[]={0,1,0,-1};
struct node{
	int x,y,k;
};
queueq;
bool vis[N][N];
void bfs(){
	while(!q.empty()){
		int x=q.front().x;
		int y=q.front().y;
		int k=q.front().k;
		q.pop();
		mp[x][y]=k;
		for(int i=0;i<4;i++){
			int xx=x+idx[i];
			int yy=y+idy[i];
			if(xx<1||xx>n||yy<1||yy>m||vis[xx][yy]) continue;
			vis[xx][yy]=true;
			q.push({xx,yy,k+1});
		}
	}
}
void solve(){
   cin>>n>>m>>a>>b;
   for(int i=1;i<=a;i++){
   	 int x,y;
   	 cin>>x>>y;
   	 q.push((node){x,y,0});
   	 vis[x][y]=true;
   }
   bfs();
   for(int i=1;i<=b;i++){
   	int x,y;
   	cin>>x>>y;
   	cout<

over~

你可能感兴趣的:(c++,算法,bfs)