文章

3

粉丝

405

获赞

1

访问

27.7k

头像
bfs
P1563 天津大学/南开大学2019年机试题
发布于2020年3月26日 21:06
阅读数 8.9k

#include<iostream>
#include<vector>
#include<queue>
#include<cstdio>
using namespace std;
int hang[4]={-1,1,0,0}; //上下左右四个方向
int lie[4]={0,0,-1,1};
int bfs(vector<vector<char> >&G,vector<vector<bool> >&vis,int x,int y,int h,int w){
	queue<pair<int,int> > q;
	q.push(make_pair(x,y));
	int res=0;
	while(q.size()!=0){
		res++;
		int N=q.size();
		while(N--){
			pair<int,int> p=q.front();
			q.pop();
			int m=p.first;
			int n=p.second;
			for(int i=0;i<4;i++){
				int new_x=m+hang[i];
				int new_y=n+lie[i];
				if(new_x>=0 && new_x<h && new_y>=0 && new_y<w && G[new_x][new_y]=='*' && vis[new_x][new_y]==false ){
					q.push(make_pair(new_x,new_y));
					vis[new_x][new_y]=true;//记得更新状态
				}else if(new_x>=0 && new_x<h && new_y>=0 && new_y<w && G[new_x][new_y]=='E'){
					return res;
				}
...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发