kuangbin系列【简单搜索】
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 36385 | Accepted: 17950 |
Description
Input
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
Sample Input
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output
2
1
【分析】:建议学习回溯的时候对N皇后还有N皇后的变式好好学习一下,类N皇后真是学习回溯非常好的例题。
如在第i行第j列,遇到'#'号。那么接下来的处理就有两种情况了。
第一种:把i,j放入到一个数组C中,然后继续向第i+1行进行搜索,直到找到m个位置或者到了棋盘的边界
另一种:不选择第i行第j列的位置,然后继续向第i+1行进行搜索,直到找到m个位置或者到了棋盘的边界 【代码】:
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <set>
#include <map>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <vector>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
#pragma comment(linker, "/STACK:102400000,102400000")
#define Abs(x) ((x^(x >> 31))-(x>>31))
#define Swap(a,b) (a^=b,b^=a,a^=b)
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define MOD 1000000007
#define max_ 505
#define maxn 200002 using namespace std; int n,m;
char s[][];//表示棋盘
int c[];//表示每一列有没有摆放过棋子
int tot,cnt; void dfs(int cur)//cur表示当前所在行
{
if(cnt == m)//cnt表示当前所摆放棋子数目
{
tot++;
return ;
} if(cur >= n)//超出搜索范围
return ; for(int j=;j<n;j++)
{
if(!c[j] && s[cur][j]=='#')//空白处并且还没有摆放棋子
{
c[j]=;
cnt++;
dfs(cur+);//搜索下一行
c[j]=;//标记清除
cnt--;
}
}
dfs(cur+);//如果当前行没有可以摆放的位置 或者cnt已经等于m 但是还没有搜索完整个棋盘 将要继续搜索下一行
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==-&&m==-) break;
memset(c,,sizeof(c));//将标记初始化为0
tot=cnt=;
for(int i=;i<n;i++)
{
scanf("%s",&s[i]);
}
dfs();
printf("%d\n",tot);
}
}
POJ - 2251 Dungeon Master 【三维dfs】
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
【题意】:给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径
移动方向可以是上,下,左,右,前,后,六个方向
每移动一次就耗费一分钟,要求输出最快的走出时间。
不同L层的地图,相同RC坐标处是连通的
【代码】:
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <set>
#include <map>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <vector>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
#pragma comment(linker, "/STACK:102400000,102400000")
#define Abs(x) ((x^(x >> 31))-(x>>31))
#define Swap(a,b) (a^=b,b^=a,a^=b)
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define MOD 1000000007
#define max_ 505
#define maxn 200002 using namespace std; int n,m,k,x,y,z,sx,sy,sz,ex,ey,ez;
char s[][][];//表示棋盘
int vis[][][];//表示每一列有没有摆放过棋子
int dir[][]={ {,,},{,,-},{-,,},{,,},{,-,},{,,} }; struct node
{
int x,y,z,step;
}; int check(int x,int y,int z)
{
if(x< || y< || z< || x>=k || y>=n || z>=m)
return ;
else if(s[x][y][z] == '#')
return ;
else if(vis[x][y][z])
return ;
return ;
} int bfs()
{
//初始化
node a,tmp; queue<node> q;
a.x = sx,a.y = sy,a.z = sz,a.step = ;
vis[sx][sy][sz]=; q.push(a); while(!q.empty())
{
a=q.front();
q.pop();
if(a.x==ex && a.y==ey && a.z==ez)
return a.step; for(int i=;i<;i++)
{
tmp = a;
tmp.x=a.x+dir[i][];
tmp.y=a.y+dir[i][];
tmp.z=a.z+dir[i][];
if(check(tmp.x,tmp.y,tmp.z))
continue;
vis[tmp.x][tmp.y][tmp.z]=;
tmp.step=a.step+;
q.push(tmp);
}
}
return ;
} int main()
{
int i,j,r;
while(scanf("%d%d%d",&k,&n,&m),n+m+k)
{
for(i = ; i<k; i++)
{
for(j = ; j<n; j++)
{
scanf("%s",s[i][j]);
for(r = ; r<m; r++)
{
if(s[i][j][r] == 'S')
{
sx = i,sy = j,sz = r;
}
else if(s[i][j][r] == 'E')
{
ex = i,ey = j,ez = r;
}
}
}
}
memset(vis,,sizeof(vis));
int ans;
ans = bfs();
if(ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
} return ;
}
poj 3278 【一维bfs】
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 71899 | Accepted: 22632 |
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
Source
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring> using namespace std;
#define maxn 100010 queue<int> q;
int n,k;
int vis[maxn];
int step[maxn];//步数数组装总步数 int bfs(int n,int k)
{
///////////////////////////
memset(vis,,sizeof(vis));
int now,nxt;
step[n]=;//初始化步数为0
vis[n]=;//标记最开始的节点被访问
q.push(n);//起始节点入队
/////////////////////////// while(!q.empty())
{
now=q.front();
q.pop(); //if(nxt==k) return step[nxt]; for(int i=;i<;i++) //遍历
{
if(i==) nxt=now+;
else if(i==) nxt=now-;
else if(i==) nxt=now*; //顺序无关 if(nxt<||nxt>maxn) continue;//越界 if(!vis[nxt]) //判重
{
vis[nxt]=;
step[nxt]=step[now]+;
q.push(nxt);
} if(nxt==k) return step[nxt];//找到 }
}
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
if(n>=k)
printf("%d\n",n-k);
else
printf("%d\n",bfs(n,k));
return ;
}
一维BFS
Find The Multiple POJ - 1426
Input
Output
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111
【题意】:输入一个整数,求大于等于这个整数的且满足条件的最小值 ,条件是这个整数能整出输入的整数,且这个整数只能包括0和1。
【分析】:可以BFS/DFS。这题搜索的方向有两个而且它的下界不好确定。所以可以用迭代加深搜索的技巧.用一个maxed控制搜索的下界。或者根据无符号整型确定深度最多为19.起点必须为1.
【代码】:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map> using namespace std;
#define LL long long
int n;
LL now;
void bfs(int ans)
{
queue<LL> q;
q.push(ans);
while(!q.empty())
{
now = q.front();
q.pop();
if(now%n==)
{
cout<<now<<endl;
return ;
}
q.push(now*);
q.push(now*+);
}
}
int main()
{
while(cin>>n,n)
{
bfs();
}
return ;
}
BFS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map> using namespace std;
#define ULL unsigned __int64
int f,n;
void dfs(ULL now,int s)
{
if(f) return;// 放在最前面
if(now%n==)
{
cout<<now<<endl;
f=;
return;
}
if(s==) return; //因为unsigned __int64的范围是-9223372036854775808~9223372036854775807(10^19)与0~18446744073709551615(10^20)
//为防止超出范围,循环深度应小于20
dfs(now*,s+); //当前数字有两种选择方案,即下一个数选1或选0
dfs(now*+,s+);
}
int main()
{
while(cin>>n,n)
{
f=;
dfs(,);//首位数字必须为1
}
}
DFS
#include<cstdio>
#include<algorithm>
using namespace std;
long long n,maxed;
long long s;
bool flag;
void dfs(long long i,int step)
{
if(flag) return;
if(step>=maxed) return;//当当前的递归深度达到上界后就return;
if(i%n==)
{
printf("%lld\n",i);
flag=true;
return;
}
dfs(i*,step+);
dfs(i*+,step+);
} int main()
{
while(~scanf("%d",&n)&&n)
{
flag=false;
for(maxed=;;++maxed)//让第一个搜不到就结束
{
if(flag)
break;
dfs(,);
}
}
}
DFS-迭代加深
kuangbin系列【简单搜索】的更多相关文章
- kuangbin专题简单搜索题目几道题目
1.POJ1321棋盘问题 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形 ...
- kuangbin专题——简单搜索
A - 棋盘问题 POJ - 1321 题意 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大 ...
- 简单搜索 kuangbin C D
C - Catch That Cow POJ - 3278 我心态崩了,现在来回顾很早之前写的简单搜索,好难啊,我怎么写不出来. 我开始把这个写成了dfs,还写搓了... 慢慢来吧. 这个题目很明显是 ...
- 和我一起打造个简单搜索之SpringDataElasticSearch入门
网上大多通过 java 操作 es 使用的都是 TransportClient,而介绍使用 SpringDataElasticSearch 的文章相对比较少,笔者也是摸索了许久,接下来本文介绍 Spr ...
- 和我一起打造个简单搜索之SpringDataElasticSearch关键词高亮
前面几篇文章详细讲解了 ElasticSearch 的搭建以及使用 SpringDataElasticSearch 来完成搜索查询,但是搜索一般都会有搜索关键字高亮的功能,今天我们把它给加上. 系列文 ...
- 和我一起打造个简单搜索之Logstash实时同步建立索引
用过 Solr 的朋友都知道,Solr 可以直接在配置文件中配置数据库连接从而完成索引的同步创建,但是 ElasticSearch 本身并不具备这样的功能,那如何建立索引呢?方法其实很多,可以使用 J ...
- 和我一起打造个简单搜索之IK分词以及拼音分词
elasticsearch 官方默认的分词插件,对中文分词效果不理想,它是把中文词语分成了一个一个的汉字.所以我们引入 es 插件 es-ik.同时为了提升用户体验,引入 es-pinyin 插件.本 ...
- 和我一起打造个简单搜索之ElasticSearch集群搭建
我们所常见的电商搜索如京东,搜索页面都会提供各种各样的筛选条件,比如品牌.尺寸.适用季节.价格区间等,同时提供排序,比如价格排序,信誉排序,销量排序等,方便了用户去找到自己心里理想的商品. 站内搜索对 ...
- 和我一起打造个简单搜索之ElasticSearch入门
本文简单介绍了使用 Rest 接口,对 es 进行操作,更深入的学习,可以参考文末部分. 环境 本文以及后续 es 系列文章都基于 5.5.3 这个版本的 elasticsearch ,这个版本比较稳 ...
- ElasticSearch 5学习(4)——简单搜索笔记
空搜索: GET /_search hits: total 总数 hits 前10条数据 hits 数组中的每个结果都包含_index._type和文档的_id字段,被加入到_source字段中这意味 ...
随机推荐
- react书写规范小记
1.对齐方式 //如果没有属性,在自闭和标签前添加一个空格: <Footer /> //如果可以放在一行,放在一行上即可: <Footer bar="bar" / ...
- Win10安装bash慢的解决方案
电脑的Win10系统经过最近一年大量软件的装装删删,感觉已经有问题了,而且也存在大量无法清理的垃圾,占用着宝贵的SSD空间... 重新做系统的想法已经有一段时间了,正好赶上Win10的大更新 Crea ...
- Pythontutor:可视化代码在内存的执行过程
http://www.pythontutor.com/visualize.html今天去问开发一个Python浅拷贝的问题,开发给了一个神器,可以可视化代码在内存的执行过程,一看即懂,太NB了!~真是 ...
- NGUI-为Popuplist的下拉选项添加删除功能
NGUI例子里的popuplist是这样的:,但有时我们希望下拉选项都有删除功能,也就是这样:,一种方法是改popuplist的源码,我想这个实现起来不难,但现在我想说的是用反射来实现此功能,以及其他 ...
- Android记事本09
昨天: Activity的数据传递. 今天: 从Activity中返回数据 请求码和返回码的作用 遇到的问题: 无.
- Linux 内核数据结构bitmap
#include <stdio.h> #include <stdlib.h> #define MAX_PRIO 10000 #define BITS_PER_LONG 32 # ...
- lombok 去除麻烦的实体类get和set,toString书写
首先在pom.xml中添加 <dependency> <groupId>org.projectlombok</groupId> <artifactId> ...
- nagios原理及配置详解
1.Nagios如何监控Linux机器 NRPE总共由两部分组成:(1).check_nrpe插件,运行在监控主机上.服务器端安装详见:(2).NRPE daemon,运行在远程的linux主机上(通 ...
- [BZOJ3196] 二逼平衡树 [权值线段树套位置平衡树]
题面 洛咕题面 思路 没错我就是要不走寻常路! 看看那些外层位置数据结构,必须二分的,$O(n\log^3 n)$的做法吧! 看看那些cdq分治/树状数组套线段树的,空间$O(n\log^2 n)$挤 ...
- [SDOI2015][bzoj3990] 序列 [搜索]
题面 传送门 思路 首先,这道题目有一个非常显然(但是我不会严格证明,只能意会一下)的结论:一个合法的操作序列中,任意两个操作是可以互换的 那么,这个结论加上本题极小的数据范围,为什么不搜索一下呢? ...