poj 3009 Curling 2.0(dfs)

【题解】POJ 3009 Curling2.0(DFS) POJ 3009 Curling2.0 https://vjudge.net/problem/POJ-3009 题意 冰壶游戏,在一个游戏板上进行,板上有一个起点和一个终点并且有许多砖块,问冰壶从起点到终点最少要用的步数。 细则: 开始时,冰壶静止于起点。 冰壶静止时,你可以让它向上下左右移动,并且移动方向上的相邻点不能有砖块,如图a。 冰壶移动中无法主动停下,除非如下几种情况: ... 阅读详情

Language:
Curling 2.0
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11926 Accepted: 5051

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.

0vacant square
1block
2start position
3goal 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




题意:冰球游戏,遇到障碍物(1)就可以撞碎,求从2到3的最小步数


注意点:

   1、    如果这个点事起点,周围的点有障碍物,那么不能撞,即不能走这个方向(第三个样例)

  2、   最多10次移动冰球,剪枝

  3、 撞碎障碍物后,停在障碍物前面


其他的在代码中:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define N 30
#define INF 0x3f3f3f3f

int a[N][N],ans;
int n,m;
int step[4][2]={{0,-1},{1,0},{0,1},{-1,0}};

int judge(int x,int y)
{
	 if(x>=0&&x<n&&y>=0&&y<m)
		return 1;
	 return 0;
}

void dfs(int x,int y,int pos)
{
	if(pos>10) return ;

	if(a[x][y]==3)
	{
		if(ans>pos)
			ans=pos;
		return ;
	}

	int i;
	for(i=0;i<4;i++)
	{
		int xx=x;
		int yy=y;
         if(a[xx+step[i][0]][yy+step[i][1]]==1)          		//冰球还没有启动时如果这个方向有1,那么不能撞破这个1
			continue;

		while(1)
		{
			xx=xx+step[i][0];
			yy=yy+step[i][1];

			if(!judge(xx,yy))
				break;

			if(a[xx][yy]==1)
			{
				a[xx][yy]=0;
				dfs(xx-step[i][0],yy-step[i][1],pos+1);  //撞碎障碍物后,停在障碍物前面
				a[xx][yy]=1;
				break;
			}
			if(a[xx][yy]==3)
				dfs(xx,yy,pos);
	   }
	}
}

int main()
{
	int i,j,sx,sy;
	while(~scanf("%d%d",&m,&n),n+m)
	{
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				scanf("%d",&a[i][j]);
			     if(a[i][j]==2)
				 {
				 	sx=i;
				 	sy=j;
				 	a[sx][sy]=0;
				 }
			}
		 ans=INF;
		 dfs(sx,sy,1);
		 if(ans==INF)
			printf("-1\n");
		 else
			printf("%d\n",ans);
	}
   return 0;
}




【poj 3009】Curling 2.0 中文题意&题解&代码(C++) poj dfs 阅读详情

相关推荐

掌握POJ3009-Curling 2.0:DFS、向量、回溯与剪枝的实战运用

深度优先搜索算法是一种沿着树的分支进行探索直到末端,然后回溯到上一个节点继续探索的过程。它适用于各种图遍历场景,比如解决迷宫问题、拓扑排序以及求解连通性问题等。std::vector定义于头文件中,它封装了一个动态数组,并提供了一系列成员函数来管理这个数组。创建一个std::vector对象可以使用不同的构造函数来完成。// 创建一个空的vector// 创建一个有5个整数元素,初始值都是0的vector// 创建一个有5个整数元素,初始值都是1的vector。

weixin_28931507的博客 860

张三玩冰壶:C++用DFS深度优先搜索解POJ3009 Curling 2.0问题

POJ_3009_Curling 2.0 就不放英文题目了,去POJ搜题号就可以看到 简单理解一下题目: 张三去玩一种新出的冰壶项目,在一个棋盘一样的冰面上,他站在其中一格S上,从S点出发,有一些棋盘格有障碍物,他可以沿着上左下右四个方向运动,但是只有碰到障碍物才能停下来,而且碰到障碍物会停在运动方向上障碍物前面那个格子上,障碍物会消失,一个方向算一步,如果步数超过10或者不小心出界都算失败,问张三最短走几步能到终点G。 看一下输入: 一次性输入好几组数据,每一组开头是棋盘的横向距离和纵向距离(非常无奈我在

只想AC的博客 980

OMNET++ INET框架学习教程(一)

在自组网学习的道路上摸石头过河,帮你快速入门OIMNET++平台下的INET框架。

NLDwangzhe的博客 6562

【和小安一起刷题】POJ3009 Curling 2.0 ----深度优先搜索(DFS)

01、题目描述 小伙伴们大家好,这里是和 【和小安一起刷题 】 系列栏目,今天我们要做的是一道求最少步的问题——POJ3009 Curling 2.0,一般的最少步。走迷宫型问题都会涉及广度优先搜索(bfs),但是这道题用深搜来做却更容易实现,话不多说,先看题目吧。 题目链接: POJ3009 Curling 2.0. 题目描述:有一个w*h的冰面,在这些冰面上一些地方有冰块,给定起点(S) 与 终点(G) 的位置。 石头从起点开始运动,可以选择相邻处无冰块的方向前进,每次运动只有撞上冰块才会停止,否则会一

一枝之栖 303

MT6768平台Sensor Bring Up全流程实战:从驱动添加到问题排查(附避坑指南)

本文详细介绍了联发科MT6768平台Sensor Bring Up的全流程实战指南,从驱动添加、硬件配置到问题排查,提供了全面的避坑建议。内容涵盖环境准备、驱动文件集成、硬件接口配置、编译系统适配以及常见问题解决方案,帮助开发者高效完成传感器集成与调试工作。

weixin_30374009的博客 416

C# 进阶:打造WinForms中的动态交互式UserControl

本文深入探讨了如何在C# WinForms中创建动态交互式UserControl,以提升桌面应用的用户体验和代码复用性。通过剖析UserControl生命周期、事件体系,并结合一个可拖拽缩放的分割面板实战案例,详细讲解了鼠标键盘事件处理、自定义绘制、状态管理以及性能优化等核心技巧,帮助开发者封装复杂交互逻辑,打造专业级自定义控件。

weixin_29279153的博客 460

POJ-3009 Curling 2.0

POJ-3009 Curling 2.0 题目链接:POJ-3009 题目大意:打冰壶 你可以使冰壶向一个前面一格不为空的方向移动 冰壶遇到障碍会在障碍之前的位置停下来 并且将那个障碍击碎为空格 问你能不能到达给定的终点 能到达的话需要几步 解题思路:dfs即可 主要是注意每次dfs之前需要处理到一行的最末端 即走一行空格 另外注意10步之后默认为不能到达记得返回 这题一开始做麻烦了整整做了一下...

鱼头的水题之路 296

STM32驱动MG996R舵机的PWM控制与闭环原理

舵机是一种典型的模拟闭环伺服系统,其核心是通过电位器反馈实现角度位置的自动校正。工作原理基于负反馈控制:目标PWM脉宽决定期望角度,电位器输出实时反馈实际角度,控制电路计算偏差并驱动电机消除误差。该机制赋予舵机强带载能力与抗干扰性,是机器人、云台、智能执行器等场景的关键执行单元。在嵌入式开发中,STM32通过定时器生成标准20ms周期、0.5–2.5ms脉宽的PWM信号,需严格遵循电气规范,并解决PA15引脚SWJ复用冲突、定时器重映射及CCR精度计算等关键问题。本文深入解析MG996R舵机控制全流程,涵盖

weixin_42600407的博客 559

POJ 3009 dfs回溯

POJ3009 Curling 2.0 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format:%lld Java class name:Main Description On Planet MM-21, after their Olympic games this year, curlin...

CC_且听风吟丶的博客 499

C#上位机面试必问30题:机器视觉+工业通信+性能优化(含答案解析)

工业通信:围绕“稳定性”展开,重点答异常处理、断线重连、资源释放;机器视觉:围绕“精度+速度”展开,重点答预处理、ROI、参数优化;性能优化:围绕“UI不卡、内存不泄、效率不低”展开,重点答异步、限流、资源复用;面试加分项:结合实际项目案例(如“我在XX项目中用信号量解决了多设备并发卡顿问题”)。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1508

如何设置Xilinx以太网IP核的Pause帧处理

这里要注意,为什么是或,第一个条件是该功能使能取反,也就是说,如果你设置使能不打开的话,这个条件是直接满足的;先来看图中第二句,判断该以太网数据包为Global Pause帧(gpp),首先要满足Step1,然后是由上图中第二句代码(assign global_pause_packet)后面的所有项“与”(&&)起来的,就是说要满足后面所有的项,分别是DA符合,SA符合,Etype符合,Opcode符合,并且使能了该功能(即设置ctl_rx_enable_gpp=1)。如果没通过,则进入Step3。

qq_43416206的博客 617

POJ 3009 Curling 2.0(深度优先搜索+剪枝)

POJ 3009  Curling 2.0题目大意:在一块光滑的h*w的矩形平面上,有若干个障碍物,用1表示以及空格用0表示。现在有一个小球在平面上的数字2的地方,通过抛掷这个小球,使其达到数字3所在的敌方。小球可以向上下左右四个方向抛掷,抛掷后小球会不断地滚动,直到撞上障碍物,撞上障碍物后小球会停在障碍物旁边一格,并且这个障碍物会消失;如若不断滚动直到出界,则这种方式不可行;如若在滚动的过程中达...

ELYNABC的博客 438

windows搭建IIS(FTP)

适用于参加网络系统管理的学生进行学习与参考

a1119619789的博客 280

Curling 2.0 POJ - 3009(dfs)

Curling 2.0 POJ - 3009 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 whic...

rainbowsea_1的博客 249

京东/淘宝商品评论情感分析:基于Python的大规模评论爬取与NLP情感分析实战

在当今电商蓬勃发展的时代,用户评论已成为影响消费者购买决策的关键因素。本文将详细介绍如何使用Python最新技术栈爬取京东和淘宝商品评论,并运用先进的自然语言处理技术对评论进行情感分析,识别用户的好评与差评倾向。通过完整的代码实现和深入的技术解析,帮助读者掌握从数据采集到情感分析的全流程。本文详细介绍了如何使用Python最新技术栈实现京东和淘宝商品评论的爬取与情感分析。通过结合异步爬虫、反爬策略、BERT情感分析等先进技术,我们构建了一个完整的评论分析系统。高效爬取。

2201_76125261的博客 1402

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

poj 3009 Curling 2.0

poj 3009 Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21445 Accepted: 8742 Description On Planet MM-21, after their Olympic games this ye

代码屋 224

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

POJ 3009 Curling 2.0(DFS深度优先搜索)

//DFS深度优先搜索,WA了5次就因为1个while后面的条件差了一个……教训啊!!!#includeusing namespace std;int map[22][22],possible,minstep,w,h,beginx,beginy;void dfs(int x,int y,int r,int di)//di是4个方向的编号 1:向上 2:向右 3:向下 4:向左

Czy 1011

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

【poj3009】curling 2.0——dfs

题目: Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16112   Accepted: 6657 Description On Planet MM-21, after their Olympic games this yea

CFhM_R's Blog 723

poj 3009 Curling 2.0——dfs

Curling 2.0 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

five_east_west的博客 305

【ACM】【DFS】POJ 3009 Curling 2.0

Curling 2.0 点我前往题目链接 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28809 Accepted: 11408 Description On Planet MM-21, after their Olympic games this year, curling i...

lesileqin的博客 305

poj3009 Curling 2.0 DFS水

http://poj.org/problem?id=3009 题意:给定一个m*n的网格,在这些网格上一些地方有障碍物,给定起点与终点的位置,当石头从起点开始走,撞上障碍才会转弯,否则会一直沿着来时的方向继续前进。撞到障碍后停在障碍前的位置,障碍消失。然后石头可以选择四个方向(相邻处无障碍的方向)前进,问至少需要停多少次才能从起点到达终点。不能到达或者多余10步后游戏失败。如果能到达输...

147

[POJ3009]Curling2.0

[POJ3009]Curling 2.0 1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<limits.h> 5 using namespace std; 6 const int way[4][2]={{1,0...

weixin_30352645的博客 81

POJ 3009 Curling 2.0——————dfs

Curling 2.0 今年的奥运会之后,在行星mm-21上冰壶越来越受欢迎。但是规则和我们的有点不同。这个游戏是在一个冰游戏板上玩的,上面有一个正方形网格。他们只用一块石头。游戏的目的是让石子从起点到终点,并且移动的次数最小 图1显示了一个游戏板的例子。一些正方形格子可能被砖块占据。有两个特殊的格子,起始点和目标点,这是不占用块。(这两个方块是不同的)一旦石头开始移动就不会停下,除非它击中砖块块...

鸿杰 的博客 398

POJ3009 Curling 2.0【DFS】

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 ...

weixin_34080571的博客 87

POJ3009 Curling 2.0 DFS

果然退步到不行了。。这道dfs并不难,思路也大差不多,就是一直写不对。。。心情复杂.jpg 然后看了别人的写法,要注意几点:1. 一直前进到底时用while不用dfs会更方便(这个可以和之前做的ZOJ1259模拟栈的出栈操作放在一起想,如果某项操作中不会出现别的情况就直接写while而不要递归,否则就一点点来); 2.用sign标记某条路行不行得通,能走再进行后续(一直前进); 3.一个...

A confused programmer 353

POJ3009 Curling 2.0(DFS)

题意: 冰壶比赛,球会沿着直线一直运动下去,碰到阻碍会停下,同时阻碍消失,求最小步数 要点: DFS的变种,原本的DFS是一步一步的,现在是沿着直线一直下去,其实差不多,就是把直线看成一步就行。因为可以交错,所以不用设visit数组,同时要用回溯法。因为题目上说超过10步就失败,大大减少了时间复杂度,直接搜索每条路线就可以了。 15196974 Seasonal

SeasonJoe的博客 482
上一篇: poj 1321 棋盘问题(dfs)
下一篇: poj 1276 Cash Machine(dp 水题)
左眼皮跳跳~
博客等级 码龄13年 17粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值