hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010
这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧。
但是这题要过除了细心外,还需要强力的剪枝。
奇偶性剪枝:参考 http://www.cppblog.com/Geek/archive/2010/04/26/113615.html
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
char map[][];
int n,m,t,di,dj;
bool escape;
int dir[][]={{,-},{,},{,},{-,}};
void dfs(int si,int sj,int cnt)
{
if(cnt>) return;
if(escape) return;
if(si>n||sj>m||si<=||sj<=) return;
if(cnt==t&&si==di&&sj==dj) escape=;
if(escape) return;
if(cnt>=t) return;
int i,temp;
temp=(t-cnt)-abs(si-di)-abs(sj-dj);
if(temp<||temp&) return;
for(i=;i<;i++){
if(map[si+dir[i][]][sj+dir[i][]]!='X')
{
map[si+dir[i][]][sj+dir[i][]]='X';
dfs(si+dir[i][],sj+dir[i][],cnt+);
map[si+dir[i][]][sj+dir[i][]]='.';
}
}
return;
}
int main()
{
int i,j,si,sj;
while(cin>>n>>m>>t)
{
if(n==&&m==&&t==) break;
int wall=;
for(i=;i<=n;i++)
for(j=;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=;
map[si][sj]='X';
dfs(si,sj,);
if(escape) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}
http://acm.hdu.edu.cn/showproblem.php?pid=1015
给定一个字符串和一个数n,然后再字符串中找出5个字符,满足题目中的等式并且字典序最大。
输入之后先把字符串从大到小排序,然后搜索即可。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; bool cmp(char a,char b)
{
return a>b;
} int k,j,flag,vis[];
char s[],ss[],res[]; bool judge(int v,int w,int x,int y,int z)
{
if(v-w*w+x*x*x-y*y*y*y+z*z*z*z*z==k)
return ;
return ;
} void dfs(int x)
{
if(flag) return;
int i;
if(x==)
{
if(judge(ss[]-,ss[]-,ss[]-,ss[]-,ss[]-)) {flag=;strcpy(res,ss);}
return;
}
int l=strlen(s);
for(i=;i<l;i++)
{
if(!vis[i])
{
vis[i]=;
ss[x]=s[i];
dfs(x+);
vis[i]=;
}
}
} int main()
{
int i;
//freopen("a.txt","r",stdin);
while(scanf("%d %s",&k,s)!=EOF&&k!=&&strcmp(s,"END")!=)
{
flag=;
sort(s,s+strlen(s),cmp);
memset(vis,,sizeof(vis));
dfs();
if(flag)
{
printf("%s\n",res);
}
else printf("no solution\n");
}
return ;
}
hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)的更多相关文章
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- 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 (奇偶性剪枝)
题意:有一副二维地图'S'为起点,'D'为终点,'.'是可以行走的,'X'是不能行走的.问能否只走T步从S走到D? 题解:最容易想到的就是DFS暴力搜索,,但是会超时...=_=... 所以,,要有其 ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...
- HDU 1010 Tempter of the Bone(深度+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次. 首先先来介绍一下奇偶性 ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- (step4.3.1) hdu 1010(Tempter of the Bone——DFS)
题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...
- HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)
需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...
随机推荐
- 单点登陆CAS安装过程中可能遇到的问题
可能遇到的问题: 错误: java.security.cert.CertificateException: No name matching localhost found 原因: keystore里 ...
- Navicat Premium 11.0.19中文破解版 安装
一.navicat-premium简介 它是一款可连接多种数据库的软件,具体参见官网介绍:http://www.navicat.com.cn/products/navicat-premium 二.下载 ...
- bzoj 3170 manhattan距离
首先将坐标系顺时针旋转45度,得到一个新的坐标系,这个坐标系 对应的坐标的manhattan距离就是原图中的距离,然后快排,利用前缀和 数组O(N)求所有的答案,然后找最小值就行了,总时间O(Nlog ...
- ZOJ 1111 Poker Hands
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1111 A poker hand consists of 5 ca ...
- Flash Attribute
参考:http://www.open-open.com/lib/view/open1397266120028.html 为解决POST/Forward/GET出现的重复提交数据问题,改用POST/Re ...
- PIX的使用
这几天pix的一个问题可坑死我了,之前用的时候有蓝色的链接,点过去就可以查看相应资源,后来都是黑色的了没有可以点的链接. 看文档翻来翻去也没进展,后来找到了, 先打开render那个窗口 这样even ...
- javascript实现数据结构与算法系列:线性表的静态单链表存储结构
有时可借用一维数组来描述线性链表,这就是线性表的静态单链表存储结构. 在静态链表中,数组的一个分量表示一个结点,同时用游标(cur)代替指针指示结点在数组中的相对位置.数组的第0分量可看成头结点,其指 ...
- ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)
ZOJ 2971 Give Me the Number 题目 ZOJ 2311 Inglish-Number Translator 题目 //两者题目差不多,细节有点点不一样,因为不是一起做的,所以处 ...
- POJ 2100
Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 4443 Accepted: 946 ...
- JavaScript文件应该放在网页的什么位置
JavaScript文件应该放在网页的什么位置 http://www.lihuai.net/qianduan/js/352.html 在<良好的JavaScript编程习惯>系列教程的第三 ...