HDOJ-三部曲一(搜索、数学)-1003-Curling 2.0-程序员宅基地

技术标签: java  数据结构与算法  

Curling 2.0

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 22   Accepted Submission(s) : 10
Problem Description

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

 
Input

The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.

Each dataset is formatted as follows.

the width(=w) and the height(=h) of the board First row of the board ... h-th row of the board

The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.

Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.

0 vacant square
1 block
2 start position
3 goal position

The dataset for Fig. D-1 is as follows:

6 6 1 0 0 2 1 0 1 1 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 1 1

 
Output

For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.

 
Sample Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
 
Sample Output
1
4
-1
4
10
-1
 
Source
PKU
 
 
 
 
这道题花了很长时间,原先看到求最短的走法就以为BFS,但发现没有办法压缩状态,后来看了POJ的Discuss才知道要DFS,原来DFS也可是求最短路径。
这题的模拟也很让我头疼。总算写出来了却TLE,后来和别人的代码一对照才知道step超过10时要剪枝。。。最后终于过了居然要157ms。。。。
 
 
#include<iostream>
#include<cstring>
using namespace std;


struct pos
{
	int x,y;
};

int w,h,Min,step=0;
pos p;
int board[101][101];

void DFS(pos p)
{
	int i;
	if(step>=10)                                     //step大于等于10时,不能走了,剪枝,回退
		return;
	if(p.x+1<h&&board[p.x+1][p.y]!=1)                //如果下一步不是石头,且不越界,就走试试看
	{
		step++;
		for(i=1;i<h-p.x&&board[i+p.x][p.y]!=1;i++)   //一步一步验证是否能到终点,是否碰到石头
		{
			if(board[i+p.x][p.y]==3)                 //到达终点,如果step比当前最小值小,保存step的值
			{
				if(Min>step)
					Min=step;
				step--;
				return;
			}
		}
		if(i<h-p.x)                                 //如果不划出边界
		{	
			board[i+p.x][p.y]=0;                    //碰到的石头变成空白
			p.x+=i-1;                               //移动到当前位置
			DFS(p);
			board[p.x+1][p.y]=1;
			p.x-=i-1;
		}
		step--;
	}
	if(p.x-1>=0&&board[p.x-1][p.y]!=1)
	{

		step++;
		for(i=1;i<=p.x&&board[p.x-i][p.y]!=1;i++)
		{
			if(board[p.x-i][p.y]==3)
			{
				if(Min>step)
					Min=step;
				step--;
				return;
			}	
		}
		if(i<=p.x)
		{	
			board[p.x-i][p.y]=0;
			p.x-=i-1;
			DFS(p);
			board[p.x-1][p.y]=1;
			p.x+=i-1;
		}
		step--;
	}
	if(p.y+1<w&&board[p.x][p.y+1]!=1)
	{
		step++;
		for(i=1;i<w-p.y&&board[p.x][p.y+i]!=1;i++)
		{
			if(board[p.x][p.y+i]==3)
			{
				if(Min>step)
					Min=step;
				step--;
				return;
			}
		}
		if(i<w-p.y)
		{	
			board[p.x][p.y+i]=0;
			p.y+=i-1;
			DFS(p);
			board[p.x][p.y+1]=1;
			p.y-=i-1;
		}
		step--;
	}
	if(p.y-1>=0&&board[p.x][p.y-1]!=1)
	{
		step++;
		for(i=1;i<=p.y&&board[p.x][p.y-i]!=1;i++)
		{
			if(board[p.x][p.y-i]==3)
			{
				if(Min>step)
					Min=step;
				step--;
				return;
			}
		}
		if(i<=p.y)
		{	
			board[p.x][p.y-i]=0;
			p.y-=i-1;
			DFS(p);
			board[p.x][p.y-1]=1;
			p.y+=i-1;
		}
		step--;
	}
	return;
}

int main()
{
	while(cin>>w>>h&&(w+h))
	{
		Min=11;
		int i,j;
		step=0;
		memset(board,0,sizeof(board));
		for(i=0;i<h;i++)
		{
			for(j=0;j<w;j++)
			{
				cin>>board[i][j];
				if(board[i][j]==2)
				{
					p.x=i;
					p.y=j;
				}
			}
		}
		DFS(p);
		if(Min==11)
			cout<<-1<<endl;
		else
			cout<<Min<<endl;

	}
}

 

 

转载于:https://www.cnblogs.com/aljxy/p/3343569.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30651273/article/details/95160092

智能推荐

Prometheus + Grafana 图形化监控实践_prometheus图形化监控-程序员宅基地

文章浏览阅读1k次。本文将详细介绍Prometheus和Grafana的快速搭建,并实现JVM、Mysql等实时监控。本文将在Windows环境搭建Demo。_prometheus图形化监控

全球十大农业大数据经典案例-程序员宅基地

文章浏览阅读4.2k次。基于物联网等技术的应用,农业领域积累了大量的数据,为大数据应用于农业奠定了基础。从国内国际的发展来看,大数据正在驱动农业发展路径发生变化,以提高农业效率,保障食品安全,实..._农业大数据应用案例

Mac在Dock程序坞上添加分割线,分割APP图标_mac程序坞分割线-程序员宅基地

文章浏览阅读9.9k次。遗憾分割线无法添加,我们只能添加一个空白的透明图标,来充当分割线,效果如下:添加方法按下F4,打开“其他”文件夹,打开“终端” 输入以下两行命令,回车 defaults write com.apple.dock persistent-apps -array-add '{ "tile-type" = "spacer-tile"; }'killall Dock 拖动空白的图标到需要的地方 完成..._mac程序坞分割线

git revert 撤销中间的某次提交_git revert --continue-程序员宅基地

文章浏览阅读5.9k次。使用场景如下:首先看一下我的提交(commit1这种都是指的是提交的commit-id)commit1commit2commit3commit4commit5commit6现在想把commit4扔掉,只需git log 从这里拿到commit4的id(当然咱们这里已经拿到了,coomit4就是)git revert commit4 正常情况下就撤销成功了..._git revert --continue

机器学习数据集之鸢尾花-程序员宅基地

文章浏览阅读1.4k次。Iris数据集是常用的分类实验数据集,由Fisher, 1936收集整理。Iris也称鸢尾花卉数据集,是一类多重变量分析的数据集, 它包含150个数据集,分为3类,每类50个数据,每个数据包含4个属性。自变量 feature 特性petal length 花瓣长度petal width 花瓣宽度sepal length 花萼长度sepal width 花萼宽度因变量..._book7_ch08_核技巧__机器学习__鸢尾花书__从加减乘除到机器学习.pdf

R in action 读书笔记(1)--第五章:高级数据管理-程序员宅基地

文章浏览阅读149次。5.2.1数学函数函数描述abs(x)绝对值sqrt(x)平方根ceiling(x)不小于x的最小整数floor(x)不大于x的最大整数trunc(x)向0的方向截取的X中的整数部分..._r in action 学习笔记:第五章

随便推点

计算机专业程序员单词分享含Anki牌组版_anki 牌组 单词-程序员宅基地

文章浏览阅读2.6k次,点赞17次,收藏8次。许多对计算机或者编程感兴趣的小伙伴都苦于英语脱了后腿所以特在此分享自己整理和和网络整合的计算机基础1500词分享Anki版为自己手动制作,以下为预览界面如果觉得不好看或者不想使用Anki的可以把文档导入到背单词软件,当然有的软件像有道、百词斩没有针对计算机的解释,所以推荐用欧陆你掌握这1500词之后相信日常编程,软件使用都可以应付了,但有的同学就是对自己要求高,就不想用百度,想在谷歌进行搜索,或者使用GitHub啊,Stack Overflow社区啊,可能这些单词就不够用了,所以这里还准备了一个_anki 牌组 单词

麻雀算法极限学习机(SSA-ELM)回归预测及其MATLAB代码实现-程序员宅基地

文章浏览阅读112次。SSA-ELM通过结合麻雀算法和极限学习机,能够优化ELM的隐层神经元数量和激活函数的选择,从而提高回归预测的性能。通过使用麻雀算法搜索的方式,SSA-ELM能够找到最佳的隐层神经元数量和激活函数,从而提高ELM的预测性能。极限学习机(ELM)是一种单隐层前馈神经网络模型,其特点是随机初始化输入层到隐层之间的连接权重和隐层的偏置,然后通过解析解的方式快速求解输出层到隐层之间的连接权重。麻雀算法极限学习机(SSA-ELM)是一种基于麻雀算法和极限学习机(ELM)的回归预测方法。极限学习机(ELM)简介。_ssa-elm

LaTeX 日语_setcjkmainfont{ipamincho}-程序员宅基地

文章浏览阅读3.6k次。有许许多多的包支持在不同 LaTeX 编译环境下的日语的输入,但它们并不是都支持特定的日语输入习惯,例如垂直方向的文字。本文简要介绍如何使用 pdfLaTeX、XeLaTeX、pTeX 和 LuaLaTeX 来输入日语。_setcjkmainfont{ipamincho}

Windows下查看端口占用情况_查看8080端口被哪个进程占用-程序员宅基地

文章浏览阅读5k次,点赞2次,收藏15次。编程的时候经常发现我们需要使用的端口被别的程序占用,这个时候需要清楚查看是哪个程序占用了端口,用且清除了这个进程!,回车,查看是哪个进程或者程序占用了2668端口,结果是:TIM.exe。注:后两步可以使用任务管理器,因为看的比较直观而且方便。,回车,记下最后一位数字,即PID,这里是2668。,点击查看—>选择列,_查看8080端口被哪个进程占用

hive中生成从1到n的连续数字的UDTF方法_hive生成连续数字-程序员宅基地

文章浏览阅读9.3k次。之前在博客中分享了个生成从1到n的连续数字的transform或map方法,最近研究了一下UDTF,发现用UDTF写出来的函数用起来更方便,语法更接近于普通的函数。而且在JAVA中对参数的个数、类型等进行校验也更容易。 ORACLE中生成从1到n的连续数字的方法很多,最简单的一种是:select level from dual connect by level _hive生成连续数字

解决阿里云Tomcat8080端口拒绝访问_阿里云8080拒绝连接请求-程序员宅基地

文章浏览阅读878次。解决阿里云Tomcat8080端口拒绝访问_阿里云8080拒绝连接请求

推荐文章

热门文章

相关标签