POJ 3009 Curling 2.0 回溯,dfs 难度:0
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的更多相关文章
- POJ 3009 Curling 2.0【带回溯DFS】
POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...
- 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
题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...
- POJ 3009 Curling 2.0 {深度优先搜索}
原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- POJ 1321 棋盘问题 dfs 难度:0
http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...
- 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 ...
- POJ 3009 Curling 2.0(DFS + 模拟)
题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...
- poj 3009 Curling 2.0( dfs )
题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...
随机推荐
- Swift语言学习之学习资源
(1) http://swift.sh (2) Let's Swift – WRITE THE CODE. CHANGE THE WORLD. http://letsswift.com (3)http ...
- js object(对象)
http://www.cnblogs.com/pingchuanxin/p/5773326.html Object(对象)是在所有的编程语言中都十分重要的一个概念,对于事物我们可以把他们看作是一个对象 ...
- 设置TextView下划线并响应点击事件(SpannableString)
下面是一个20行的完整Demo代码:基本原理是使用一个SpannableString并设置其ClickableSpan来响应点击事件. TextView useInfo = (TextView) fi ...
- J2EE 第二阶段项目(八)
类别统计差不多完成了! 还有个地区统计了!
- iOS开发 判断代理以及代理方法是否有人遵循
if (self.delegate && [self.delegate respondsToSelector:@selector]) { return YES; }
- Hbase之批量数据写入
/** * Created by similarface on 16/8/16. */ import java.io.IOException; import org.apache.hadoop.con ...
- 如何组织较大项目的MVC文件夹结构
现在还用不到,拷贝下来备用,原文链接 2016 年 9 月 第 31 卷,第 9 期 ASP.NET Core - ASP.NET Core MVC 的功能切分 作者 Steve Smith | 20 ...
- 编译maxscale
编译maxscale,需要依赖mariadb版本的MySQL.有自己的版本就是任性啊
- maven 添加支持编译jdk1.7
1.在<profiles>元素内增加如下内容 <profile> <id>jdk17</id> <activation> ...
- node.js关于传送数据的二三事
配置好node环境后 书写代码 目录结构: . 代码: <!DOCTYPE html> <html lang="en"> <head> < ...