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. hdu3124Arbiter(最小圆距离-扫描线)

    链接 详解http://blog.sina.com.cn/s/blog_6e7b12310100qnex.html #include <iostream> #include<cstd ...

  2. thinkphp分页显示

    先在Common\Comon里建一个function.php公共方法,然后在里面新建一个getpage方法,代码如下: <?php /** * TODO 基础分页的相同代码封装,使前台的代码更少 ...

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

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

  4. 【ufldl tutorial】Softmax Regression

    今天太长姿势了,什么叫懂了也写不出代码说的不就是我吗,就那么几行代码居然叽叽歪歪写了一个小时. 首先exercise要实现的是softmax的cost function和gradient,如下图: ( ...

  5. ORA-00205

    场景 数据库启动时报错.关闭前还是正常运行的,再次启动时,就报了以下错误. Copyright (c) , , Oracle. All rights reserved. Connected to an ...

  6. gcj_2016_Round1_B

    题目 一个NxN的矩阵,矩阵中每个方格中都有一个数值,且每一行的数值严格单调递增,每一列的数值严格单调递增.分别取出N行和N列,形成2N个长度为N的数组,现在有一个数组丢失,已知剩下的2N-1个长度为 ...

  7. Sublime3基础使用技巧

    1.安装SideBarEnhancements插件 ctrl+shift+p —> Install Package —> 找到SideBarEnhancements 2.安装CSS调色器: ...

  8. 通过yum安装nginx-mysql-php-fastcgi配置LNMP

    最近指想服务器跑静态文件,所以想单独配置个nginx的webserver,然而并不是我想象的那么简单,使用rpm包来安装会发生很多软件依赖的错误: 当我尝试使用yum安装nginx的时候,总是提示未找 ...

  9. dede标签调用

    关键描述调用标签: <meta name="keywords" content="{dede:field name='keywords'/}">&l ...

  10. 华为面试题——约瑟夫问题的C++简单实现(循环链表)

    /*     author:jiangxin     Blog:http://blog.csdn.net/jiangxinnju     Function:method of Josephus que ...