跳到正文

个人记录

独轮车

yong123456约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
int n,m,sx,sy;
struct qwq
{
	int x,y,d,c,t;
};
queue<qwq> q;
bool vis[30][30][4][5];
int dx[5]={-1,0,1,0};
int dy[5]={0,1,0,-1};
int a[100][100];
int bfs()
{
	q.push({sx,sy,0,0,0});
	vis[sx][sy][0][0]=1;
	while(!q.empty())
	{
		qwq s=q.front();
		q.pop();
		if(a[s.x][s.y]==2&&s.c==0)
		{
			return s.t;
		}
		int nx=s.x+dx[s.d],ny=s.y+dy[s.d];
		if(nx>=0&&nx<m&&ny>=0&&ny<n&&a[nx][ny]!=1)
		{
			int nc=(s.c+1)%5;
			if(!vis[nx][ny][s.d][nc])
			{
				vis[nx][ny][s.d][nc]=1;
				q.push({nx,ny,s.d,nc,s.t+1});
			}
		}
		int nd=(s.d+3)%4;
		if(!vis[s.x][s.y][nd][s.c])
		{
			vis[s.x][s.y][nd][s.c]=1;
			q.push({s.x,s.y,nd,s.c,s.t+1});
		}
		nd=(s.d+1)%4;
		if(!vis[s.x][s.y][nd][s.c])
		{
			vis[s.x][s.y][nd][s.c]=1;
			q.push({s.x,s.y,nd,s.c,s.t+1});
		}
	}
	return -1;
}
int main()
{

	char z;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>z;
			if(z=='#')
			{
				a[i][j]=1;
			}
			if(z=='S')
			{
				sx=i;
				sy=j;
			}
			if(z=='T')
			{
				a[i][j]=2;
			}
		}
	}
	int ans=bfs();
	if(ans==-1)
	{
		cout<<-1;
	}
	else
	{
		cout<<ans;
	}

	return 0;
}
原文