去年因为太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求解。

最新文章

  1. jvm系列(三):java GC算法 垃圾收集器
  2. UVALive 3644 X-Plosives
  3. Javascript学习笔记2.1 Javascript与DOM简介
  4. python学习之路-day6-面向对象
  5. echarts入门基础,画折线图
  6. poj 1475 uva 589 - Pushing Boxes
  7. 从0 开始 WPF MVVM 企业级框架实现与说明 ---- 第二讲 WPF中 绑定
  8. oracle REGEXP_SUBSTR实现字符串转列
  9. T4文本模板
  10. Android开发之监听发出的短信
  11. GridView 的简单应用
  12. 在webAPI的BaseController上使用RoutePrefix
  13. 基于 Redis 的分布式锁
  14. 一步一步教你如何用Python做词云
  15. MyBatis 的 XML 映射文件使用说明
  16. https跨域到http问题解决
  17. SQL Server 跨服务器查询
  18. Spring中的@Bean注解、@Configuration注解、@Value
  19. shell命令,从字符串中提取数字
  20. ThinkPHP框架 祖辈分的理解 【儿子 FenyeController】继承了【父亲 FuController】继承了【祖辈 Controller】的

热门文章

  1. 错误:非法字符:“\ufeff”
  2. layui table 分页 记住之前勾选的数据
  3. 【洛谷P3959】宝藏
  4. django开发环境搭建(参考流程)
  5. 【leetcode】523. Continuous Subarray Sum
  6. java生成图片验证码(转)--封装生成图片验证码的工具类
  7. volley简介
  8. 利用python进行数据分析--pandas入门2
  9. iOS环境搭建
  10. duration of lease 1 0.5 0.875 DHCP 租借时间 续租时间 重新绑定时间