HDU1010 Temper of the Bone

2024-08-26 08:32
文章标签 hdu1010 temper bone

本文主要是介绍HDU1010 Temper of the Bone,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

刚刚上实验课,榨果汁。。。自觉学不到什么东西,就来到了机房,看看HDU上的题。一道搜索,其中包含了几个我从没想到过的剪枝方法,例如奇偶剪枝、路径剪枝还有在主函数中的一个剪枝,诸见代码与注释,虽然看起来没什么用的剪枝没准会遇到变态的测试数据导致最后的完蛋,所以只要能想到的剪枝就尽量写上去。
注:这题的代码来自标程。

/*Exe.Time    Exe.Memory  Code Len.   Language    Author62MS        1676K       2081 B      C++         int9
*/// PPT上的标程
#define LOCAL#include <iostream>
#include <cmath>
#include <fstream>using namespace std;// 迷宫地图
// X:墙壁,小狗不能进入
// S:小狗所处位置
// D:迷宫的门
// .:空格char map[9][9];
int n, m, t, di, dj;    // (di, dj):门的位置
bool escape;int dir[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};/*1 1 1 1 1 1 1 1 11 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 11 1 1 1 1 1 1 1 1*/void dfs(int si, int sj, int cnt) // 表示在si,sj要求在cnt秒达到门的位置
// cnt 是当前的时间而非上述的注释
{int i, temp;if (si > n || sj > m || si <= 0 || sj <= 0) return;if (si == di && sj == dj && cnt == t) {escape = 1;return;}// 此处奇偶剪枝和路径剪枝temp = (t - cnt) - abs(si - di) - abs(sj - dj);if (temp < 0 || temp & 1) return;for (i = 0; i < 4; i++) {if (map[si + dir[i][0]][sj + dir[i][1]] != 'X') {map[si + dir[i][0]][sj + dir[i][1]] = 'X';dfs(si + dir[i][0], sj + dir[i][1], cnt + 1);if (escape) return;// backtrackmap[si + dir[i][0]][sj + dir[i][1]] = '.';}}return;
}// 到此这个DFS函数已经颇有效率,如果在此main函数中不进行剪枝也是可以的int main() {
#ifdef LOCALfreopen("input.txt", "r", stdin);
#endifstd::ios::sync_with_stdio(false);int i, j, si, sj;while (cin >> n >> m >> t && n && m && t) {// 此处数据是将要对对某些破烂数据的剪枝,可以删除掉int wall = 0;// Inputfor (i = 1; i <= n; i++) {for (j = 1; j <= m; j++) {cin >> map[i][j];if (map[i][j] == 'S') { si = i; sj = j; }else if (map[i][j] == 'D') { di = i; dj = j; }// 对墙数量的剪枝else if (map[i][j] == 'X') wall++;}}if (n * m - wall <= t) {cout << "NO" << endl;continue;}escape = 0;// 上述剪枝完毕,估计也没多少数据会用到上面这个剪枝,对效率提升不大map[si][sj] = 'X';dfs(si, sj, 0);if (escape) cout << "YES" << endl;else cout << "NO" << endl;}return 0;}

同样是对于矩阵的搜索,这题的矩阵范围不大,但是测试组数貌似会很多。

这篇关于HDU1010 Temper of the Bone的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1108058

相关文章

hdu1010 奇偶剪枝

恰好t时间到达 import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.util.Arrays;import

HDU 1010 Tempter of the Bone (搜索)

OJ题目 : click here ~~ 大概题意 : 迷宫搜索。从起点到终点 ,不能回头 , 问能不能在恰好在T 时刻,准时到达终点。 本题充分体现了剪枝的重要性: 奇偶性剪枝: 可以把maze看成这样:  0 1 0 1 0 1  1 0 1 0 1 0  0 1 0 1 0 1  1 0 1 0 1 0  0 1 0 1 0 1  从为 0 的格子走一步,必然走向为 1 的格子

hdu1010矩形图的搜索

/*给出地图和步数,在有墙的图内从起点到终点,判断能否在输入的步数内走完*/ #include<iostream>#include<cstdio>#include<algorithm>using namespace std;int sx,sy,ex,ey;int n,m;char map[10][10];int flag;int d[4][2]={0,1,1,0,0,-1,-

HDU 2602 Bone Collector

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. 许多年前,在泰迪镇有这么一个骨骼收集家。 This man like to collect varies of bones , such as dog’s , cow’s , also he went to the gra

hdu---1010 Tempter of the Bone (经典DFS,注意剪枝)

/*经典的dfs 主要考虑剪枝否则会超时           HDU 1010  */ # include<iostream> # include<cstdio> # include<cmath> # include<cstdlib> # include<cstring> # include<string> using namespace std; char

Hdu 2639 Bone Collector II

求价值第K大的01背包问题,技巧是多加一维表示第k大时的价值,转移的时候用两个有序数列合并的方法不断更新第二维。 /********************* Author:fisty* Data:2014-11-6* HDU2639* *****************/#include <cstdio>#include <cstring>#include <algorithm>us

九度OJ-1461:Tempter of the bone

深度优先搜索+回溯法的题。关于路径存在性的问题,都考虑使用深度优先搜索+回溯法。 Debug记录: ①visit的时候忘记标记visited数组了导致超时 ②关于使用scanf()录入%c:cin>>x无论x是啥类型,'\n'  '\t'   ' '(空白字符)不会被提取进x,一定会留在输入流中,当其后的字符被提取时其被丢弃。而对于scanf(“%口”,&x),其他的与cin一样,唯独x

Bone Collector -01背包

题目描述 Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … The bone col

hdu1010_tempter_of_the_Bone(dfs减枝)

题目大意:在一个矩阵之中从S到D在规定的时间到达,不能走回头路。题目的连接hdu1010 解题思路:因为到达的时间是固定的,因此使用广度优先遍历是不行的,广度优先遍历只能找到由S到达D的最小时间而不能找到规定的时间。因此要使用深度优先遍历,而深度优先遍历要注意减枝来提高最终的效率。 设当前的坐标为i,j。那么由当前位置到终点 (ex,ey)的最短距离为以两个点为对角线的矩形的长和宽之和减2,或

HDU-2602 Bone Collector 01背包

Bone Collector Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he we