题目如题。题解如题。

因为目标点最多仅仅有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)的更多相关文章

  1. hdu 4771 Stealing Harry Potter&#39;s Precious(bfs)

    题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...

  2. 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 ...

  3. 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 ...

  4. HDU 4771 Stealing Harry Potter's Precious dfs+bfs

    Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...

  5. 算法导论—无向图的遍历(BFS+DFS,MATLAB)

    华电北风吹 天津大学认知计算与应用重点实验室 最后改动日期:2015/8/22 无向图的存储方式有邻接矩阵,邻接链表,稀疏矩阵等. 无向图主要包括双方面内容,图的遍历和寻找联通分量. 一.无向图的遍历 ...

  6. hdu 1710 二叉树的遍历

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1710 大意:给出一个二叉树的前序和中序,求其后序遍历 ps:1.在写链表时,需要写明typedef str ...

  7. 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压

    2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...

  8. HDU 2023 求平均成绩

    Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU ...

  9. hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: ...

随机推荐

  1. Vue系列(一):简介、起步、常用指令、事件和属性、模板、过滤器

    一. Vue.js简介 1. Vue.js是什么 Vue.js也称为Vue,读音/vju:/,类似view,错误读音v-u-e 是一个轻量级MVVM(Model-View-ViewModel)框架,和 ...

  2. Null值操作

      1)NULL值写入的操作    create table j010(     id number(7),     name varchar2(20),     salary number(7,2) ...

  3. ASP.NET-SOAP、UDDI知识点

    1. 什么是SOAP? 答:是简单访问协议.是在分布式环境中,交换信息并实现远程调用的协议.是一个基于XML的协议.使用SOAP,可以不考虑任何传输协议,但通常还是HTTP协议,可以允许任何类型的对象 ...

  4. npm API文档

    npm API文档 https://docs.npmjs.com/

  5. Detours改动段属性漏洞

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  6. 关于python的hashlib md5的报错处理

    1.报错信息是:TypeError: Unicode-objects must be encoded before hashing 2.报错信息是:TypeError: object supporti ...

  7. .net中的目录

    System.Environment.CurrentDirectory Application.StartupPath https://msdn.microsoft.com/en-us/library ...

  8. nyoj--514--1的个数(贪心)

     1的个数 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 给你两个数a和b,你的任务是计算出1在a和b之间出现的次数,比如说,如果a=1024,b=1032,那么a ...

  9. Install Rails on ubuntu 12.04 LTS

    There are basically there ways to install Rails development environment on your ubuntu linux system, ...

  10. Caffe学习--Blob分析

    Caffe_blob 1.基本数据结构 Blob为模板类,可以理解为四维数组,n * c * h * w的结构,Layer内为blob输入data和diff,Layer间的blob为学习的参数.内部封 ...