hdu 4771 求一点遍历全部给定点的最短路(bfs+dfs)
题目如题。题解如题。
因为目标点最多仅仅有4个,先bfs出俩俩最短路(包含起点)。再dfs最短路。)0s1A;(当年弱跪杭州之题,现看如此简单)
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct point
{
int x,y;
int cnt;
};
char a[105][105];
vector<point>po;
int n,m;int k;
int mindis[10][10];
int vis[105][105];
int f[4][2]={0,1,0,-1,1,0,-1,0};
int bfs(int s,int t)
{
memset(vis,0,sizeof(vis));
queue<point>q;
po[s].cnt=0;
po[t].cnt=-1;
q.push(po[s]);
vis[po[s].x][po[s].y]=1;
while(!q.empty())
{
point cur=q.front();
q.pop();
point next;
for(int i=0;i<4;i++)
{
next.x=cur.x+f[i][0];
next.y=cur.y+f[i][1];
if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&vis[next.x][next.y]==0&&a[next.x][next.y]!='#')
{
next.cnt=cur.cnt+1;
if(next.x==po[t].x&&next.y==po[t].y)
{
return next.cnt;
}
q.push(next);
vis[next.x][next.y]=1;
}
}
}
return -1;
}
int mins=100000000;
int vis2[10];
void dfs(int u,int lev,int sumdis)
{
if(sumdis>=mins)return ;
if(lev==k)
{
if(sumdis<mins)
{
mins=sumdis;return ;
}
}
for(int i=1;i<=k;i++)
{
if(!vis2[i])
{
vis2[i]=1;
dfs(i,lev+1,sumdis+mindis[u][i]);
vis2[i]=0;
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m)&&(n||m))
{
memset(mindis,0,sizeof(mindis));
mins=100000000;
po.clear();
for(int i=0;i<n;i++)
{
getchar();
for(int j=0;j<m;j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='@')
{
point txy;txy.x=i;txy.y=j;
po.push_back(txy);
}
}
}
scanf("%d",&k); int aa,bb;
for(int i=0;i<k;i++)
{
scanf("%d%d",&aa,&bb);
point txy;txy.x=aa-1;txy.y=bb-1;
po.push_back(txy);
}
int mark=1;
for(int i=0;i<k+1;i++)
for(int j=i+1;j<k+1;j++)
{
mindis[j][i]=mindis[i][j]=bfs(i,j);
if(mindis[i][j]==-1)
{
mark=0;break;
}
}
if(mark==0)
{
printf("-1\n");continue;
}
memset(vis2,0,sizeof(vis2));
dfs(0,0,0);
printf("%d\n",mins);
}
}
hdu 4771 求一点遍历全部给定点的最短路(bfs+dfs)的更多相关文章
- hdu 4771 Stealing Harry Potter's Precious(bfs)
题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
- hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)
7-4 Cartesian Tree (30分) A Cartesian tree is a binary tree constructed from a sequence of distinct ...
- HDU 4771 Stealing Harry Potter's Precious dfs+bfs
Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...
- 算法导论—无向图的遍历(BFS+DFS,MATLAB)
华电北风吹 天津大学认知计算与应用重点实验室 最后改动日期:2015/8/22 无向图的存储方式有邻接矩阵,邻接链表,稀疏矩阵等. 无向图主要包括双方面内容,图的遍历和寻找联通分量. 一.无向图的遍历 ...
- hdu 1710 二叉树的遍历
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1710 大意:给出一个二叉树的前序和中序,求其后序遍历 ps:1.在写链表时,需要写明typedef str ...
- 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压
2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
- HDU 2023 求平均成绩
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU ...
- hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的: ...
随机推荐
- django-7-django模型系统
<<<常用的模型字段类型>>>https://docs.djangoproject.com/en/2.1/ref/models/fields/#field-type ...
- 小学生都能学会的python(编码 and 字符串)
小学生都能学会的python(编码 and 字符串) 一,编码 最早的计算机编码是ASCII. 有英文+数字+特殊字符 8bit => 1byte 没有中文, 后面的编码必须兼容ASCII ...
- ASP.NET-viewBag Viewdata Tempdata
ASP.NET MVC提供3种选择ViewData,ViewBag,TempData来从controller到View及后续请求传输数据. ViewData和ViewBag很相似,而TempData有 ...
- java用freemarker实现导出excel
前几天做了jxl导出excel,现在用freemarker做一下 freemarker导出excel和导出word步骤和是实现方法是相同的. 1.制作excel模板 2.将后缀名改为ftl,放到对应的 ...
- [BLE--Link Layer]设备蓝牙地址
简述 不论什么网络设备而言,都会有自己独特的一个MAC地址,不然在设备量较大的情况下非常可能造成通信的混乱.蓝牙是无线通信中使用非常广泛的技术.当然其蓝牙地址也是相当的重要的了. 蓝牙地址简述 种类划 ...
- pat(A) 1066. Root of AVL Tree
代码: #include<iostream> #include<cstdio> #include<cmath> #include<stdlib.h> # ...
- node15---cookie session
二.Cookie和Session 2.1 Cookie ● HTTP是无状态协议.简单地说,当你浏览了一个页面,然后转到同一个网站的另一个页面,服务器无法认识到,这是同一个浏览器在访问同一个网站.每一 ...
- caffe中lenet_solver.prototxt配置文件注解
caffe框架自带的例子mnist里有一个lenet_solver.prototxt文件,这个文件是具体的训练网络的引入文件,定义了CNN网络架构之外的一些基础参数,如总的迭代次数.测试间隔.基础学习 ...
- 关于如何让cell一直保持选中?
在M上 1.cell的展示,一直都是依靠数据源的支持.所以,必须要在数据源里面新增bool,默认为false 在V上 2.cell的setModel方法里面,将数据源的新增bool赋值为cell的是否 ...
- Synchronization (computer science)
过程同步.数据同步. In computer science, synchronization refers to one of two distinct but related concepts: ...