Tempter of the Bone------剪枝
看了好多别人的 代码,最终还是 感觉 这种代码的风格适合我 下面附上代码
/* 首先 应该充满信心! 先写出来 自己的程序 然后慢慢改 ,
如果是 答题思路错误的话 借鉴别人的 代码 再写
*/
/*没有剪枝 时间超限*/
#include<stdio.h>
#include<string.h>
char a[][];
int visited[][];
int s[][]={-,,,,,-,,};
int n,m,t,bx,by,cx,cy,step=,mark;
void DFS(int y,int x,int step) // 传输 进去现在 的 位置 并且传出过去 已有的 步数
{
if(y==cy&&x==cx&&step==t)
{
mark=;
}
visited[y][x]=;
for(int i=;i<;i++)
{
if(a[y+s[i][]][x+s[i][]]!='X'&&(y+s[i][])>=&&(y+s[i][])<n&&(x+s[i][])>=&&(x+s[i][])<m&&!visited[y+s[i][]][x+s[i][]]) // 不超界 并且 不是墙 而且没有访问
{
step++;
DFS(y+s[i][],x+s[i][],step);
step--;
} }
visited[y][x]=;
}
int main()
{
int i,j;
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
if(n==&&m==&&t==)
break
getchar();
memset(visited,,sizeof(visited));
for(mark=step=i=;i<n;i++)
{
for(j=;j<m;j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='S') // 找到进入的坐标
{
bx=j;
by=i;
}
if(a[i][j]=='D') // 找到出去的坐标
{
cx=j;
cy=i;
}
}
getchar();
}
DFS(by,bx,); // 传输 当前座标
if(mark!=)
printf("NO\n");
else
printf("YES\n");
}
return ;
}
上面的 没有剪枝 时间超限 下面开始 剪枝 .
/* 首先 应该充满信心! 先写出来 自己的程序 然后慢慢改 , 如果是 答题思路错误的话 借鉴别人的 代码 再写 */
/*
剪枝:
1: 奇偶剪枝:到终点的最短步数(就算需要绕路也没事要的奇偶) 和 到终点限定的步数 如果 奇偶性不同的话是不可能达到的 (绕路是同时多出偶数倍的步数) 1/2 的时间
2: 进去的时候 到 终点的最短路 如果大于 给定的步数 也不可能 . (也可能绕路绕超所以需要重复)
3: 可以走的格子 走完但是仍然还不够限定的步数 就不行了
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<map>
#include<cctype>
#include<limits.h>
#define leng 10
using namespace std;
char a[leng][leng];
int ex,ey,n,m,t,bx,mark,by,b[][]={,,,,,-,-,},visited[leng][leng];
void DFS(int y,int x,int step)
{
int d=t-step-abs(y-ey)-abs(x-ex); // 剩下 的步数 - 到终点的步数 小于 0 完蛋 .
if(t<step||mark==||d<||d%) // 这里三个 剪枝 . 上述公式 如果不是 偶数的话 , 就完蛋.
return;
if(a[y][x]=='D'&&t==step)
{
mark=;
}
visited[y][x]=;
for(int i=;i<;i++)
{
int tx=x+b[i][],ty=y+b[i][];
if(a[ty][tx]!='X'&&tx>=&&tx<m&&ty>=&&ty<n&&!visited[ty][tx]) // 不是 墙 不超界 没用过
{
DFS(ty,tx,step+);
}
}
visited[y][x]=;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t),!(n==m&&m==t&&t==))
{
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
scanf(" %c",&a[i][j]);
if(a[i][j]=='S')
{
bx=j;
by=i;
}
if(a[i][j]=='D')
{
ex=j;
ey=i;
}
}
}
memset(visited,,sizeof(visited));
mark=;
DFS(by,bx,);
if(mark==)
printf("NO\n");
else
printf("YES\n");
}
return ;
}
Tempter of the Bone------剪枝的更多相关文章
- hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- Tempter of the Bone(dfs+奇偶剪枝)题解
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1010:Tempter of the Bone(DFS+奇偶剪枝+回溯)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- Linux学习笔记(五) 账号管理
1.用户与组账号 用户账号:包括实际人员和逻辑性对象(例如应用程序执行特定工作的账号) 每一个用户账号包含一个唯一的用户 ID 和组 ID 标准用户是系统安装过程中自动创建的用户账号,其中除 root ...
- SQL Server数据库基础编程
转载,查看原文 Ø Go批处理语句 用于同时执行多个语句 Ø 使用.切换数据库 use master go Ø 创建.删除数据库 方法1. --判断是否存在该数据库,存在就删除 if (exist ...
- L2-014. 列车调度(带图详解)
L2-014. 列车调度 火车站的列车调度铁轨的结构如下图所示. Figure 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选 ...
- SpringBoot yaml的配置及使用
application.yml配置如下 person: lastName: hello age: boss: false birth: // maps: {k ...
- leetcode 19.删除链表的第n个节点
删除链表的第n个节点 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第 ...
- Leetcode 131.分割回文串
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...
- Leetcode 114.二叉树展开为链表
二叉树展开为链表 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 将其展开为: class Solution{ public: void flatten(TreeNode* root){ if ...
- New Barns
New Barns 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Farmer John notices that his cows tend to get into argument ...
- HDU 2604 矩阵快速幂
题目大意 给定长度为l的只有f,m两种字母 的序列,问不出现fff,fmf的序列个数有多少个 每次的下一个状态都与前一次状态的后两个字母有关 比如我令mm : 0 , mf : 1 , fm : 2 ...
- CTF常用在线工具总结
在线工具 MD5加密 MD5解密(推荐) MD5解密(推荐) MD5解密 escap加解密 维吉尼亚密码 SHA 对称加密AES DES\3DES RC4\Rabbit Quoted-print ...