三、搜索和二分 [Cloned] M - 搜索

原题:

On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.

Fig. 1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.


Fig. 1: Example of board (S: start, G: goal)

The movement of the stone obeys the following rules:

  • At the beginning, the stone stands still at the start square.
  • The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
  • When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. 2(a)).
  • Once thrown, the stone keeps moving to the same direction until one of the following occurs:
    • The stone hits a block (Fig. 2(b), (c)).
      • The stone stops at the square next to the block it hit.
      • The block disappears.
    • The stone gets out of the board.
      • The game ends in failure.
    • The stone reaches the goal square.
      • The stone stops there and the game ends in success.
  • You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.


Fig. 2: Stone movements

Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.

With the initial configuration shown in Fig. 1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. 3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. 3(b).


Fig. 3: The solution for Fig. D-1 and the final board configuration

题意:

这就是小时候玩过的一个小游戏啊,从起始位置弹出一个小球,碰到墙壁就在墙壁前边停下,并且把碰到的那块墙壁打掉,小球只能走正四个方向,问怎么走才能走到终点(终点阻力无限大,一走到上边直接停下)

扫描二维码关注公众号,回复: 2597731 查看本文章

题解:

依然是深搜,从四个方向进行深搜,不过要注意的是只有在停下的情况才能进行四个方向的深搜,不然只能朝着哪一个方向一直走直到碰到墙壁停下或者出界,也就是深搜包括两种情况,用一个变量cur表示是哪种情况,因为只用输出步数就可以不用具体的步骤,所以只用一个变量记录第一种也就是四个方向深搜的次数就可以了。

代码:AC

#include<iostream>
#include<cstring>
using namespace std;
int A[45][45];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int step=10000;
int m,n;
int min(int a,int b)
{
	return a<b?a:b;
}
void DFS(int x,int y,int cur,int num)
{
	int i;
	if(A[x][y]==3)
		step=min(step,num);
	if(num>10)
		return;
	if(cur==-1)
	{
		for(i=0;i<4;i++)
		{
			int xx=x+fx[i];
			int yy=y+fy[i];
			if(xx>=0&&xx<n&&yy>=0&&yy<m)
			{
				if(A[xx][yy]!=1)
					DFS(xx,yy,i,num+1);
			}
		}
	}
	else
	{
		int xx=x+fx[cur];
		int yy=y+fy[cur];
		if(xx>=0&&xx<n&&yy>=0&&yy<m)
		{
			if(A[xx][yy]!=1)
				DFS(xx,yy,cur,num);
			if(A[xx][yy]==1)
			{
				A[xx][yy]=0;
				DFS(x,y,-1,num);
				A[xx][yy]=1;
			}
		}
	}
}
	
int main()
{
	while(cin>>m>>n)
	{
		if(m+n==0)
			break;
		memset(A,0,sizeof(A));
		int i,j,startx,starty;
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				cin>>A[i][j];
				if(A[i][j]==2)
				{
					startx=i;
					starty=j;
				}
			}
		}
		step=10000;
		DFS(startx,starty,-1,0);
		if(step>10)
			cout<<"-1"<<endl;
		else
			cout<<step<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/npuyan/article/details/81416620
今日推荐