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. 禁止ubuntu的super快捷键

    在mac上安装了ubuntu虚拟机, 但是发现command健(ubuntu中叫super健)被系统占用了, 习惯了command健的同学来说非常不方便, 如何禁用默认的command健呢? You ...

  2. textarea还剩余字数统计

    <!DOCTYPE html><html><head> <meta charset="utf-8" /> <title> ...

  3. maven核心概念4

    一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Maven坐标主要组成 groupId:组织标识(包名) artifactId:项目名称 ver ...

  4. CentOS下源码安装MySQL

    一.创建mysql用户与组,相关目录 useradd mysql -s /sbin/nologin mkdir /usr/local/mysql chown -R mysql.mysql mkdir ...

  5. windows 环境变量 %SystemDrive% 和 %SystemRoot%

    %SYSTEMROOT% == c:\windows (or the root windows directory) %SYSTEMDRIVE% == the drive windows is ins ...

  6. apt-get的常用用法

    我们装完linux后的第一件事情就是安装软件了,下面的命令可以帮助你在Ubuntu发行版或基于Debain的发行版上快速的安装软件: sudo apt-get install package-name ...

  7. @synchronized (object)使用详解

    synchronized关键字代表这个方法加锁,相当于不管哪一个线 程A每次运行到这个法时,都要检查有没有其它正在用这个方法的线程B(或者C D等),有的话要等正在使用这个方法的线程B(或者C D)运 ...

  8. 【linux命令】:查看系统运行状态,命令top 【转载】

    原文:http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316399.html 简介 top命令是Linux下常用的性能分析工具,能够实时显示系 ...

  9. iOS开发 判断字符串是不是表情

    + (BOOL)stringContainsEmoji:(NSString *)string { __block BOOL returnValue = NO; [string enumerateSub ...

  10. PHP5中PDO的简单使用

    PHP5中PDO的简单使用 标签: php数据库mysql扩展extensionexception 2012-05-06 10:27 27753人阅读 评论(0) 收藏 举报  分类: PHP(6)  ...