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 ...
随机推荐
- flexbox父盒子flex-direction属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python3.5 中Django框架连接mysql
ps:mysqldb目前还不支持3.0python唉,最近赶了个新潮,用起了Python3.4跟Django1.6,数据库依然是互联网企业常见的MySql.悲催的是在Python2.7时代连接MySq ...
- 【代码审计】XIAOCMS_存在任意文件删除漏洞分析
0x00 环境准备 XIAOCMS官网: http://www.xiaocms.com/ 网站源码版本:XiaoCms (发布时间:2014-12-29) 程序源码下载:http://www.xi ...
- Android开发-- findViewById()方法得到空指针
如果想通过调用findViewById()方法获取到相应的控件,必须要求当前Activity的layout通过setContentView. 如果你通过其他方法添加了一个layout,如需获取这个la ...
- urllib 基础模块
(1) urllib.request:最基本的HTTP请求模块,用来模拟发送请求,就像在浏览器里输入网址然后回车一样(2) urllib.error:异常处理模块,如果出现请求错误,我们可以捕获这些异 ...
- Dictionary的应用
在C#中,Dictionary提供快速的基于兼职的元素查找.他的结构是这样的:Dictionary<[key], [value]> ,当你有很多元素的时候可以使用它.它包含在System. ...
- Netty《一》
作者:郭无心链接:https://www.zhihu.com/question/24322387/answer/78947405来源:知乎著作权归作者所有,转载请联系作者获得授权. Netty是什么? ...
- 【分布式系列之ActiveMq】ActiveMq入门示例
前言 github地址:https://github.com/AndyFlower/web-back/tree/master/ActiveMq01 下载ActiveMQ :http://activem ...
- PCL—低层次视觉—关键点检测(iss&Trajkovic)
关键点检测往往需要和特征提取联合在一起,关键点检测的一个重要性质就是旋转不变性,也就是说,物体旋转后还能够检测出对应的关键点.不过说实话我觉的这个要求对机器人视觉来说是比较鸡肋的.因为机器人采集到的三 ...
- jQuery的parseHTML方法
// data html字符串 // context 如果指定了context,则碎片将在此范围内被创建,默认是document // keepScripts 如果是true,则将有scr ...