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字段中这意味 ...
随机推荐
- Activiti入门 -- 轻松解读数据库
相关文章: <史上最权威的Activiti框架学习指南> <Activiti入门 --环境搭建和核心API简介> 在Activiti中,相对前身JBPM基础上又额外多了5张,框 ...
- linux ubuntu开启sshd
which ssh #查看文件 sudo apt-get install ssh #安装ssh cd /etc/init.d #切换目录 ls -l | grep ssh #执行启动脚本 sudo s ...
- 孤荷凌寒自学python第三十九天python 的线程锁Lock
孤荷凌寒自学python第三十九天python的线程锁Lock (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 当多个线程同时操作一个文件等需要同时操作某一对象的情况发生时,很有可能发生冲突, ...
- mysql中查询常用的关键字
最简单的查询: 1 select * from [where ] 1 select column1,column2....from [where] 这里需要注意的是where子句中条件过滤使用到的关键 ...
- hashcode和equals方法小记
在正确的逻辑下,两个对象的hashcode一样,不代表两个对象equals:两个对象equals,则hashcode一定一样 在HashSet集合中,是不允许有重复的元素的,那么,set怎么才知道元素 ...
- POJ 2987 Firing | 最大权闭合团
一个点带权的图,有一些指向关系,删掉一个点他指向的点也不能留下,问子图最大权值 题解: 这是最大权闭合团问题 闭合团:集合内所有点出边指向的点都在集合内 构图方法 1.S到权值为正的点,容量为权值 2 ...
- 2017 多校6 String
多校6 String(ac自动机) 题意: 给一本有\(n\)个单词的字典 \(q\)个查询 \(pref_i,suff_i\) 查询字典里有多少单词前缀匹配\(pref_i\),后缀同时匹配\(su ...
- hdu 1512
思路:用并查集即可,每次合并的时候将小的集合合并到大的集合上去.理论上的平均复杂度是n*lgn*lgn. #include<map> #include<queue> #incl ...
- 雅礼集训 Day1 T3 画作 解题报告
画作 题目描述 小\(\mathrm{G}\)的喜欢作画,尤其喜欢仅使用黑白两色作画. 画作可以抽象成一个\(r\times c\)大小的\(01\)矩阵.现在小\(\mathrm{G}\)构思好了他 ...
- ZCC loves cube(cube)
题目描述 调戏完了狗,ZCC开始玩起了积木.ZCC的面前有一块n*n的棋盘,他要用这些1*1*1的积木在棋盘上搭出一个宏伟的建筑.积木有三种颜色,ZCC认为一个建筑要被称为宏伟的应该满足能从正面看到的 ...