题目链接: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. iOS 学习笔记 五 (2015.03.17)使用storyBoard进行tableview的跳转

    方法一: 点击tableviewCell后,按住ctrl键拖拽至想要跳转的新的界面.这样跳转的结果是,点击tableview中的任何一行都会跳转到新的界面.可以通过控制cell的 属性 userInt ...

  2. hadoop概述测试题和基础模版代码

    hadoop概述测试题和基础模版代码 1.Hadoop的创始人是DougCutting?() A.正确 B.错误答对了!正确答案:A解析:参考课程里的文档,这个就不解释了2.下列有关Hadoop的说法 ...

  3. org.hibernate.TransientObjectException

    使用JPA注解@ManyToMany做一个多对多的用例. 为了避免在删除主表数据时同时级联删除从表数据,JPA官方文档建议在主表的从表字段使用级联注解:CascadeType.PERSIST,Casc ...

  4. jQuery_Ajax_Json 异步接收PHP端传来的json数据

    [json]{"user_id":"1172940","rmb_point":"0","weixin_id&q ...

  5. ecstore2.0数据库词典

    数据库词典= 数据库tables列表 =|| Name | Comment ||| sdb_aftersales_return_product | 售后申请 || sdb_b2c_brand | 商品 ...

  6. bianwu | 数据行 | 填写意见

    protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e) { //首先判断是否是数据行 if (e.Row.RowT ...

  7. Java获取当前第几周【转】

    本文copy自:http://swxzqsd.blog.sohu.com/156208509.html 作者:camelcanoe String today = "2010-01-11&qu ...

  8. fastjson和json-lib的区别

    上次把原生json替换成了fastjson,发生很多地方不兼容,对这个也做了一些总结: 1.对于没有赋值的变量处理,json-lib会根据类型给出相应初始值,而fastjson直接忽略这个字段. 解决 ...

  9. SDUT 2416:Fruit Ninja II

    Fruit Ninja II Time Limit: 5000MS Memory limit: 65536K 题目描述 Have you ever played a popular game name ...

  10. SQL Server 索引和表体系结构(二)

    转自:http://www.cnblogs.com/chenmh 非聚集索引 概述 对于非聚集索引,涉及的信息要比聚集索引更多一些,由于整个篇幅比较大涉及接下来的要写的“包含列的索引”,“索引碎片”等 ...