求助 70分WA1,7,8想不通为什么
查看原帖
求助 70分WA1,7,8想不通为什么
868012
H2230819077楼主2024/7/14 22:02
#include<bits/stdc++.h>
using namespace std;
const int N=150;
int cnt;
int ans=1e5+10;
char g[N][N];
char fx[4]={'W','S','D','A'};
int dx[4]={-1,1,0,0},dy[4]={0,0,1,-1};
int ex,ey,sx,sy;
int n;
string p[N];
bool st[N][N];
int dist[N][N];
int tx[4][4]={{0,2,1,1},{2,0,1,1},{1,1,0,2},{1,1,2,0}};
struct node
{
	int x,y;
	char flag;
};
void bfs(int x,int y,char ch)
{
	queue<node> q;
	q.push({x,y,ch});
	dist[x][y]=0;
	while(!q.empty())
	{
		node t=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			int xx=t.x+dx[i],yy=t.y+dy[i];
			if(xx<1||yy<1||xx>cnt||yy>cnt)
			continue;
			if(g[xx][yy]=='x')
			continue;
			if(g[xx][yy]=='.'||g[xx][yy]=='B')
			{ 
				for(int j=0;j<4;j++)
				{
					if(t.flag==fx[j])
					{
						if(dist[xx][yy]>=dist[t.x][t.y]+tx[j][i])
						{
							dist[xx][yy]=dist[t.x][t.y]+tx[j][i];
							q.push({xx,yy,fx[i]});
						}
					}
				}
			}
		}
	}
}
int main()
{
	cin>>n;
	getchar();
	for(int i=1;i<=n;i++)
	getline(cin,p[i],'\n');
	for(int i=1;i<=n;i++)
	{
		cnt=1;
		for(int j=0;j<p[i].size();j++)
		{
			if(p[i][j]!=' ')
			g[i][cnt++]=p[i][j];
		}
	}
	cnt--;
	for(int i=1;i<=cnt;i++)
	{
		for(int j=1;j<=cnt;j++)
		{
			if(g[i][j]=='A')
			sx=i,sy=j;
			else if(g[i][j]=='B')
			ex=i,ey=j;
		}
	}
	for(int i=0;i<4;i++)
	{
		memset(dist,0x3f,sizeof dist);
		bfs(sx,sy,fx[i]);
		ans=min(ans,dist[ex][ey]);
	}
	if(ans==1e5+10)
	cout<<-1;
	else
	cout<<ans;
	return 0;
}
2024/7/14 22:02
加载中...