0分求助qwq
查看原帖
0分求助qwq
194761
Isenthalpic楼主2020/5/22 14:34
#include<bits/stdc++.h>
#define ll long long
#define inf 1e9
using namespace std;
const int N=110;
int n,dp[N][N][N*2+1];
int sx,sy,ex,ey;
char mp[N][N];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			cin>>mp[i][j];
			if(mp[i][j]=='A')sx=i,sy=j;
			if(mp[i][j]=='B')ex=i,ex=j;
		}
	for(int k=1;k<=n*2+1;k++)
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				dp[i][j][k]=inf;
	dp[sx][sy][0]=0;
	for(int k=1;k<=n*2+1;k++)
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				if(mp[i][j]=='.'||mp[i][j]=='B')
				{
					if(mp[i-1][j]=='.')dp[i][j][k]=min(dp[i][j][k],dp[i-1][j][k-1]);
					if(mp[i][j-1]=='.')dp[i][j][k]=min(dp[i][j][k],dp[i][j-1][k-1]);
					if(mp[i+1][j]=='.')dp[i][j][k]=min(dp[i][j][k],dp[i+1][j][k-1]);
					if(mp[i][j+1]=='.')dp[i][j][k]=min(dp[i][j][k],dp[i][j+1][k-1]);
					if(k==1)continue;
					if((mp[i-1][j]=='.'||mp[i][j-1]=='.')&&mp[i-1][j-1]=='.')dp[i][j][k]=min(dp[i][j][k],dp[i-1][j-1][k-2]+1);
					if((mp[i+1][j]=='.'||mp[i][j+1]=='.')&&mp[i+1][j+1]=='.')dp[i][j][k]=min(dp[i][j][k],dp[i+1][j+1][k-2]+1);
					if((mp[i-1][j]=='.'||mp[i][j+1]=='.')&&mp[i-1][j+1]=='.')dp[i][j][k]=min(dp[i][j][k],dp[i-1][j+1][k-2]+1);
					if((mp[i+1][j]=='.'||mp[i][j-1]=='.')&&mp[i+1][j-1]=='.')dp[i][j][k]=min(dp[i][j][k],dp[i+1][j-1][k-2]+1);
				}
	if(dp[ex][ey][n*2+1]==inf)printf("%d",-1);
	else printf("%d",dp[ex][ey][n*2+1]);
	return 0;
}
2020/5/22 14:34
加载中...