题目链接:http://poj.org/problem?id=3009

题意:从2出发,要到达3, 0可以通过,碰到1要停止,并且1处要变成0, 并且从起点开始沿着一个方向要一直前进,直至碰到1(或者3)处才能停止,(就是反射来反射去知道反射经过3).如果反射10次还不能到达3,就输出-1.

#include<cstdio>
#include<cstring>
int pic[][];
int ans, n, m;
const int dir[][] = {, -, , , , , -, };
int limit(int x, int y)
{
return (x>&&x<=n&&y>&&y<=m);
} void dfs(int x, int y, int step)
{
if(step>=) return;
for(int i=; i<; i++)
{
int nx = x+dir[i][];
int ny = y+dir[i][];
if(limit(nx, ny)&&pic[nx][ny]!=)
{
while(limit(nx, ny)&&pic[nx][ny]!=&&pic[nx][ny]!=)
{
nx +=dir[i][];
ny += dir[i][];
}
if(pic[nx][ny]==)
{
if(ans>step+)
ans = step+;
return;
}
else if(pic[nx][ny]==)
{
pic[nx][ny] = ;
dfs(nx-dir[i][], ny-dir[i][], step+);
pic[nx][ny] = ;
}
}
}
} int main()
{
int x, y;
while(scanf("%d%d", &m, &n), n||m)
{
memset(pic, , sizeof(pic));
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
{
scanf("%d", &pic[i][j]);
if(pic[i][j]==)
{
x = i;
y = j;
}
}
ans = 0x7fff;
dfs(x, y, );
if(ans>)
printf("-1\n");
else
printf("%d\n", ans);
}
return ;
}

POJ3009 Curling的更多相关文章

  1. POJ-3009 Curling 2.0 (DFS)

    Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...

  2. POJ3009——Curling 2.0(DFS)

    Curling 2.0 DescriptionOn Planet MM-21, after their Olympic games this year, curling is getting popu ...

  3. POJ3009 Curling 2.0

    正式做POJ的第一题,做出来后又看了别人的代码,就又完善了一下,也通过了.参考 http://blog.sina.com.cn/s/blog_4abcd9bc0100phzb.html 改了之后觉得写 ...

  4. poj3009 Curling 2.0(很好的题 DFS)

    https://vjudge.net/problem/POJ-3009 做完这道题,感觉自己对dfs的理解应该又深刻了. 1.一般来说最小步数都用bfs求,但是这题因为状态记录很麻烦,所以可以用dfs ...

  5. POJ-3009 Curling 2.0---DFS求最短路

    题目链接: https://vjudge.net/problem/POJ-3009 题目大意: 问题:打冰球.冰球可以往上下左右4个方向走,只有当冰球撞到墙时才会停下来,而墙会消失.当冰球紧贴墙时,不 ...

  6. poj3009 Curling 2.0 (DFS按直线算步骤)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14563   Accepted: 6080 Desc ...

  7. POJ3009 Curling 2.0(DFS)

    题目链接. 分析: 本题BFS A不了. 00100 00001 01020 00000 00010 00010 00010 00010 00030 对于这样的数据,本来应当是 5 步,但bfs却 4 ...

  8. POJ3009 Curling 2.0(DFS)

    迷宫问题求最短路. 略有不同的是假设不碰到石头的话会沿着一个方向一直前进,出界就算输了.碰到石头,前方石头会消失,冰壶停在原地. 把这个当作状态的转移. DFS能够求出其最小操作数. #include ...

  9. poj3009 Curling 2.0 深搜

    PS:以前看到题目这么长就没写下去了.今天做了半天,没做出来.准备看题解,打开了网站都忍住了,最后还是靠自己做出来的.算是一点进步吧. 分析: 题目的意思没明白或者理解有偏差都没办法做题.看样例3和样 ...

随机推荐

  1. linux设备驱动归纳总结(四):1.进程管理的相关概念【转】

    本文转载自;http://blog.chinaunix.net/uid-25014876-id-64866.html linux设备驱动归纳总结(四):1.进程管理的相关概念 xxxxxxxxxxxx ...

  2. STM32模拟I2C

    之前为了测试, 拿最小板做了一个I2C的主发跟主读, 一开始当然是尝试用硬件I2C, 结果弄了很久, 时间紧迫, 只好用了模拟, 结果发现, 哎, 真特么挺好用的, 现在1片儿顶过去5片儿. 硬件I2 ...

  3. MySQL存储引擎之InnoDB

    一.The InnoDB Engine Each InnoDB table is represented on disk by an .frm format file in the database ...

  4. awk 手册--【转载】

    1. 前言 有关本手册 : 这是一本awk学习指引,  其重点着重于 : l         awk 适于解决哪些问题 ? l         awk 常见的解题模式为何 ? 为使读者快速掌握awk解 ...

  5. php的内存分配还是很智能的

    <?php echo memory_get_usage().PHP_EOL;$a = 1;$b = $a;echo memory_get_usage().PHP_EOL; <?php ec ...

  6. 【转】MYSQL入门学习之七:MYSQL常用函数

    转载地址:http://www.2cto.com/database/201212/175864.html 一.数学函数  www.2cto.com           ABS(x)           ...

  7. 2015-09-17 001 日志与对话框公用类_public

    using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...

  8. 华东交通大学2016年ACM“双基”程序设计竞赛 1002

    Problem Description 今天小学弟又训练完了,但是小学弟又不想看球赛,于是小学弟看马赛了.他发现马鞍是一个奇怪的东西.于是小学弟根据马鞍定义了一种马鞍数:在一个二位矩阵中,马鞍数在当前 ...

  9. dubbo扩展

    dubbo源码版本:2.5.4 经统计,dubbo一共有31个扩展,如下: ------------------------------------------------------ META-IN ...

  10. 一次编译Android源码实验

    注意,本文只供参考,是老文章 1.必要的软件环境 sudo apt-get install build-essential sudo apt-get install make sudo apt-get ...