[kuangbin带你飞] | 专题一 | 简单搜索(持续更新中)

本专栏将持续更新…

知识点

参考文章:搜索

笔记:

DFS

题目

BFS

题目
题目

题目

A - 典型棋盘问题(DFS) - POJ 1321

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

Input

输入含有多组测试数据。 每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n
<= 8 , k <= n 当为-1 -1时表示输入结束。 随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, .
表示空白区域(数据保证不出现多余的空白行或者空白列)。

Output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

Sample Input

2 1
#.
.#
4 4
…#
…#.
.#…
#…
-1 -1

Sample Output

2 1

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
const int inf=0x3f3f3f3f;
const int maxn=1e4+5;
int a[maxn][maxn];  //记录棋盘每格是否可放 
int visj[maxn];
int n,k,num; 
void dfs(int kk,int ii,int jj)
{  //第kk个棋子放在坐标(ii,jj)处 
	if(kk==k)  //放完了
	{
		num++;  //该方案可行 计数 
		return;
	} 
	for(int i=ii+1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(a[i][j]&&!visj[j])  //该点可以放并且没放过
			{
				visj[j]=1;  //标记访问 
				dfs(kk+1,i,j);
			    visj[j]=0;  //回溯后重置为未访问状态 
			} 
		}
	 } 
}
int main()
{
	while(cin>>n>>k)
	{
		if(n==-1&&k==-1)  break;
		for(int i=1;i<=n;i++)
		{
			getchar();  //小细节别忘了 
			for(int j=1;j<=n;j++)
			{
				char ch;
				cin>>ch;
				if(ch=='.')  a[i][j]=0;
				if(ch=='#')  a[i][j]=1; 
			}
		}
	    num=0;
		memset(visj,0,sizeof(visj));
		dfs(0,0,0);
		cout<<num<<endl;
	}
    return 0;	
}

最最基础的板子题 以后要尽量做到闭眼打:)

B - 典型迷宫问题(BFS) - POJ 2251

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

题目

Sample Output

Escaped in 11 minute(s).
Trapped!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define ll long long
using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=1e4+5;
int l,r,c,sum,flag;
int stx,sty,stz,edx,edy,edz;
int direction[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
//六个方向的赋值 每两个一组表示一条坐标轴的进退情况
int vis[35][35][35];
char a[35][35][35];

struct node
{
	int x,y,z;
	int dis;
}; 

int judge(int x,int y,int z)  //判断是否可走
{
	if(x<0||y<0||z<0||x>=l||y>=r||z>=c)  return 0;
	if(vis[x][y][z]==1)  return 0;
	if(a[x][y][z]=='#')  return 0;
	return 1;
}

void bfs()
{
	node now,next;  //结构体和队列要在函数里面建
	queue<node>q;  //这样重置后不会影响下一组数据
	now.x=stx;  //起始点赋值
	now.y=sty;
	now.z=stz;
	now.dis=0;
	q.push(now);   //起始点推入队列
	vis[now.x][now.y][now.z]=1;  //标记访问
	while(!q.empty())
	{
		now=q.front();  //取出首元素
		q.pop();  //删去尾元素
		if(now.x==edx&&now.y==edy&&now.z==edz)
		{   //走到终点
			sum=now.dis;
			flag=1;
			return;
		}
		for(int i=0;i<6;i++)  //依次枚举六个方向的情况
		{    //更新新坐标
			int newx=now.x+direction[i][0];
			int newy=now.y+direction[i][1];
			int newz=now.z+direction[i][2];
			if(judge(newx,newy,newz))
			{
				vis[newx][newy][newz]=1;  //标记访问
				//更新坐标和距离
				next.x=newx;
				next.y=newy;
				next.z=newz;
				next.dis=now.dis+1;
				q.push(next);  //把邻居符合条件的都推进队列
			}
			else continue;
		}
	}
}

int main()
{
	while(cin>>l>>r>>c)
	{
		if(l==0&&r==0&&c==0)  break;
		getchar();
		memset(vis,0,sizeof(vis));
		for(int i=0;i<l;i++)
		{
			for(int j=0;j<r;j++)
			{
				for(int k=0;k<c;k++)
				{
					cin>>a[i][j][k];
					if(a[i][j][k]=='S')  //记录起始点
					{
						stx=i;
						sty=j;
						stz=k;
					}
					if(a[i][j][k]=='E')  //记录结束点
					{
						edx=i;
						edy=j;
						edz=k;
					}
				}
			}
		}
		sum=0;  //初始化
		flag=0;
		bfs();
		if(flag)  cout<<"Escaped in "<<sum<<" minute(s)."<<endl;
		else  cout<<"Trapped!"<<endl;
	}
    return 0;	
}

C - POJ 3278(BFS+剪枝)

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

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

这题re了好几发,一开始以为是开得不够大或者数据类型的缘故,又去把那篇参考文章看了一遍,考虑是bfs没有剪枝导致队列后面直接爆了,就去做一些可行性剪枝。
主要有:
①当起点>=终点,只能通过-1的操作去实现,就可以直接计算了
②因为n和k都是0~1e5,所以在更新结点的操作后可以判断一次来剪枝
③(这是ac后看别的博主写的),在now.st更新之前,可以预判一下更新后的结点是否标记访问过
剪枝 在搜索中也很重要,不光是这题,哪怕其他不剪枝也能过的也可以写的时候考虑一下(不怕一万就怕万一)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;
int st,ed,sum;
int vis[maxn];
struct node
{
	int st,time;
};
void bfs()
{
	node now,next;
	queue<node>q;
    now.st=st;
    now.time=0;
    q.push(now);
    vis[now.st]=1;
    while(!q.empty())
    {
    	now=q.front();
    	q.pop();
		for(int i=0;i<3;i++)  //有三种操作
		{
			if(i==0)  next.st=now.st-1;
			else if(i==1)  next.st=now.st+1;
			else   next.st=now.st*2;
			if(next.st<0||next.st>100000)  continue;  //剪枝
			if(!vis[next.st])  //更新邻居结点并放进队列
			{
				next.time=now.time+1;
		        vis[next.st]=1;
		        q.push(next);
			}
			if(next.st==ed)  //结束状态
    	    {
    	        sum=next.time;
    	        return;
	        }
		}
	}
}
int main()
{
	cin>>st>>ed;
	sum=0;
	memset(vis,0,sizeof(vis));
	if(st>=ed)  sum=st-ed;  //剪枝
	else  bfs();
	cout<<sum;
    return 0;	
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值