POJ P3009 Curling 2.0 题解
深搜,向四个方向,在不越界的情况下一直闷头走,直到撞墙。到达终点就输出,没到就回溯。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
int map[][];
int xxx,yyy;
int ans=;
int tx[]={,-,,};
int ty[]={,,,-};
void dfs(int x,int y,int step)
{
if(step>=ans||step>=)
{
return;
}
for(int i=;i<;i++)
{
int sx=x+tx[i];
int sy=y+ty[i];
if(map[sx][sy]==)continue;
while(sx>=&&sy>=&&sx<n&&sy<m&&map[sx][sy]!=&&map[sx][sy]!=)
{
sx+=tx[i];
sy+=ty[i];
}
if(sx<||sy<||sx==n||sy==m)continue;
else
{
if(map[sx][sy]==)
{
ans=step+;
return;
}
else
{
map[sx][sy]=;
dfs(sx-tx[i],sy-ty[i],step+);
map[sx][sy]=;
}
}
}
}
int main()
{
while(cin>>m>>n&&n!=&&m!=)
{
ans=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]==)
{
xxx=i;
yyy=j;
}
}
}
dfs(xxx,yyy,);
if(ans==) cout<<-<<endl;
else cout<<ans<<endl;
}
}
请各位大佬斧正(反正我不认识斧正是什么意思)
POJ P3009 Curling 2.0 题解的更多相关文章
- poj 3009 Curling 2.0 (dfs )
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11879 Accepted: 5028 Desc ...
- POJ 3009 Curling 2.0 {深度优先搜索}
原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...
- POJ 3009 Curling 2.0【带回溯DFS】
POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...
- poj 3009 Curling 2.0
题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...
- POJ 3009 Curling 2.0(DFS + 模拟)
题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...
- POJ 3009 Curling 2.0 回溯,dfs 难度:0
http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...
- poj 3009 Curling 2.0( dfs )
题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...
- 【POJ】3009 Curling 2.0 ——DFS
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11432 Accepted: 4831 Desc ...
- POJ 3009:Curling 2.0 推箱子
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14090 Accepted: 5887 Desc ...
随机推荐
- leetcode的Hot100系列--347. 前 K 个高频元素--hash表+直接选择排序
这个看着应该是使用堆排序,但我图了一个简单,所以就简单hash表加选择排序来做了. 使用结构体: typedef struct node { struct node *pNext; int value ...
- golang 之 flag
针对官网对flag 的定义解释一堆,对与我来说看了许久只想获取它的用法时,特意去整理一下.能快速使用并掌握它. 查看flag源码大致定义几下几种格式 //定义一个字符串的变量 type string ...
- Python 读取excel指定的列
一.摘要 在这篇文章中: https://www.cnblogs.com/xiao987334176/p/9330368.html#autoid-4-5-2 介绍了使用 xlrd 模块,读取指定坐标的 ...
- The four Day 给出一个平衡字符串,将它分割成尽可能多的平衡字符串
""" 在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的. 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串. 返回可以通过分割得到的平衡字符串的 ...
- ansible debugger 模块
在搞TF(tungstenfabric)时遇到了一些错误,TF通过ansible playbook 来部署的.通常情况下遇到错误都是通过ansibale xxxx –vvv 来详细输出一下.出错的类型 ...
- C#字符串基础
C#字符串基础 1. 字符串的两种创建形式 (1)String A=”cat”; (2)String B=new string{‘a’,4} .调用类方法,创建一个“aaaa”的字符串 (3) ...
- JAVA字符串比较,BigDecimal比较
import java.math.BigDecimal; public class StrCompareTo { public static void main(String[] args) { Sy ...
- 基于SVM的道路简单分割
折腾了几天了,这个看似简单的东西,怎么做起来那么费劲啊? 任重而道远,光玩,光去幻想,是什么也做不出来的,要一点一点儿大量时间与精力的投入,才能出结果的. (点击下图,可选择原图观看,清晰的效果) 2 ...
- requirejs:模块加载(require)及定义(define)时的路径理解
给新来的实习生普及下JS基本知识,看到比较好的文章 转载https://blog.csdn.net/xuxiaoping1989/article/details/52384778 接触过require ...
- springmvc 注解二
@SessionAttributes @sessionattributes注解应用到Controller上面,可以将Model中的属性同步到session作用域当中. SessionAttribute ...