1671: [Usaco2005 Dec]Knights of Ni 骑士

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 416  Solved: 266
[Submit][Status][Discuss]

Description

Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded by the Knights of Ni. In order to pass through safely, the Knights have demanded that she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible. Bessie has a map of of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel to the X and Y axes. The map is W x H units in size (1 <= W <= 1000; 1 <= H <= 1000). The map shows where Bessie starts her quest, the single square where the Knights of Ni are, and the locations of all the shrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks are impassable because of swamps, cliffs, and killer rabbits). Bessie can not pass through the Knights of Ni square without a shrubbery. In order to make sure that she follows the map correctly, Bessie can only move in four directions: North, East, South, or West (i.e., NOT diagonally). She requires one day to complete a traversal from one grid block to a neighboring grid block. It is guaranteed that Bessie will be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest way for her to do so.

 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为了能安全地离开,贝茜不得不按照骑士们的要求,在森林寻找一种特殊的灌木并带一棵给他们.当然,贝茜想早点离开这可怕的森林,于是她必须尽快完成骑士们给的任务,贝茜随身带着这片森林的地图,地图上的森林被放入了直角坐标系,并按x,y轴上的单位长度划分成了W×H(1≤W,H≤1000)块,贝茜在地图上查出了她自己以及骑士们所在的位置,当然地图上也标注了她所需要的灌木生长的区域.某些区域是不能通过的(比如说沼泽地,悬崖,以及食人兔的聚居地).在没有找到灌木之前,贝茜不能通过骑士们所在的那个区域,为了确保她自己不会迷路,贝茜只向正北、正东、正南、正西四个方向移动(注意,她不会走对角线).她要走整整一天,才能从某块区域走到与它相邻的那块区域.    输入数据保证贝茜一定能完成骑士的任务.贝茜希望你能帮她计算一下,她最少需要多少天才可脱离这可怕的地方?

Input

    第1行输入2个用空格隔开的整数,即题目中提到的W、H.
    接下来输入贝茜持有的地图,每一行用若干个数字代表地图上对应行的地形.第1行描述了地图最北的那一排土地;最后一行描述的则是最南面的.相邻的数字所对应的区域是相邻的.如果地图的宽小于或等于40,那每一行数字恰好对应了地图上的一排土地.如果地图的宽大于40,那每行只会给出40个数字,并且保证除了最后一行的每一行都包含恰好40个数字.没有哪一行描述的区域分布在两个不同的行里.
    地图上的数字所对应的地形:
    0:贝茜可以通过的空地
    1:由于各种原因而不可通行的区域
    2:贝茜现在所在的位置
    3:骑士们的位置
    4:长着贝茜需要的灌木的土地

Output

    输出一个正整数D,即贝茜最少要花多少天才能完成骑士们给的任务.

Sample Input

8 4
4 1 0 0 0 0 1 0
0 0 0 1 0 1 0 0
0 2 1 1 3 0 4 0
0 0 0 1 1 1 1 0

Sample Output

11

中文题面有错

把“相邻的数字所对应的区域是相邻的……布在两个不同的行里”这句话去掉就好了

求出起点到所有点的最短路,再求出终点到所有点的最短路

然后暴力枚举每个灌木求个最短路之和的最小值

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct
{int x;int y;
}Point;
Point now, temp, s[1000005];
queue<Point> q1, q2;
int dp1[1005][1005], dp2[1005][1005], a[1005][1005];
int dir[4][2] = {1,0,-1,0,0,-1,0,1};
int main(void)
{int cnt, n, m, i, j, ans;scanf("%d%d", &m, &n);cnt = 0;memset(a, -1, sizeof(a));memset(dp1, 62, sizeof(dp1));memset(dp2, 62, sizeof(dp2));for(i=1;i<=n;i++){for(j=1;j<=m;j++){scanf("%d", &a[i][j]);if(a[i][j]==2){now.x = i, now.y = j;q1.push(now);dp1[i][j] = 0;}if(a[i][j]==3){now.x = i, now.y = j;q2.push(now);dp2[i][j] = 0;}if(a[i][j]==4)s[++cnt].x = i, s[cnt].y = j;}}while(q1.empty()==0){now = q1.front();q1.pop();for(i=0;i<=3;i++){temp.x = now.x+dir[i][0];temp.y = now.y+dir[i][1];if(a[temp.x][temp.y]==1 || a[temp.x][temp.y]==-1)continue;if(dp1[now.x][now.y]+1<dp1[temp.x][temp.y]){dp1[temp.x][temp.y] = dp1[now.x][now.y]+1;q1.push(temp);}}}while(q2.empty()==0){now = q2.front();q2.pop();for(i=0;i<=3;i++){temp.x = now.x+dir[i][0];temp.y = now.y+dir[i][1];if(a[temp.x][temp.y]==1 || a[temp.x][temp.y]==-1)continue;if(dp2[now.x][now.y]+1<dp2[temp.x][temp.y]){dp2[temp.x][temp.y] = dp2[now.x][now.y]+1;q2.push(temp);}}}ans = 1000005;for(i=1;i<=cnt;i++)ans = min(ans, dp1[s[i].x][s[i].y]+dp2[s[i].x][s[i].y]);printf("%d\n", ans);return 0;
}

bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士(BFS)相关推荐

  1. bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士

    题目链接 题目背景: 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为了能安全地离开,贝茜不得不按照骑士们的要求,在森林寻找一种特殊的灌木 ...

  2. 1671: [Usaco2005 Dec]Knights of Ni 骑士

    1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 351  Solved: 22 ...

  3. bzoj1671 Knights of Ni 骑士 BFS

    Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为了能安全地离开,贝茜不得不按照骑士们的要求,在森林寻找一种特殊的灌 ...

  4. [题解] Knights of Ni 骑士 C++

    Knights of Ni 骑士 题目 Description Input Output Sample Input Sample Output 思路 代码 题目 Description 给出一张W*H ...

  5. bzoj 1673: [Usaco2005 Dec]Scales 天平(DFS)

    1673: [Usaco2005 Dec]Scales 天平 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 695  Solved: 253 [Subm ...

  6. bzoj 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚(DP)

    1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 941  Solved ...

  7. bzoj 1731 [Usaco2005 dec]Layout 排队布局——差分约束

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 对差分约束理解更深.还发现美妙博客:http://www.cppblog.com/me ...

  8. bzoj 1731: [Usaco2005 dec]Layout 排队布局【差分约束】

    差分约束裸题,用了比较蠢的方法,先dfs_spfa判负环,再bfs_spfa跑最短路 注意到"奶牛排在队伍中的顺序和它们的编号是相同的",所以\( d_i-d_{i-1}>= ...

  9. BZOJ 1673 [Usaco2005 Dec]Scales 天平:dfs 启发式搜索 A*搜索

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1673 题意: 有n个砝码(n <= 1000),重量为w[i]. 你要从中选择一些砝 ...

最新文章

  1. Python爬虫小偏方:如何用robots.txt快速抓取网站?
  2. PostgreSQL(三)pgpool管理PostgreSQL集群下主机宕机后的主从切换
  3. vue 创建项目的命令
  4. 计算机秋招必备!广州互联网大厂企业整理清单!
  5. OpenCV示例学习(二): 基本图形绘制算子:line(),circle(),fillPoly(), ellipse()
  6. Android 高级UI设计笔记08:Android开发者常用的7款Android UI组件(转载)
  7. Delphi 防止程序多次运行《转》
  8. 浅入浅出 Android 安全 翻译完成!
  9. A股收盘:深证区块链50指数跌3.80%,爱迪尔等9股涨停
  10. tp auth 转载保存
  11. 闭包 进阶 javascript
  12. 【Spring学习笔记-0】Spring开发所需要的核心jar包
  13. 如果希望同时导入m中的所有成员_Python3.7知其然知其所以然-第十九章 模块导入...
  14. Coap协议学习(二)
  15. html主题标签是什么意思,HTML5所有标签汇总及标签意义解释
  16. 工作15年码农总结:学编程难吗?那只是你觉得难!
  17. 高三学习计划作文计算机专业,高三学习计划作文.docx
  18. 计算机应用格式工厂部分教案,格式工厂教学案.doc
  19. saas-export项目-系统日志管理-系统日志AOP配置
  20. c语言中:=和==的区别是什么?

热门文章

  1. php和python-python与php比较
  2. python语言入门-分分钟入门python语言
  3. ZS语音识别(智能语音识别工具)V1.3 绿色版
  4. python数据分析天气预报_数据分析----天气预报走向(pygal)
  5. html图片使用glide,jQuery响应式幻灯片插件jquery.glide.js(支持触摸轻量级)
  6. 【链表】剑指offer:从尾到头打印链表
  7. ffmpeg.c函数结构简单分析(画图)
  8. python爬虫怎么发布请求_http请求如何在python爬虫中实现?
  9. python中如何反解函数_PyTorch中反卷积的用法详解
  10. java适合年龄_Java实现三人年龄