求助
查看原帖
求助
704234
Sad_Rex楼主2023/3/28 18:57
#include <bits/stdc++.h>
using namespace std;

int n, k;
char ch[400][400];
queue <pair<int , int> > q;
int dx[] = { 0 , 0 , 1 , -1 };
int dy[] = { 1 , -1 , 0 , 0 };
int step[400][400];
int num;
bool check( int x , int y , int w )
{
	for( int i = -w ; i <= w ; i ++ )
	{
		for( int j = -w ; j <= w ; j ++ )
		{
			if( ch[x + i][y + j] == '*' ) return 0;
		}
	}
	return 1;
}
void bfs()
{
	q.push(make_pair(3 , 3));
	memset(step , -1 , sizeof(step));
	step[3][3] = 0;
	while( !q.empty() )
	{
		int x = q.front().first, y = q.front().second, w = 2 - (step[x][y] >= k) - (step[x][y] >= 2 * k);
		q.pop();
		if( x == n - 2 && y == n - 2 )
	    {
	        cout << step[x][y];
	    	exit(0);
	    }
		for( int i = 0 ; i < 4 ; i ++ )
		{
			int xx = x + dx[i], yy = y + dy[i];
			if( xx  - w >= 1 && xx + w <= n && yy - w >= 1 && yy + w <= n && check(xx , yy , w) && step[xx][yy] == -1 )
			{
				q.push(make_pair(xx , yy));
				step[xx][yy] = step[x][y] + 1;
			}
		}
		if( w != 0 )
		{
		    q.push( make_pair(x , y) );
		    step[x][y] ++;
		}
	}
	return;
}

int main()
{
	cin >> n >> k;
	for( int i = 1 ; i <= n ; i ++ )
	{
		for( int j = 1 ; j <= n ; j ++ )
		{
			cin >> ch[i][j];
		}
	}
	bfs();
	
	return 0;
}

2023/3/28 18:57
加载中...