文章目录
罪过,好长时间没刷题了,宽搜也很难,先把一些贴上来,回头来填坑
8.2 BFS处理
BFS概念
BFS例一矩阵“块”
//BFS例一矩阵"块"
//当前位置(x,y)相邻位置为(x,y+1)、(x,y-1)、(x+1,y)、(x-1,y)
//则设置增量数组X[]、Y[]——可应用于迷宫问题、八皇后问题等
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn = 110;
struct node{
int x,y;
}Node;
int n,m;//矩阵大小为n*m
int matrix[maxn][maxn];//记录矩阵信息
bool isinq[maxn][maxn] = {false};//记录位置(x,y)是否入队
int X[4] = {0,0,1,-1};//增量数组
int Y[4] = {1,-1,0,0};
bool judge(int x,int y){//判断(x,y)是否需要访问
//越界返回false
if(x >= n || x < 0|| y >= m || y < 0) return false;
//当前位置为0,或(x,y)已经入队,返回false
if(matrix[x][y] == 0|| isinq[x][y] == true) return false;
//以上都不满足,返回true
return true;
}
//BFS函数访问位置(x,y)所在的块,将该块所有的"1"的isinq都设置为true
void BFS(int x,int y){
queue<node> Q;//STL定义队列
Node.x = x,Node.y = y;//当前结点的坐标为(x,y)
Q.push(Node);//将结点Node入队
isinq[x][y] = true;//设置(x,y)已经入队
while(!Q.empty()){
node temp = Q.front();//取出队首元素
Q.pop();//队首元素出队
for(int i=0;i<4;i++){//循环4次,得到4个相邻位置
int newX = temp.x + X[i];
int newY = temp.y + Y[i];
if(judge(newX,newY)){//如果新位置(newX,newY)需要访问
//设置Node的坐标为(newX,newY)
Node.x = newX,Node.y = newY;
Q.push(Node);//将结点Node加入队列
isinq[newX][newY] = true;//设置位置(newX,newY)已经入队
}
}
}
}
/*
6 7
0 1 1 1 0 0 1
0 0 1 0 0 0 0
0 0 0 0 1 0 0
0 0 0 1 1 1 0
1 1 1 0 1 0 0
1 1 1 1 0 0 0
*/
int main(){
cin>>n>>m;
for(int x=0;x < n;x++){
for(int y=0;y < m;y++){
cin>>matrix[x][y];
}
}
int ans = 0;
for(int x=0;x < n;x++){
for(int y=0;y < m;y++){
if(matrix[x][y] == 1 && isinq[x][y] == false){
ans++;//块数加1
BFS(x,y);//访问整个块,将该块所有"1”的isinq都标记为true
}
}
}
cout<<ans<<endl;
return 0;
}
BFS例二走迷宫最小步数
//BFS例二走迷宫最小步数
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 110;
struct node{
int x,y;//位置(x,y)
int step;//step为从起点S到达该位置的最少步数(即层数)
}S,T,Node;//S为起点,T为终点,Node为临时结点
int n,m;//n为行,m为列
char maze[maxn][maxn];//迷宫信息
bool isinq[maxn][maxn] = {false};//记录位置(x,y)是否已入队
int X[4] = {0,0,1,-1};int Y[4] = {1,-1,0,0};//增量数组
//检测位置(x,y)是否有效
bool test(int x,int y){
if(x >= n || x < 0 || y >= m|| y < 0) return false;//越界返回
if(maze[x][y] == '*') return false;//墙壁不能通行
if(isinq[x][y] == true) return false;//已经入队
return true;//有效位置
}
int BFS(){
queue<node> q;//定义队列
q.push(S);//起点S入队
while(!q.empty()){
node temp = q.front();//取队首元素
q.pop();//对手噢元素出队
if(temp.x == T.x && temp.y == T.y){//到达终点
return temp.step;//直接返回最少步数
}
for(int i=0;i < 4;i++){//循环4次访问当前结点4个相邻位置
int newX = temp.x + X[i];
int newY = temp.y + Y[i];
if(test(newX,newY)){//位置(newX,newY)有效
//设置Node的坐标为(newX,newY)
Node.x = newX,Node.y = newY;
Node.step = temp.step + 1;//Node层数为temp层数加1
q.push(Node);//将结点Node加入队列
isinq[newX][newY] = true;//设置位置(newX,newY)已入队
}
}
}
return -1;//无法到达终点T时返回-1
}
/*
5 5
.....
.*.*.
.*S8.
.***.
...T*
2 2 4 3
*/
int main(){
cin>>n>>m;
for(int i = 0;i < n;i++){
getchar();//过滤掉每行后面的换行符
for(int j=0;j < m;j++){
maze[i][j] = getchar();
}
maze[i][m+1] = '\0';
}
cin>>S.x>>S.y>>T.x>>T.y;//输入起始点和终点坐标
S.step = 0;
cout<<BFS()<<endl;
return 0;
}
Codeup习题
Contest100000609 - 《算法笔记》8.2小节——搜索专题->广度优先搜索(BFS)链接:http://codeup.cn/contest.php?cid=100000609
2078-ProblemA-Jugs
链接: http://codeup.cn/problem.php?cid=100000609&pid=0
倒水问题的经典形式是这样的:
“假设有一个池塘,里面有无穷多的水。现有2个空水壶,容积分别为5升和6升。问题是如何只用这2个水壶从池塘里取得3升的水。”
当然题外是有一些合理的限制的,比如从池塘里灌水的时候,不管壶里是不是已经有水了,壶一定要灌满,不能和另一个壶里的水位比照一下“毛估估”(我们可以假设壶是不透明的,而且形状也不同);同样的,如果要把水从壶里倒进池塘里,一定要都倒光;如果要把水从一个壶里倒进另一个壶里,也要都倒光,除非在倒的过程中另一个壶已经满了;倒水的时候水没有损失(蒸发溢出什么的)等等等等。
事实上,要解决上面这题,你只要用两个壶中的其中一个从池塘里灌水,不断地倒到另一个壶里,当第二个壶满了的时候,把其中的水倒回池塘里,反复几次,就得到答案了。以5升壶(A)灌6升壶(B)为例:
A B
0 0
5 0 A→B
0 5
5 5 A→B
4 6
4 0 A→B
0 4
5 4 A→B
3 6
现在我们问,如果是多于2只壶的情况怎么办(这样一来就不能用上面的循环倒水法了)?如何在倒水之前就知道靠这些壶是一定能(或一定不能)倒出若干升水来的?试举数例:
1) 两个壶:65升和78升,倒38升和39升。
2) 三个壶:6升,10升和45升,倒31升。
我们可以看到,在1)中,65=5×13,78=6×13,而39=3×13。所以如果把13升水看作一个单位的话(原题中的“升”是没有什么重要意义的,你可以把它换成任何容积单位,毫升,加仑——或者“13升”),这题和最初的题目是一样的。而38升呢?显然是不可能的,它不是13的倍数,而65升和78升的壶怎么也只能倒出13升的倍数来。也可以这样理解:这相当于在原题中要求用5升和6升的壶倒出38/39升来。
那么2)呢?你会发现,只用任何其中两个壶是倒不出31升水的,理由就是上面所说的,(6,10)=2,(6,45)=3,(10,45)=5,(这里(a,b)是a和b的最大公约数),而2,3,5均不整除31。可是用三个壶就可以倒出31升:用10升壶四次,6升壶一次灌45升壶,得到1升水,然后灌满10升壶三次得30升水,加起来为31升。
一般地我们有“灌水定理”:
“如果有n个壶容积分别为A1,A2,……,An(Ai均为大于0的整数)设w为另一大于0的整数。则用此n个壶可倒出w升水的充要条件为:
1) w小于等于A1+A2+…+An;
2) w可被(A1,A2,…,An)(这n个数的最大公约数)整除。”
这两个条件都显然是必要条件,如果1)不被满足的话,你连放这么多水的地方都没有。2)的道理和上面两个壶的情况完全一样,因为在任何步骤中,任何壶中永远只有(A1,A2,…,An)的倍数的水。
现在我们来看一下充分性。在中学里我们学过,如果两个整数a和b互素的话,那么存在两个整数u和v,使得ua+vb=1。证明的方法很简单:在对a和b做欧几里德辗转相除时,所有中间的结果,包括最后得到的结果显然都有ua+vb的形式(比如第一步,假设a小于b,记a除b的结果为s,余数为t,即b=sa+t,则t=(-s)a+b,即u=-s,v=1)。而两个数互素意味着欧几里德辗转相除法的最后一步的结果是1,所以1也可以记作ua+vb的形式。稍微推广一点,如果(a,b)=c,那么存在u和v使得ua+vb=c(两边都除以c就回到原来的命题)。
再推广一点,如果A1,A2,……,An是n个整数,(A1,A2,…,An)=s,那么存在整数U1,U2,……,Un,使得
U1A1 + U2A2 + … + UnAn = s. ()
在代数学上称此结果为“整数环是主理想环”。这也不难证,只要看到
(A1,A2,A3,A4,…,An) = ((((A1,A2),A3),A4),…,An).
也就是说,可以反复应用上一段中的公式:比如三个数a,b,c,它们的最大公约数是d。假设(a,b)=e,那么(e,c)=((a,b),c)=d。现在有u1,u2使得u1a+u2b=e,又有v1,v2使得v1e+v2c=d,那么
(v1u1)a+(v1u2)b+(v2)c=d.
好,让我们回头看“灌水定理”。w是(A1,A2,…,An)的倍数,根据上节的公式(),两边乘以这个倍数,我们就有整数V1,V2,……,Vn使得 V1A1 + V2A2 + … + VnAn = w.注意到Vi是有正有负的。
这就说明,只要分别把A1,A2,……,An壶,灌上V1,V2,……,Vn次(如果Vi是负的话,“灌上Vi次”要理解成“倒空-Vi次”),就可以得到w升水了。具体操作上,先求出各Vi,然后先往Vi是正数的壶里灌水,灌1次就把Vi减1。再把这些水到进Vi是负数的壶里,等某个壶灌满了,就把它倒空,然后给这个负的Vi加1,壶之间倒来倒去不变更各Vi的值。要注意的是要从池塘里灌水,一定要用空壶灌,要倒进池塘里的水,一定要是整壶的。这样一直到所有Vi都是0为止。
会不会发生卡住了,既不能灌水又不能倒掉的情况?不会的。如果有Vi仍旧是负数,而Ai壶却没满:那么如果有其它Vi是正的壶里有水的话,就都倒给它;如果有其它Vi是正的壶里没水,那么就拿那个壶打水来灌(别忘了给打水的壶的Vi减1);如果根本没有任何Vi是正的壶了——这是不可能的,这意味着w是负的。有Vi仍旧是正数,而Ai壶却没满的情况和这类似,你会发现你要用到定理中的条件1)。
这样“灌水定理”彻底得证。当然,实际解题当中如果壶的数目和容积都比较大的话,手工来找()中的各Ui比较困难,不过可以写个程序,连倒水的步骤都算出来。最后要指出的一点是,()中的Ui不是唯一的,所以倒水的方式也不是唯一的。
4054-ProblemB-DFS or BFS?
链接: http://codeup.cn/problem.php?cid=100000609&pid=1
//## 4054-ProblemB-DFS or BFS?
#include <iostream>
#include <map>
using namespace std;
struct stone{
int x,y;
}s[100];//存储石头位置
map<string,int> mp;
int num,dx[9] = {-1,-1,-1,0,0,0,1,1,1},dy[9]={-1,0,1,-1,0,1,-1,0,1};
bool flag;//判断能否到达出口
void DFS(int x,int y){
if(x==0 && y==7){//DFS出口,若到达终点
flag = true;
return;
}
for(int k = 0;k < num;k++){//若到达的位置是石头
if(x == s[k].x && y == s[k].y)
return;
}
for(int i = 0;i < 9;i++){
if(x + dx[i] >= 0 && x+dx[i] < 8 && y+dy[i] >=0 && y+dy[i] < 8){
bool ok = false;
for(int k=0;k < num;k++){//若移动到的位置有石头则continue
if(x + dx[i] == s[k].x && y+dy[i] == s[k].y){
ok = true;
}
}
if(ok) continue;
string cf;//重复
for(int k = 0;k < num;k++){
if(s[k].x >= 0 && s[k].x<8 && s[k].y >= 0 && s[k].y < 8){
cf += '0' + k;
cf += '0' + s[k].x;
cf += '0' + s[k].y;
}
}
cf += '0' + x + dx[i];
cf += '0' + y + dy[i];
if(mp.find(cf) != mp.end()) continue;//用map去重
else mp[cf]++;
for(int k = 0;k < num;k++){//使石头下落一层
s[k].x++;
}
DFS(x + dx[i],y + dy[i]);
for(int k = 0;k < num;k++){//返回DFS上一层状态
s[k].x--;
}
}
}
}
int main(){
int n;
cin>>n;
for(int t = 1;t <= n;t++){
char c;
num = 0,flag = false;
mp.clear();
for(int i = 0;i < 8;i++){
for(int j = 0;j < 8;j++){
cin >> c;
if(c == 'S'){
s[num].x = i;
s[num++].y = j;
}
}
}
DFS(7,0);
if(flag) cout<<"Case #"<<t<<": Yes"<<endl;
else cout<<"Case #"<<t<<": No"<<endl;
}
}
5997-ProblemC-【宽搜入门】8数码难题
链接: http://codeup.cn/problem.php?cid=100000609&pid=2
//5997-ProblemC-【宽搜入门】8数码难题
//7种方法求解八数码问题:https://blog.csdn.net/u012283461/article/details/79078653
#include<cstdio>
#include<cstring>
#include<algorithm>
#include <map>
#include<queue>
#include <iostream>
#include<stack>
using namespace std;
int start,termination,pos;
struct node{
int num,step,pos;//状态、步数、0的下标
node(int n,int s,int p){
num = n,step = s,pos = p;
}
};
int oper(char str[],int p,int flag,bool operate){
if(flag == 0 && operate || flag == 1 && !operate){//up
if(p < 3) return -1;
swap(str[p],str[p - 3]);
return p - 3;
}
else if(flag == 1 && operate || flag == 0 && !operate){//down
if(p >= 6) return -1;
swap(str[p],str[p+3]);
return p+3;
}
else if(flag == 2 && operate || flag == 3 && !operate){//left
if(p % 3 ==0) return -1;
swap(str[p],str[p - 1]);
return p-1;
}
else{//right
if(p % 3 == 2) return -1;
swap(str[p],str[p+1]);
return p+1;
}
}
queue<node> q;
map<int,bool> mp;
void BFS(){
char temp[10];
int temp1;
node s(start,1,pos);
q.push(s);
mp[start] = true;
while(!q.empty()){
s = q.front();
q.pop();
if(s.num == termination){
cout<<s.step<<endl;
return;
}
sprintf(temp,"%d",s.num);
if(s.num < 100000000){//注意第一个数字为0时转换
for(int i = 8;i > 0;i--){
temp[i] = temp[i-1];
}
temp[0] = '0';
}
for(int i=0;i<4;i++){
int d = oper(temp,s.pos,i,true);
if(d == -1) continue;
sscanf(temp,"%d",&temp1);//注意sscanf区别于scanf
//sscanf与scanf类似,都是用于输入的,只是后者以键盘(stdin)为输入源,前者以固定字符串为输入源。
if(mp.count(temp1) == 0){
node r(temp1,s.step+1,d);
q.push(r);
mp[temp1] = true;
}
oper(temp,d,i,false);
}
}
}
int main(){
int n;
while(cin>>n){
if(n == 0) pos=0;
start = n,termination = 0;
for(int i = 1;i< 9;i++){
cin>>n;
start = start*10 + n;
if(n == 0) pos = i;
}
for(int i = 0;i < 9;i++){
cin>>n;
termination = termination * 10 + n;
}
BFS();
}
return 0;
}
5998-ProblemD-【宽搜入门】魔板
链接: http://codeup.cn/problem.php?cid=100000609&pid=3
5999-ProblemE-【宽搜入门】巧妙取量
链接: http://codeup.cn/problem.php?cid=100000609&pid=4
BFS小结
BFS与DFS采取相反的策略,即尽可能地广撒网,把当前层次所有结点都访问后再访问下一层的结点,采用队列数据结构,类似于树的层次遍历