【题解】洛谷P1979 华容道(bfs)

本文介绍了一种基于迷宫地图的寻路算法实现方法,通过建立图模型并使用宽度优先搜索(BFS)和最短路径算法SPFA来解决起点到终点的最短路径问题。文章详细解释了如何构建节点连接、如何处理特殊场景,并提供了完整的C++代码示例。

这道题目

注意反数组和dx与dy数组应对应

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#define inf 1e9
using namespace std;
const int maxm=100010;
const int maxn=35;
const int dx[5]={0,1,0,-1,0};
const int dy[5]={0,0,1,0,-1};
const int fan[5]={0,3,4,1,2}; 
int headd[maxm*2],nnext[maxm*2],to[maxm*2],length[maxm*2];
int tot,n,m,q,no=0;
int map[maxn][maxn];
int step[maxn][maxn];
int num[maxn][maxn][5];
int dis[maxm];
struct zb
{
	int x;
	int y;
};
void add(int x,int y,int l)
{
	tot++;
	nnext[tot]=headd[x];
	headd[x]=tot;
	to[tot]=y;
	length[tot]=l;
}
int bfs(int sx,int sy,int tx,int ty)
{
	if(sx==tx&&sy==ty) return 0;
	struct zb team[maxm];
	int head=0,tail=0; 
	struct zb S,T;
	S.x=sx, S.y=sy, T.x=tx, T.y=ty;
	bool b[maxn][maxn];
	memset(b,false,sizeof(b));
	memset(step,0,sizeof(step));
	b[S.x][S.y]=true;
	team[++tail]=S;
	while(head<tail)
	{
		struct zb x=team[++head];
	//	cout<<x.x<<' '<<x.y<<endl;
		for(int i=1;i<=4;i++)
		{
			struct zb tmp;
			tmp.x=x.x+dx[i];
			tmp.y=x.y+dy[i];
			if(!b[tmp.x][tmp.y]&&map[tmp.x][tmp.y]==1)
			{
				step[tmp.x][tmp.y]=step[x.x][x.y]+1;
				if(tmp.x==tx&&tmp.y==ty) return step[tmp.x][tmp.y];
				b[tmp.x][tmp.y]=true;
				team[++tail]=tmp;
			}
		}
	}
	return inf;
}
void beginning()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(map[i][j]==1)
			{
				for(int w=1;w<=4;w++)
				{
					if(map[i+dx[w]][j+dy[w]]==1)
					{
						for(int k=1;k<=4;k++)
						{
							if(map[i+dx[k]][j+dy[k]]==1&&w!=k)
							{
								map[i][j]=0;
								int tmp=bfs(i+dx[w],j+dy[w],i+dx[k],j+dy[k]);
								if(tmp!=inf) add(num[i][j][w],num[i][j][k],tmp);
								map[i][j]=1;
							}
						}
					}
				}
			}
		}
	}
		
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(map[i][j]==1)
				for(int w=1;w<=4;w++)
					if(map[i+dx[w]][j+dy[w]]==1)
						add(num[i][j][w],num[i+dx[w]][j+dy[w]][fan[w]],1);
}
void spfa(int s,int t)
{
	int team[maxm],head=0,tail=0;
	
	bool b[maxm];
	memset(b,false,sizeof(b));
	
	for(int i=1;i<=no;i++) dis[i]=inf;
	
	dis[s]=0;
	b[s]=true;
	team[++tail]=s;
	while(head<tail)
	{
		int now=team[++head];
		b[now]=false;

		for(int i=headd[now];i;i=nnext[i])
		{
			int y=to[i];

			if(dis[y]>dis[now]+length[i])
			{
				dis[y]=dis[now]+length[i];
				if(!b[y])
				{
					b[y]=true;
					team[++tail]=y;
				}
			}
		}
	}

	if(dis[t]==inf) cout<<"-1"<<endl;
	else printf("%d\n",dis[t]);
}
int main()
{
	scanf("%d%d%d",&n,&m,&q);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			scanf("%d",&map[i][j]);
		}
	}
	
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			for(int w=1;w<=4;w++)
				num[i][j][w]=++no;
				
	beginning();
	for(int ex,ey,sx,sy,tx,ty,i=1;i<=q;i++)
	{
		scanf("%d%d%d%d%d%d",&ex,&ey,&sx,&sy,&tx,&ty);
		if(sx==tx&&sy==ty)
		{
			cout<<"0"<<endl;
			continue;
		}
		if(map[ex][ey]==0||map[sx][sy]==0||map[tx][ty]==0)
		{
			cout<<"-1"<<endl;
			continue;
		}
		int s=++no,t=++no;
		for(int j=1;j<=4;j++)
		{
			if(map[sx+dx[j]][sy+dy[j]]==1)
			{
				map[sx][sy]=0;
				int tmp=bfs(ex,ey,sx+dx[j],sy+dy[j]);
				//cout<<j<<"*"<<tmp<<endl;
				if(tmp!=inf) add(s,num[sx][sy][j],tmp);
				map[sx][sy]=1;
			}
		}

		for(int j=1;j<=4;j++)
		{
			if(map[tx+dx[j]][ty+dy[j]]==1)
			{

				add(num[tx][ty][j],t,0);
			}
		}
		spfa(s,t);
	}
	return 0;
}

 

洛谷 P2516 题目涉及的是一个与字符串处理和动态规划相关的挑战。题目要求对一个由数字组成的字符串进行分割,使得每个分割出的数字子串能构成一个递增序列,并且每部分对应的数值都比前一部分大。以下是解题思路及实现方法。 ### 问题解析 - 输入是一个长度不超过 **40** 的纯数字字符串。 - 目标是将该字符串分割成若干个非空数字子串,这些子串所表示的数值形成一个严格递增序列。 - 每个分割出来的子串必须满足其数值大于前一个子串的数值。 - 终输出所有可能的合法分割方案的数量。 ### 解法概述 此问题可以通过 **深度优先搜索 (DFS)** 或 **回溯法** 来解决: - 使用递归的方式尝试在每一个位置进行分割。 - 对于每一次分割,提取当前子串并转换为整数,然后判断它是否大于上一次分割得到的值。 - 如果符合递增条件,则继续递归处理剩余的字符串部分。 - 当遍历完整个字符串并且满足所有分割条件时,计数器加一。 ### 实现细节 - 因为输入字符串长度大为 **40**,所以需要考虑大数问题(超过 `int` 范围),建议使用 `long long` 类型或 Python 中的 `int` 类型自动处理大整数。 - 在分割过程中,确保没有前导零(除非子串长度为 1)。 - 递归终止条件是字符串已经被完全分割。 ### 示例代码 (C++) ```cpp #include <iostream> #include <string> using namespace std; int count = 0; // 将字符串 s 的 [start, end) 子串转换为整数 long long to_number(const string &s, int start, int end) { long long num = 0; for (int i = start; i < end; ++i) { num = num * 10 + (s[i] - '0'); } return num; } // DFS 函数:从 pos 位置开始分割,last_num 表示上一次分割出的数 void dfs(const string &s, int pos, long long last_num) { if (pos == s.size()) { count++; return; } for (int i = pos + 1; i <= s.size(); ++i) { // 剪枝:如果子串长度大于1且以0开头,则跳过 if (i - pos > 1 && s[pos] == '0') break; long long current = to_number(s, pos, i); if (current > last_num) { dfs(s, i, current); } } } int main() { string s; cin >> s; dfs(s, 0, -1); // 初始时 last_num 设置为 -1,保证第一个数可以任意选择 cout << count << endl; return 0; } ``` ### 算法复杂度分析 - 时间复杂度:坏情况下为指数级 $O(2^n)$,因为每次递归都有多个分支。 - 空间复杂度:主要取决于递归栈深度,多为 $O(n)$。 这种方法适用于题目给定的数据规模(字符串长度 ≤ 40),通过适当的剪枝优化,可以在合理时间内完成计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值