UDP广播协议叫吃饭


私信TA

用户名:Mustenaka

访问量:134872

签 名:

个人博客www.mustenaka.cn

等  级
排  名 12
经  验 23732
参赛次数 8
文章发表 196
年  龄 3
在职情况 学生
学  校 Sky_box
专  业 NE

  自我简介:

欢迎光临我的博客www.mustenaka.cn,Python,C#,U3D,C/C++开发合作可以找我

解题思路:
    几乎可以说是一个模板题目,记得收藏这种模板,BFS的模板
参考代码:

#include<bits/stdc++.h>
#define hh ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int maxn=105;
char mp[maxn][maxn];
int vis[maxn][maxn];
const int dir[4][2]= {0,1,0,-1,1,0,-1,0};
//方向向量 右,左,下,上
int t,n,m;
int out_x,out_y;
struct state {
	int x,y;
	int step;
};
bool check(state s) {
	if(!vis[s.x][s.y]&&s.x<n&&s.y<m&&s.x>=0&&s.y>=0) {
		return true;
	}
	return false;
}
int bfs(state st) {
	queue<state> q;
	state now,next;
	st.step=0;
	q.push(st);
	vis[st.x][st.y]=1;
	while(!q.empty()) {
		now=q.front();
		if(now.x==out_x&&now.y==out_y) {
			return now.step;
		}
		for(int i=0; i<4; i++) {
			next.x=now.x+dir[i][0];
			next.y=now.y+dir[i][1];
			next.step=now.step+1;
			if(check(next)) {
				q.push(next);
				vis[next.x][next.y]=1;
			}
		}
		//state temp=q.front();
		//printf("%d %d %d\n",temp.x,temp.y,temp.step);
		q.pop();
	}
	return -1; 
}
int main() {
	hh;
	cin>>t;
	while(t--) {
		state st;
		memset(mp,0,sizeof(mp));
		memset(vis,0,sizeof(vis));
		cin>>n>>m;
		for(int i=0; i<n; i++) {
			for(int j=0; j<m; j++) {
				cin>>mp[i][j];
				if(mp[i][j]=='S') {
					st.x=i;
					st.y=j;
					st.step=0;
				}
				if(mp[i][j]=='E') {
					out_x=i,out_y=j;
				}
				if(mp[i][j]=='#'){
					vis[i][j]=1;
				}
			}
		}
		cout<<bfs(st)<<endl;
		/*for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				cout<<vis[i][j]<<' ';
			}
			cout<<endl;
		}	*/
	}
	return 0;
}


 

0.0分

4 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

hh是干嘛的
2020-07-02 14:52:14
  • «
  • 1
  • »