POJ - 1101 The Game dfs
题意:给你一个地图,上面有一些‘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的更多相关文章
- POJ 1321 棋盘问题 --- DFS
POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- 搜索 + 剪枝 --- POJ 1101 : Sticks
Sticks Problem's Link: http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...
- 开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)
博客第一篇写在11月1号,果然die die die die die alone~ 一道不太难的题,白书里被放到排序这一节,半年前用快排A过一次,但是现在做的时候发现可以用字典树加深搜,于是乐呵呵的开 ...
- poj 1416 Shredding Company( dfs )
我的dfs真的好虚啊……,又是看的别人的博客做的 题目== 题目:http://poj.org/problem?id=1416 题意:给你两个数n,m;n表示最大数,m则是需要切割的数. 切割m,使得 ...
- poj 1129 Channel Allocation ( dfs )
题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...
- OpenJudge 2802 小游戏 / Poj 1101 The Game
1.链接地址: http://bailian.openjudge.cn/practice/2802 http://poj.org/problem?id=1101 2.题目: 总时间限制: 1000ms ...
- poj 3373 Changing Digits (DFS + 记忆化剪枝+鸽巢原理思想)
http://poj.org/problem?id=3373 Changing Digits Time Limit: 3000MS Memory Limit: 65536K Total Submi ...
随机推荐
- Spring @Lazy
@DependsOn用于强制初始化其他Bean.可以修饰Bean类或方法,使用该Annotation时可以指定一个字符串数组作为参数,每个数组元素对应于一个强制初始化的Bean. @DependsOn ...
- ios开发之--跳转到指定的TabBarViewController中的某一个VIewController
比较简单,也很实用,方法大同小异,仅做记录,方法的系统记录如下: [self dismissViewControllerAnimated:YES completion:^{ // 这是从一个模态出来的 ...
- YUV格式学习笔记
YUV与RGB表现图像的方法不同,其采用的是一个亮度信号加两个色差信号的方式来表现图像.其中UV表示的是CbCr,常见的YUV格式有:YUV4:2:0,YUV4:2:2,YUV4:1:1,YUV4:4 ...
- 设置开机自启动VirtualBox虚拟机系统
如果常用VirtualBox虚拟机系统的话,设置个随开机启动也是很方便的.不需要打开VirtualBox窗口,直接就是系统启动了. 又继续上网搜索学习了.(设置开机自启动VirtualBox虚拟机系统 ...
- Android文件系统编译出错记录
错误1: 注意:external/protobuf/java/src/main/java/com/google/protobuf/GeneratedMessageLite.java 使用了未经检查或不 ...
- 在mysql存储过程中拼接sql解决in的字段类型不匹配问题
一个朋友问我一个问题,他写了一个存储过程,并在存储过程调用了另外一个自定义的函数.该函数返回类型如'1,34,56'的字符串,并将该字符串作为存储过程的select的id条件. begin DECLA ...
- Python入门 学习笔记
十六进制:0x123 布尔运算:and, or, not 空值:None 注释:# raw字符串不需要转义:r'XXX' 多行字符:'''XXX''' 多行字符+raw字符串:r'''XXX''' U ...
- 通过设置P3P头来实现跨域访问COOKIE
通过设置P3P头来实现跨域访问COOKIE 实际工作中,类似这样的要求很多,比如说,我们有两个域名,我们想实现在一个域名登录后,能自动完成另一个域名的登录,也就是PASSPORT的功能. 我只写一个大 ...
- golang文件传输服务
续上篇,本篇介绍一个完整的golang文件传输服务器. 完整的代码可以看服务器,客户端 网络使用的框架如上篇介绍,这里就不再复述. 首先定义3个命令码: const ( request_file = ...
- python之SQLAlchemy ORM
前言: 这篇博客主要介绍下SQLAlchemy及基本操作,写完后有空做个堡垒机小项目.有兴趣可看下python之数据库(mysql)操作.下篇博客整理写篇关于Web框架和django基础~~ 一.OR ...