题意:给你一个地图,上面有一些‘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. spring mvc处理方法返回方式

    Model: package org.springframework.ui; import java.util.Collection; import java.util.Map; public int ...

  2. ubuntu13.04 搭建 Nginx + PHP + PHP-FPM + Mysql (lnmp) 环境

    Nginx 是一个轻量级,以占用系统资源少,运行效率而成为web服务器的后起之秀,国内现在很多大型网站都以使用nginx,包括腾讯.新浪等大型信息网站,还有淘宝网站使用的是nginx二次开发的web服 ...

  3. AcceptEx 以及 获取远程IP与port

    // 獲取本地以及遠程的IP和port setsockopt(clientfd, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (char *)&listenfd ...

  4. iptables相关操作以及简单理解端口和服务之间关系

    一般CentOS7默认安装的是firewall不是iptables 1.查看firewall状态 firewall-cmd --state 关闭后显示not running,开启后显示running ...

  5. Couldn’t load locSDK3

    在项目的libs目录下新建一个文件夹,命名为armeabi-v7a,然后将liblocSDK3.so复制一份到该文件夹 ok

  6. html主要笔记

    1.用title属性作为工具提示 2.链接到锚点 <a href="http://wickedlysmart.com/buzz#Coffee"> 3.<em> ...

  7. 用Python编写一个简单的Http Server

    用Python编写一个简单的Http Server Python内置了支持HTTP协议的模块,我们可以用来开发单机版功能较少的Web服务器.Python支持该功能的实现模块是BaseFTTPServe ...

  8. 更新npm至最新版本

    npm install npm@latest –g 或者@ 符号后面直接添加你想更新到的版本号

  9. 解决react-router 的activeClassName 首页重复匹配问题

    首先先看一下我的inedx.js入口文件: 这是未点击的时候index默认的是选中状态, 这是我点击其他的路由之后:,明显index路由重复了,仔细看了官方文档之后,发现其实Navlink也应该使用e ...

  10. codeforces水题100道 第八题 Codeforces Round #274 (Div. 2) A. Expression (math)

    题目链接:http://www.codeforces.com/problemset/problem/479/A题意:给你三个数a,b,c,使用+,*,()使得表达式的值最大.C++代码: #inclu ...