题目来源:http://poj.org/problem?id=3009

一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向。

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cstdlib>
5 #include<cmath>
6 #include<algorithm>
7 #include<queue>
8 #define inf 0x7fffffff
9 using namespace std;
10 int w,h;
11 int an[4][2]={-1,0, 1,0 ,0,-1, 0,1 };
12 int bn[25][25],vis[25][25];
13 int sx,sy,gx,gy;
14 int ans;
15 void dfs(int x,int y,int time)
16 {
17 if (time>=10) return ;
18 if (x<0 || x>=h || y<0 || y>=w) return ;
19 int ok=0;
20 int j=x,k=y;
21 int i;
22 for (i=j-1 ;i>=0 ;i--)
23 {
24 if (bn[i][k]==1) break;
25 if (bn[i][k]==3) {ans=min(ans,time+1) ;return ; }
26 }
27 if (i>=0 && i+1!=x)
28 {
29 bn[i][k]=0;
30 dfs(i+1,k,time+1);
31 bn[i][k]=1;
32 }
33 j=x ;k=y ;
34 for (i=j+1 ;i<h ;i++)
35 {
36 if (bn[i][k]==1) break;
37 if (bn[i][k]==3) {ans=min(ans,time+1) ;return ; }
38 }
39 if (i<h && i-1!=x)
40 {
41 bn[i][k]=0;
42 dfs(i-1,k,time+1);
43 bn[i][k]=1;
44 }
45 j=x ;k=y ;
46 for (i=y-1 ;i>=0 ;i--)
47 {
48 if (bn[j][i]==1) break;
49 if (bn[j][i]==3) {ans=min(ans,time+1) ;return ; }
50 }
51 if (i>=0 && i+1!=y)
52 {
53 bn[j][i]=0;
54 dfs(j,i+1,time+1);
55 bn[j][i]=1;
56 }
57 j=x ;k=y ;
58 for (i=y+1 ;i<w ;i++)
59 {
60 if (bn[j][i]==1) break;
61 if (bn[j][i]==3) {ans=min(ans,time+1) ;return ; }
62 }
63 if (i<w && i-1!=y)
64 {
65 bn[j][i]=0 ;
66 dfs(j,i-1,time+1);
67 bn[j][i]=1;
68 }
69 }
70 int main()
71 {
72 while (cin>>w>>h)
73 {
74 if (!w && !h) break;
75 for (int i=0 ;i<h ;i++)
76 {
77 for (int j=0 ;j<w ;j++)
78 {
79 scanf("%d",&bn[i][j]);
80 if (bn[i][j]==2) {sx=i ;sy=j ; }
81 if (bn[i][j]==3) {gx=i ;gy=j ; }
82 }
83 }
84 int ok=1;
85 if (sx>0 && bn[sx-1][sy]!=1) ok=0;
86 if (sx<h-1 && bn[sx+1][sy]!=1) ok=0;
87 if (sy>0 && bn[sx][sy-1]!=1) ok=0;
88 if (sy<w-1 && bn[sx][sy+1]!=1) ok=0;
89 if (ok) {cout<<-1<<endl;continue; }
90 memset(vis,0,sizeof(vis));
91 ans=inf;
92 dfs(sx,sy,0);
93 if (ans==inf) cout<<-1<<endl;
94 else cout<<ans<<endl;
95 }
96 return 0;
97 }

poj 3009 Curling 2.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(DFS + 模拟)

    题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...

  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 3009 Curling 2.0 回溯,dfs 难度:0

    http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...

  6. poj 3009 Curling 2.0( dfs )

    题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...

  7. 【POJ】3009 Curling 2.0 ——DFS

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Desc ...

  8. 【原创】poj ----- 3009 curling 2 解题报告

    题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  9. POJ P3009 Curling 2.0 题解

    深搜,向四个方向,在不越界的情况下一直闷头走,直到撞墙.到达终点就输出,没到就回溯. #include<iostream> #include<cstring> #include ...

随机推荐

  1. Vue.js学习 Item6 -- Class 与 样式绑定

    数据绑定一个常见需求是操作元素的 class 列表和它的内联样式.因为它们都是 attribute,我们可以用 v-bind 处理它们:只需要计算出表达式最终的字符串.不过,字符串拼接麻烦又易错.因此 ...

  2. "/Date(1405056837780)/" 时间转换

    //往往json传过来的时间都是"/Date(1405056837780)/" //转换需要的方法 String.prototype.ToString = function (fo ...

  3. LevelDB源码之四LOG文件

    “LOG文件在LevelDb中的主要作用是系统故障恢复时,能够保证不会丢失数据.因为在将记录写入内存的Memtable之前,会先写入Log文件,这样即使系统发生故障,Memtable中的数据没有来得及 ...

  4. 阿里云利用web直传文件到oss服务器

    http://files.cnblogs.com/files/adtuu/oss-h5-upload-js-direct.tar.gz

  5. 深入PHP empty(),isset(),is_null()

    PHP empty(),isset(),is_null()的实例测试.   有关 PHP编程 的 empty(),isset() 还有 is_null() 这三个函数的用法讨论得已经很多了,而且很多资 ...

  6. 基于OWIN WebAPI

    http://www.cnblogs.com/Irving/p/4607104.html http://www.cnblogs.com/xishuai/p/aspnet-webapi-owin-oau ...

  7. Python初学者笔记(3):输出列表中的奇数/奇数项,字符串中的偶数项,字符串大小写转换

    [1]a=[8,13,11,6,26,19,24]1)请输出列表a中的奇数项2)请输出列表a中的奇数 解:1) a=[8,13,11,6,26,19,24] print a[::2] Result:& ...

  8. 【转载/修改】ScrollLayout代码修正,追加模仿viewpager滚动速度

    组件作用为类似ViewPager但直接插视图的横向滚动容器. 修改自:http://blog.csdn.net/yaoyeyzq/article/details/7571940 在该组件基础上修正了滚 ...

  9. 刀哥多线程之主队列gcd-06-main_queue

    主队列 特点 专门用来在主线程上调度任务的队列 不会开启线程 以先进先出的方式,在主线程空闲时才会调度队列中的任务在主线程执行 如果当前主线程正在有任务执行,那么无论主队列中当前被添加了什么任务,都不 ...

  10. hdu 4324 Triangle LOVE

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4324 Triangle LOVE Description Recently, scientists f ...