题意:给你一个地图,上面有一些‘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. iOS iTuns Connect官方配置教程

    iTunes Connect 开发者指南 (iTunes Connect Developer Guide): https://developer.apple.com/library/ios/docum ...

  2. mysql 行转列 (case when的使用)

    现有 table1 如下 table2 如下 需要统计:各个部门每个月的绩效总和 实现如下:

  3. 利用shell脚本自动获取awr报表

    观察Oracle数据库性能,oracle自带的awr功能为我们提供了一个近乎完美的解决方案,通过awr特性我们可以随时从数据库提取awr报告.通过报告可以了解一个系统的整个运行情况,生成的报告包括多个 ...

  4. 【RF库XML测试】Get Element

    Name:Get ElementSource:XML <test library>Arguments:[ source | xpath=. ]Returns an element in t ...

  5. Ansible常用模块使用

    Ansible官方提供了非常多的模块,还有若干第三方模块,我们也可以自己编写模块. Ansible对远程服务器的操作实际是通过模块完成的,先将模块拷贝到远程服务器,完成操作后,然后在远程服务器上删除该 ...

  6. 细说php的异常和错误处理机制

    再谈php错误与异常处理 讲的非常好 w3school php异常处理机制 php错误异常处理详解 注: 关注set_error_handler() set_exception_handler() r ...

  7. 非static成员函数通过类名::来调用,空指针调用成员方法不出错!

    首先来看这一段代码: #include <iostream> using namespace std; class A{ public: int k; void p1(){ cout< ...

  8. free -m 下的含义

    如下显示free是显示的当前内存的使用,-m的意思是M字节来显示内容.我们来一起看看. $ free -mtotal used free shared buffers cachedMem: 1002 ...

  9. 【laravel5.6】 Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

    在进行数据迁移时候报错: 特殊字段太长报错, php artisan migrate 现在utf8mb4包括存储emojis支持.如果你运行MySQL v5.7.7或者更高版本,则不需要做任何事情. ...

  10. 【Linux】使用 telnet 提示 Escape character is '^]'的意义

    在linux/unix下使用telnet hostname port连接上主机后会提示Escape character is '^]' 这个提示的意思是按Ctrl + ] 会呼出telnet的命令行, ...