bestlv


私信TA

用户名:peikailv

访问量:929

签 名:

等  级
排  名 2870
经  验 2035
参赛次数 0
文章发表 11
年  龄 0
在职情况 学生
学  校 福州大学
专  业 计算机类

  自我简介:

解题思路:

注意事项:

参考代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
struct Node
{
	int x;
	int y;
	int steps;
};
int n,m;
char a[101][101];
bool bj[101][101]={0};
queue<Node> q;
int flag=0;
int xm[5]={0,1,-1,0,0};
int ym[5]={0,0,0,1,-1};
void bfs()
{
    Node start;
    start.x=1;
    start.y=1;
    start.steps=1;
    q.push(start);
    bj[1][1]=1;
	while(!q.empty())
	{
		if(q.front().x==n&&q.front().y==m) {return;}
		else{
		for(int i=1;i<=4;i++)
		{
			int tx=q.front().x+xm[i];
			int ty=q.front().y+ym[i];
		    if(a[tx][ty]=='.'&&bj[tx][ty]==0)
		    {
				Node newnode;
				newnode.x=tx;
				newnode.y=ty;
				bj[tx][ty]=1;
				newnode.steps=q.front().steps+1;
				q.push(newnode);
			}
		}
		q.pop();
		}
	}    
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
		for(int j=1;j<=m;j++)
		{
			cin>>a[i][j];
		}
	}
	bfs();
	cout<<q.back().steps<<endl;
	
	return 0;
}


 

0.0分

1 人评分

  评论区