文章

60

粉丝

361

获赞

41

访问

498.2k

头像
bfs
P1126
发布于2021年1月22日 12:01
阅读数 8.6k

#include<iostream>
#include<string>
#include<string.h>
#include<cstring>
#include<stack>
#include<vector>
#include <queue>
#include <functional>
using namespace std;
const int maxn=10000;

char mpt[maxn][maxn];
int sum=0,flag;
struct node
{
	int x;
	int y;
	int step;
}beg;
int fangxiang[4][2]={0,-1,0,1,1,0,-1,0};

void bfs(int t)
{
	queue<node>q;
	node now,next;
	q.push(beg);
	mpt[beg.x][beg.y]='#';
	flag=0;
	while(!q.empty())
	{
		now=q.front ();
		q.pop();

		if(now.step==t)
			flag=1;//刚好充满或未充满,早已全部充满时step是达不到t的

		if(now.step>=t)
			continue;

		for(int i=0;i<4;i++)
		{
			next=now;
			next.x+=fangxiang[i][0];
			next.y+=fangxiang[i][1];

			//防止地图碰墙
			if(mpt[next.x][next.y]=='X'||mpt[next.x][next.y]=='#')
				continue;
			else if(mpt[next.x][next.y]=='.')
			{
				mpt[next.x][next.y]='#';
				next.step ++;
				q.push(next);
			}
		}
	}
}
int main()
{
	int m,...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发