http://poj.org/problem?id=3009

如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍)

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = ;
int maz[maxn][maxn];
int n,m;
const int dx[] = {,-,,};
const int dy[] = {,,,-}; bool in(int x,int y)
{
return x >= && x < n && y >= && y < m;
} bool dfs(int x,int y,int step)
{
if(step == )return maz[x][y] == ;
for(int i = ; i < ; i++)
{
int tx = x + dx[i],ty = y + dy[i];
if(maz[tx][ty] == )continue;
while(in(tx,ty) && maz[tx][ty] == )
{
tx += dx[i];
ty += dy[i];
}
if(!in(tx,ty))continue;
if(maz[tx][ty] == )return true;
maz[tx][ty] = ;
if(dfs(tx - dx[i],ty - dy[i],step - ))
{
return true;
}
maz[tx][ty] = ;
}
return false;
}
int main()
{
while(scanf("%d%d",&m,&n) == && (m || n))
{
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
scanf("%d",maz[i] + j);
}
}
for(int x = ; x < n; x++)
{
for(int y = ; y < m; y++)
{
if(maz[x][y] == )
{
maz[x][y] = ;
bool fl = false;
for(int i = ; i < ; i++)
{
if(dfs(x, y ,i))
{
fl = true;
printf("%d\n",i);
break;
}
}
if(!fl)
{
puts("-1");
}
break;
}
}
}
}
return ;
}

POJ 3009 Curling 2.0 回溯,dfs 难度:0的更多相关文章

  1. POJ 3009 Curling 2.0【带回溯DFS】

    POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...

  2. poj 3009 Curling 2.0 (dfs )

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Desc ...

  3. poj 3009 Curling 2.0

    题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...

  4. POJ 3009 Curling 2.0 {深度优先搜索}

    原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...

  5. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  6. POJ 1321 棋盘问题 dfs 难度:0

    http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...

  7. sgu 125 Shtirlits dfs 难度:0

    125. Shtirlits time limit per test: 0.25 sec. memory limit per test: 4096 KB There is a checkered fi ...

  8. POJ 3009 Curling 2.0(DFS + 模拟)

    题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...

  9. poj 3009 Curling 2.0( dfs )

    题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...

随机推荐

  1. [转载] what's goole mock

    原文: https://code.google.com/p/googlemock/wiki/V1_7_ForDummies 地址被墙了, 看起来费劲, 转载一份 Google C++ Mocking ...

  2. javaMail编写案列

    package common.impl; import java.util.Properties; import javax.mail.BodyPart; import javax.mail.Mess ...

  3. poj1696Space Ant(逆时针螺旋形)

    链接 贪心做法,没次找最外面的点,也就是相对前面那条线偏转角度最小的点,除第一个点需要找到最下面的点即Y坐标最小,其余的每次进行极角排序. #include <iostream> #inc ...

  4. python中的实例方法、静态方法、类方法、类变量和实例变量

    class MyTest: myname = 'peter' # add a instance attribute    def __init__(self, name):        self.n ...

  5. Java源码初学_ArrayList

    一.ArrayList的构造器和构造方法 在ArrayList中定义了两个空数组,分别对应当指定默认构造方法时候,指向的数组已经给定容量,但是容量等于0的时候,指向的数组.此外在构造函数中传入Coll ...

  6. (一)解决Sublime Text 2中文显示乱码问题

    欲解决问题,关键在于让Sublime Text 2支持GB2312和GBK.步骤如下: 1.安装Sublime Package Control.   在Sublime Text 2上用Ctrl+-打开 ...

  7. Java 中使用 UEditor 整理【待续。。。】

    1.简介 官网:http://ueditor.baidu.com/website/index.html 演示:http://ueditor.baidu.com/website/examples/ 2. ...

  8. 【CDN】国外访问国内服务器网站-响应慢-CDN

    建议采用CDN海外加速方式: (1)CDN即内容分发网络(Content Delievery Network),它可以认为是建立在现有IP网络基础结构之上的一种增值网络.CDN技术将多点负载均衡.镜像 ...

  9. JSON和JSONP区别和联系

    由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socket通讯 ...

  10. 利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码

    利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码 2014-07-31 12:53 1047人阅读 评论(0) 收藏  ...