题意:给你一个地图,上面有一些‘X',给你起点终点,让你输出从起点到终点的路径中转向(改变方向)次数最少的路径,注意,不能穿过别的’X'并且可以超过边界

题解:关于超过边界,只要在外围多加一圈‘ ’。然后正常dfs就行。

   关于转向次数的记录,每次dfs时传递上一次的方向f,然后进行判断。

技巧:if判断用几个flag来简化表达式。(t1t2t3)

坑:1.dfs的方向顺序原来不能随便打的啊。。。//要绕圈,不然会同方向来回走位,不像有vis数组的bfs。

2.getline 循环时只需要第一次getchar

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <map>
#include<stack>
#include<set>
#include<string.h> #define pb push_back
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i) using namespace std;
const int N = + ;
//double num[N], price[N], ave[N]; int board[N][N], mark[N][N];
int w, h, mn;
int dir[][] = { , ,-,, ,-, , };
void Search(int now_x, int now_y, int end_x, int end_y, int step, int f) {//f 上一步的方向。
if (step > mn)return;
if (now_x == end_x&&now_y == end_y) {
if (mn > step) {
mn = step;
return;
}
}
_for(i, , ) {
int x = now_x + dir[i][];
int y = now_y + dir[i][]; int t1 = , t2 = , t3 = ;
if (((x > - )&&( x < w + )) && ((y > -) &&( y < h + )))t1 = ;
if ((board[y][x] == ) && (mark[y][x] == ))t2 = ;
if ((x == end_x)&&(y == end_y)&&board[y][x]==) t3 = ;
if (t1 && (t2 || t3))
{
mark[y][x] = ;
if (f == i) Search(x, y, end_x, end_y, step, i);
else Search(x, y, end_x, end_y, step + , i);
mark[y][x] = ;
}
} }
int main() {
int kase = ;
while(cin >> w >> h) { memset(board, , sizeof(board));
memset(mark, , sizeof(mark));
if (w == && h == )break;
printf("Board #%d:\n", ++kase); string s;
getchar();
_for(i, , h) { getline(cin, s);
_for(j, , w) {
if (s[j] == 'X')board[i+][j+] = ;//外围加一圈
}
}
int x, y, xx, yy,cnt=;
while (cin >> x >> y >> xx >> yy) {
if (x == && y == && xx == && yy == ) break;
mn = ;
Search(x, y, xx, yy,,-);
if (mn < )printf("Pair %d: %d segments.\n", ++cnt, mn);
else printf("Pair %d: impossible.\n", ++cnt); }
cout << endl;
} system("pause");
}

POJ - 1101 The Game dfs的更多相关文章

  1. POJ 1321 棋盘问题 --- DFS

    POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...

  2. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  3. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  4. 搜索 + 剪枝 --- POJ 1101 : Sticks

    Sticks Problem's Link:   http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...

  5. 开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)

    博客第一篇写在11月1号,果然die die die die die alone~ 一道不太难的题,白书里被放到排序这一节,半年前用快排A过一次,但是现在做的时候发现可以用字典树加深搜,于是乐呵呵的开 ...

  6. poj 1416 Shredding Company( dfs )

    我的dfs真的好虚啊……,又是看的别人的博客做的 题目== 题目:http://poj.org/problem?id=1416 题意:给你两个数n,m;n表示最大数,m则是需要切割的数. 切割m,使得 ...

  7. poj 1129 Channel Allocation ( dfs )

    题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...

  8. OpenJudge 2802 小游戏 / Poj 1101 The Game

    1.链接地址: http://bailian.openjudge.cn/practice/2802 http://poj.org/problem?id=1101 2.题目: 总时间限制: 1000ms ...

  9. poj 3373 Changing Digits (DFS + 记忆化剪枝+鸽巢原理思想)

    http://poj.org/problem?id=3373 Changing Digits Time Limit: 3000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. 5 -- Hibernate的基本用法 --2 2 Hibernate的数据库操作

    在所有的ORM框架中有一个非常重要的媒介 : PO(持久化对象:Persistent Object).持久化对象的作用是完成持久化操作,简单地说,通过该对象可对数据执行增.删.改的操作 ------ ...

  2. 8 -- 深入使用Spring -- 1...4 属性占位符配置器

    8.1.4 属性占位符配置器 PropertyPlaceholderConfigurer 是一个容器后处理器,负责读取Properties属性文件里的属性值,并将这些属性值设置成Spring配置文件的 ...

  3. Quatz入门

    Demo SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory(); Scheduler sched = sched ...

  4. FFMPEG AVRational

    FFMPEG的很多结构中有AVRational time_base;这样的一个成员,它是AVRational结构的 typedef struct AVRational{ int num; ///< ...

  5. SaltStack 批量管理任务计划

    这里演示如何使用 salt-master 对多台 salt-minion 批量添加任务计划,步骤如下: [root@localhost ~]$ cat /srv/salt/top.sls # 先定义入 ...

  6. Vim 的 Python 编辑器详细配置过程 (Based on Ubuntu 12.04 LTS)

    为什么要用vim编辑py文件? 因为在Linux命令行中,缺少图形界面的IDE,vim是最佳的文本编辑器,而为了更好的编辑py文本,所以配置vim. 1. 安装完整版vim vi和vim的区别? 在L ...

  7. C#实现新建文件并写入内容

    using System; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(stri ...

  8. [iOS] 在 ios10 中使用 imessage

    本文转载至 http://www.tuicool.com/articles/zIFvQn7   原文  http://www.bourbonz.cn/在ios10中使用imessage/ 主题 iOS ...

  9. css3整理--gradient

    gradient语法: -moz-linear-gradient( [<point> || <angle>,]? <stop>, <stop> [, & ...

  10. storm事务

    1. storm 事务 对于容错机制,Storm通过一个系统级别的组件acker,结合xor校验机制判断一个msg是否发送成功,进而spout可以重发该msg,保证一个msg在出错的情况下至少被重发一 ...