http://acm.hdu.edu.cn/showproblem.php?pid=4771

题目大意:

给你一幅图(N*M)“@”是起点,"#"是墙,“.”是路,然后图上有K个珠宝,

问是否能全部取走珠宝,若能则输出最小步数,否则-1。

思路,可以bfs处理珠宝与珠宝,珠宝与起点的距离同时还可以判断是否可达,

最后因为K<=4,所以枚举路径输出最小ans

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
#define N 102
#define inf 0x3f3f3f3f int n,m,sx,sy,k;
char pic[][];
int vis[][];
int dis[][],d_cnt; //d_cnt最后有几个点(包括起点)
struct COORD{
int x,y;
}coord[];
int dir[][]={{,},{,},{-,},{,-}}; int bfs(int &x,int &y){
memset(vis,,sizeof(vis));
queue<int>q;
q.push(coord[x].x);q.push(coord[x].y);
int x1,y1,xx,yy,i;
while(!q.empty()){
x1=q.front(); q.pop();
y1=q.front(); q.pop();
if(x1==coord[y].x&&y1==coord[y].y) return vis[x1][y1];
for(i=; i<; ++i){
xx=x1+dir[i][];
yy=y1+dir[i][];
if(xx>&&xx<=n&&yy>&&yy<=m&&pic[xx][yy]!='#'&&!vis[xx][yy]){
vis[xx][yy]=vis[x1][y1]+;
q.push(xx); q.push(yy);
}
}
}
return ;
} int main(){
int i,j,x,y;
while(scanf("%d%d",&n,&m)!=EOF&&(n||m)){
for(i=; i<=n; ++i)
for(j=; j<=m; ++j){
scanf(" %c",&pic[i][j]);
if(pic[i][j]=='@') sx=i,sy=j;
}
scanf("%d",&k);
d_cnt=;
coord[].x=sx;
coord[].y=sy;
for(i=; i<k; ++i){
scanf("%d%d",&x,&y);
if(x!=sx||y!=sy){ //判断起点上是否放有珠宝
pic[x][y]='*';
coord[d_cnt].x=x;
coord[d_cnt++].y=y;
}
}
int flag=; //求距离顺便判断是否可到达
for(i=; i<d_cnt&&flag; ++i)
for(j=i+; j<d_cnt&&flag; ++j){
flag=bfs(i,j);
dis[i][j]=dis[j][i]=flag;
}
if(!flag) puts("-1");
else{
int p[],ans=inf;
for(i=; i<d_cnt; ++i) p[i]=i;
do{
int sum=;
for(i=; i<d_cnt-; ++i)
sum+=dis[p[i]][p[i+]];
ans=min(ans,sum);
}while(next_permutation(p,p+d_cnt)&&!p[]); //全排列枚举
printf("%d\n",ans);
}
}
return ;
}

然后还有一个问题希望神牛们能解决:要是K加大到50怎么办?

HDU4771(2013 Asia Hangzhou Regional Contest )的更多相关文章

  1. 2013 Asia Hangzhou Regional Contest

    Lights Against Dudely http://acm.hdu.edu.cn/showproblem.php?pid=4770 15个位置,所以可以暴力枚举那些放,对于放的再暴力枚举哪个转, ...

  2. 2013 Asia Hangzhou Regional Contest hdu4780 Candy Factory

    参考:https://blog.csdn.net/sd_invol/article/details/15813671 要点 每个任务的结束时间是固定的,不受任何因素影响 机器只在最一开始有用,在那之后 ...

  3. HDU 3685 Rotational Painting(多边形质心+凸包)(2010 Asia Hangzhou Regional Contest)

    Problem Description Josh Lyman is a gifted painter. One of his great works is a glass painting. He c ...

  4. HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)

    Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...

  5. 2012 Asia Hangzhou Regional Contest

    Friend Chains http://acm.hdu.edu.cn/showproblem.php?pid=4460 图的最远两点距离,任意选个点bfs,如果有不能到的点直接-1.然后对于所有距离 ...

  6. 2013 Asia Chengdu Regional Contest

    hdu 4786 Fibonacci Tree http://acm.hdu.edu.cn/showproblem.php?pid=4786 copyright@ts 算法源于ts,用最小生成树可以求 ...

  7. HDU 3689 Infinite monkey theorem(DP+trie+自动机)(2010 Asia Hangzhou Regional Contest)

    Description Could you imaging a monkey writing computer programs? Surely monkeys are smart among ani ...

  8. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  9. zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest

    Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history roll ...

随机推荐

  1. 【DP】【单调队列】【NOI2005】瑰丽华尔兹

    340. [NOI2005] 瑰丽华尔兹 ★★★ 输入文件:adv1900.in 输出文件:adv1900.out 简单对照 时间限制:1 s 内存限制:128 MB [任务描写叙述] 你跳过华尔兹吗 ...

  2. 【C/C++学院】0831-类与对象的异常/面试100题1-100

    类与对象的异常 Cpp异常 #include <iostream> #include <string.h> using namespace std; //标识错误的类型 cla ...

  3. CHAPTER ONE LOAD-BALANCING

    1.1 Synopsis In this part, we will explain how to create a load-balancer withnginxfor a lot of OpenE ...

  4. 【Java】Java_06 基本数据类型

    1.基本数据类型 Java是一种强类型语言,每个变量都必须声明其类型. .  Java的数据类型分为两大类:基本类型(primitive type)和引用类型    (reference type) ...

  5. java 线程的几种状态(转载)

      java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在 ...

  6. Silverlight Telerik RadGridView动态增删行及行列操作(转载)

    最近使用一直使用第三方控件Telerik,版本 2011 Q1,一直使用显示控件RadGridView,使用起来比DataGird好使, 也发现有控件问题. radgridview.BeginInse ...

  7. java - day13 - UnionPay

    银行功能实现,主要为抽象类及接口的运用 package com.javatest.bankimple; /* UnionPay */ //银联接口 public interface UnionPay ...

  8. Shiro学习(总结)

    声明:本文原文地址:http://www.iteye.com/blogs/subjects/shiro 感谢开涛提供的博文,让我学到了非常多.在这里由衷的感谢你,同一时候我强烈的推荐开涛的博文.他的博 ...

  9. Modification of UCT with Patterns in Monte-Carlo Go(论文阅读)

    摘要:用于解决多臂赌博机UCB1算法已经被扩展成了解决极大极小树搜索的UCT算法.我们开发了一套Monte-Carlo围棋程序,MoGo,这是第一个使用UCT算法实现的计算机围棋程序.我们解释了为了围 ...

  10. CF 482A(Diverse Permutation-相邻距离不同数为k的1~n全排列构造)

    A. Diverse Permutation time limit per test 1 second memory limit per test 256 megabytes input standa ...