讨厌转弯的机器人(广搜变形)

Problem Description
Let’s play maze game again! Maze problem is an old problem but here comes a challenge one. Max, an intelligent robot, is trapped in an N*M maze grid. He is located at a start position initially and aiming to find the exit of the maze. In the maze there are numbers of pillars which are set as obstacles. Max is energetic and not afraid of strolling in the maze at all. But he dislikes turning round in the maze all through. “Turning left or right several times keeps me uncomfortable and confused! It makes me feel sick and unconfident to run out of this maze!” Max said.
Given the cost of turning left and right of Max, the description of the maze grid, the start position and destination, you are going to give your hand to Max by calculating the minimum cost of turning round (no matter left or right) to get to the exit from start position.
Note: Max can just perform three operations at one time: go front, turn left, and turn right. Initially Max stands at the start point and you should decide in which direction he starts the first step in order to minimize the total dislike of Max.
Input
Input may consist of several test data sets. For each data set, it can be format as below:
First comes two integers in one line separating with one space: l(1 <= l <= 100) representing the cost of turning left of Max, r(1 <= r <= 100) representing the cost of turning right of Max.
Then six integers follows in next coming line separating with one space: r1 (1 <= r1 <= 100) representing the number of rows of the maze grid, c (1 <= c <= 100) representing the number of columns of the maze grid, sx (1 <= sx <= r1) representing the row position of the start position of Max, sy (1 <= sy <= c) representing the column position of the start position of Max, ex (1 <= ex <= r1) representing the row position of the exit of the maze, ey (1 <= ey <= c) representing the column position of the exit of the maze.Finally comes rl row(s) with c character(s) in each row. This part represents the maze data part and all character(s) can only be two types: ‘*’ representing a pillar of the maze and ‘.’ representing an empty grid cell that Max can stand on. Position of cell in the upperleft corner of the grid is (1, 1).Input is ended with l = r = 0.
Note: Max can’t go outside the range of the row and column of the maze, either go pass the pillar. You can assume the input is legal that means there is no pillar in the start position and exit of the maze.
Output
Output one integer in one line representing the minimum cost of turning to get to the exit if there is one way to get there, or output -1 in one line if it is impossible for Max to get to the destination.
Sample Input
1 2
1 4 1 1 1 4
....
1 2
1 4 1 1 1 4
..*.
1 2
2 4 2 1 2 4
....
..*.
1 2
3 4 2 1 2 4
*...
..*.
*...
0 0
Sample Output
0
-1
4

4

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#define Max 110
#define INF 1<<24
using namespace std;
int r,c,lt,rt,sx,sy,ex,ey,minn;
char g[Max][Max];
int vis[Max][Max][4];//前面二维表示坐标,第三维表示方向
int flag;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
struct point
{
	int x,y,step,dir;
	bool operator == (point &a) const//运算符重载
	{
		return a.x==x&&a.y==y;
	}
};
point st,ed;
void bfs()
{
	int i,j;
	queue<point>q;
	point p,np;
	for(q.push(st);!q.empty();q.pop())
	{
		p=q.front();
		//if(p.x==ed.x&&p.y==ed.y)
		if(p==ed)
		{
		    flag = 1;
			if(p.step<minn) minn=p.step;
			/*不能在此直接输出p.step,Wrong 了很多次,只能最后输出minn的值*/
		}
		for(i=0;i<4;i++)
		{
			/*只能向前,左转和右转,所以这几种情况不能用*/
			if(p.dir!=-1&&(p.dir==0&&i==1||p.dir==1&&i==0||p.dir==2&&i==3||p.dir==3&&i==2))
				continue;
			np.x=p.x+dir[i][0];
			np.y=p.y+dir[i][1];
			np.dir=i;
			if(np.x>=1&&np.x<=r&&np.y>=1&&np.y<=c&&g[np.x][np.y]!='*')
			{

				if(p.dir==-1||p.dir==np.dir)//直走
					np.step=p.step;
				else
				{
					if(p.dir==0&&np.dir==2||p.dir==1&&np.dir==3||p.dir==2&&np.dir==1||p.dir==3&&np.dir==0)//左转
						np.step=p.step+lt;
					else if(p.dir==0&&np.dir==3||p.dir==1&&np.dir==2||p.dir==2&&np.dir==0||p.dir==3&&np.dir==1)//右转
						np.step=p.step+rt;
				}
				if(vis[np.x][np.y][np.dir]==-1||vis[np.x][np.y][np.dir]>np.step)
				{/*note:每个点有不同的访问方式下有不同的权值,所以vis[np.x][np.y][np.dir]要取最小的值*/
					q.push(np);
					vis[np.x][np.y][np.dir]=np.step;
				}
			}
		}
	}
	if(flag)  printf("%d\n",minn);
	else printf("-1\n");
}
int main()
{
	//freopen("b.txt","r",stdin);
	int i,j;
	while(scanf("%d%d",<,&rt)==2&<+rt)
	{
		scanf("%d%d%d%d%d%d",&r,&c,&st.x,&st.y,&ed.x,&ed.y);
		for(i=1;i<=r;i++)
			scanf("%s",g[i]+1);
		st.step=0;
		st.dir=-1;
		flag = 0;
		minn = INF;
		memset(vis,-1,sizeof(vis));
		vis[st.x][st.y][0]=vis[st.x][st.y][1]=vis[st.x][st.y][2]=vis[st.x][st.y][3]=0;//初始位置要记得标记被访问
		bfs();
	}
	return 0;
}




基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不依赖物理位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估计与控制。文中结合嵌入式开发平台STM32 F4,采用如滑模观测器、扩展卡尔曼滤波或高频注入法等先进观测技术,实现对电机反电动势或磁链的估算,进而完成无传感器矢量控制(FOC)。同时,研究涵盖系统建模、控制算法设计、仿真验证(可能使用Simulink)以及在STM32硬件平台上的代码实现与调试,旨在提高电机控制系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电力电子、自动控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师。; 使用场景及目标:①掌握永磁同步电机无位置传感器控制的核心原理与实现方法;②学习如何在STM32平台上进行电机控制算法的移植与优化;③为开发高性能、低成本的电机驱动系统提供技术参考与实践指导。; 阅读建议:建议读者结合文中提到的控制理论、仿真模型与实际代码实现进行系统学习,有条件者应在实验平台上进行验证,重点关注观测器设计、参数整定及系统稳定性分析等关键环节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值