CF253C Text Editor 题解

思路

既然要求最少步数,那我们可以用bfs

如果鼠标的位置比上一行的行末位置大,如果按上的话,移到上一行的行末。

如果鼠标的位置比下一行的行末位置大,如果按下的话,移到下一行的行末。
注

意当鼠标位置超过当前行最大值的时候,要让当前位置去到最大值处。

注意,需要文件读写。

代码

#include<bits/stdc++.h>
#include<cstring>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<map>
#define ll long long
#define lhs printf("\n");
using namespace std;
const int N=1e3+10;
const int M=1e5+10;
const int inf=0x3f3f3f3f;
int a[N];
int n;
int sx,sy,ex,ey;
int vis[114][M];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node
{
	int x,y,step;
};
void bfs(int xx,int yy)
{
	queue<node> q;
	vis[xx][yy]=1;
	q.push(node{xx,yy,0});
	while(q.size())
	{
		node now=q.front();
		q.pop();	
		if(now.x==ex and now.y==ey)
		{
			printf("%d",now.step);
			return;
		}
		for(int i=0;i<4;i++)
		{
			int nx=now.x+dx[i];
			int ny=now.y+dy[i];
			if(ny>=a[nx])
			{
				ny=a[nx];
			}
			if(nx>=1 and nx<=n and ny>=1 and ny<=a[nx] and vis[nx][ny]==0)
			{
				vis[nx][ny]=1;
				q.push(node{nx,ny,now.step+1});
			}
		}
	}
}
int main()
{	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		a[i]++;
	}
	scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
	bfs(sx,sy);
	return 0;
}

AC记录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值