题目来源: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. Winform开发常用控件之Checkbox和CheckedListBox

    Winform的开发基本都是基于控件事件的,也就是事件驱动型的. 多选框的放置和值的获取有很多种,这里介绍几个简单常用的方法 1.直接放置Checkbox,并获取Checkbox的值 上图 做法也非常 ...

  2. 微软必应·英雄会第三届在线编程大赛:几个bing?

    发布公司:微软亚太研发集团 有 效 期:2013-12-31至2014-02-01 难 度 等 级: 答 题 时 长:120分钟 编程语言要求:C C++ Java C# 悬赏详情 一等奖 : 价值2 ...

  3. 3种方式实现Java多线程

    java中实现多线程的方法有两种:继承Thread类和实现runnable接口. 1.继承Thread类,重写父类run()方法 public class thread1 extends Thread ...

  4. HTTP协议中PUT/GET/POST/HEAD等介绍

    HTTP协议中GET.POST和HEAD的介绍 GET: 请求指定的页面信息,并返回实体主体. HEAD: 只请求页面的首部. POST: 请求服务器接受所指定的文档作为对所标识的URI的新的从属实体 ...

  5. mysql 截取字符

    $where="(left(`type_id`,1)=1"; 返回 type_id 字段 从左边开始长度为1个字符 LEFT(str,len)返回字符串str的最左面len个字符. ...

  6. jQuery基础知识— 获得内容和属性

    jQuery拥有可操作HTML元素和属性的方法. 获得内容: text()--设置或返回所选元素的文本内容 html()--设置或返回所选元素的内容(包括HTML标记) val()--设置或返回表单字 ...

  7. Python初学者笔记:打印出斐波那契数列的前10项

    问题:斐波那契数列(意大利语: Successione di Fibonacci),又称黄金分割数列.费波那西数列.费波拿契数.费氏数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.- ...

  8. 如何在windows下安装python第三方包

    python安装第三方库一般方式和easy_install方式 2010-06-24 17:43:53|  分类: Python |  标签:python  |字号 订阅     python安装第三 ...

  9. SQLite判断某表是否存在

    SQLite判断表是否存在:其实很简单,只要查看sqlite_master表中是否存在这条数据就可以知道了.SELECT count(*) FROM sqlite_master WHERE type= ...

  10. spring aop 使用xml方式的简单总结

    spring aop的 xml的配置方式的简单实现: 1.编写自己的切面类:配置各个通知类型 /** * */ package com.lilin.maven.service.aop; import ...