WA#3#5求助!!!
查看原帖
WA#3#5求助!!!
947881
liuyize549330楼主2023/10/15 20:59
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,k;
struct re{
	int x,y,s;
};
queue<re> q;
char c[301][301];
bool vis[350][350][4];
int fx[]={0,1,0,-1};
int fy[]={1,0,-1,0};
bool check(int x,int y,int s){
	if(s<k){
		if(vis[x][y][0]) return false;
		for(int i=x-2;i<=x+2;i++){
			for(int j=y-2;j<=y+2;j++){
				if(c[i][j]=='*'||i<1||i>n||j<1||j>n)
				return false;
			}
		}
		vis[x][y][0]=1;
	}else if(s>=k&&s<2*k){
		if(vis[x][y][1]) return false;
		for(int i=x-1;i<=x+1;i++){
			for(int j=y-1;j<=y+1;j++){
				if(c[i][j]=='*'||i<1||i>n||j<1||j>n)
				return false;
			}
		}
		vis[x][y][1]=1;
	}else{
		if(vis[x][y][2]) return false;
		if(c[x][y]=='*') return false;
		vis[x][y][2]=1;
	}
	return true;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>c[i][j];
		}
	}
	vis[3][3][0]=1;
	q.push((re){3,3,0});
	int ans=1e9;
	while(!q.empty()){
		re x=q.front();q.pop();
		if(x.x==n-2&&x.y==n-2){
			ans=min(ans,x.s);
			continue;
		}
		bool r=false;
		for(int i=0;i<4;i++){
			int tx=x.x+fx[i];
			int ty=x.y+fy[i];
			if(tx<=n&&ty<=n&&tx>=1&&ty>=1&&check(tx,ty,x.s)){
				q.push((re){tx,ty,x.s+1});
				r=true;
			//	cout<<1;
			}
		}
		if(x.s<k)
			q.push((re){x.x,x.y,k});
		if(x.s>=k&&x.s<2*k){
			q.push((re){x.x,x.y,2*k});
		}
	}
	cout<<ans;
	return 0;
}
2023/10/15 20:59
加载中...