SUSTOJ_路痴的单身小涵(图中最短路的条数)
2024-10-07 11:11:14
去年因为太low没有做出来校赛的最后一题,遂今年校赛做了这个题,下面我做详细描述。
本题大意:给定一个无向图G,每个边的权值为1,图中L表示起点,C表示终点,#表示未通路,给定时间k,让你判断是否能在给定时间k内走到C,并回答路径最短的路径有多少条?
本题思路:裸的搜索问题,由于图过大,所以直接dfs搜会超时,需要先bfs求解答案如果k步内能走到则再进行下一步的求解。。。
参考代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#define mid ((l + r) / 2)
using namespace std; typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double pi = acos(-1.0); const int maxn = 1e3 + ;
int n, m, k, step, ans, Dist;
char G[maxn][maxn];
int dist[maxn][maxn];
bool vis[maxn][maxn];
pii B, E, now, Next; int bfs(int x, int y) {
memset(vis, false, sizeof vis);
memset(dist, , sizeof dist);
queue <pii> Q;
Q.push(make_pair(x, y));
dist[x][y] = ;
while(!Q.empty()) {
pii now = Q.front();
Q.pop();
if(now.first == E.first && now.second == E.second) return dist[now.first][now.second];
for(int dx = -; dx <= ; dx ++) {
for(int dy = -; dy <= ; dy ++) {
if(abs(dx - dy) == ) {
Next.first = now.first + dx;
Next.second = now.second + dy;
if(!vis[Next.first][Next.second] && Next.first >= && Next.first < n && Next.second >= && Next.second < m && G[Next.first][Next.second] != '#') {
dist[Next.first][Next.second] = dist[now.first][now.second] + ;
Q.push(make_pair(Next.first, Next.second));
vis[Next.first][Next.second] = true;
}
}
}
}
}
return -;
} void dfs(pii B, pii E, int D) {
if(B.first == E.first && B.second == E.second) {
if(D < Dist) {
step = ;
Dist = D;
} else if(D == Dist) step ++;
return;
}
if(D > Dist) return;
for(int i = -; i <= ; i ++) {
for(int j = -; j <= ; j ++) {
if(abs(i - j) == ) {
if(B.first + i >= && B.first + i < n && B.second + j >= && B.second + j < m) {
if(G[B.first + i][B.second + j] != '#' && !vis[B.first + i][B.second + j]) {
vis[B.first + i][B.second + j] = true;
dfs(make_pair(B.first + i, B.second + j), E, D + );
vis[B.first + i][B.second + j] = false;
}
}
}
}
}
} int main() {
ios_base :: sync_with_stdio(false);
int t, Case = ;
cin >> t;
while(t --) {
step = ;
Dist = 0x3f3f3f3f;
cin >> n >> m >> k;
for(int i = ; i < n; i ++) cin >> G[i];
for(int i = ; i < n; i ++) {
for(int j = ; j < m; j ++) {
if(G[i][j] == 'L') B = make_pair(i, j);
if(G[i][j] == 'C') E = make_pair(i, j);
}
}
ans = bfs(B.first, B.second);
if(ans > k) ans = -;
cout << "Case #" << ++ Case << ": " << ans << ' ';
if(ans != -) {
memset(vis, false, sizeof vis);
dfs(B, E, );
cout << step;
}
cout << endl;
}
return ;
}
相同的题目,如果边的权值不为1,则可用SPFA+dfs求解。
最新文章
- jvm系列(三):java GC算法 垃圾收集器
- UVALive 3644 X-Plosives
- Javascript学习笔记2.1 Javascript与DOM简介
- python学习之路-day6-面向对象
- echarts入门基础,画折线图
- poj 1475 uva 589 - Pushing Boxes
- 从0 开始 WPF MVVM 企业级框架实现与说明 ---- 第二讲 WPF中 绑定
- oracle REGEXP_SUBSTR实现字符串转列
- T4文本模板
- Android开发之监听发出的短信
- GridView 的简单应用
- 在webAPI的BaseController上使用RoutePrefix
- 基于 Redis 的分布式锁
- 一步一步教你如何用Python做词云
- MyBatis 的 XML 映射文件使用说明
- https跨域到http问题解决
- SQL Server 跨服务器查询
- Spring中的@Bean注解、@Configuration注解、@Value
- shell命令,从字符串中提取数字
- ThinkPHP框架 祖辈分的理解 【儿子 FenyeController】继承了【父亲 FuController】继承了【祖辈 Controller】的