bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士(BFS)
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.
Input
Output
Sample Input
Sample Output
中文题面有错
把“相邻的数字所对应的区域是相邻的……布在两个不同的行里”这句话去掉就好了
求出起点到所有点的最短路,再求出终点到所有点的最短路
然后暴力枚举每个灌木求个最短路之和的最小值
#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)相关推荐
- bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士
题目链接 题目背景: 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为了能安全地离开,贝茜不得不按照骑士们的要求,在森林寻找一种特殊的灌木 ...
- 1671: [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 351 Solved: 22 ...
- bzoj1671 Knights of Ni 骑士 BFS
Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为了能安全地离开,贝茜不得不按照骑士们的要求,在森林寻找一种特殊的灌 ...
- [题解] Knights of Ni 骑士 C++
Knights of Ni 骑士 题目 Description Input Output Sample Input Sample Output 思路 代码 题目 Description 给出一张W*H ...
- bzoj 1673: [Usaco2005 Dec]Scales 天平(DFS)
1673: [Usaco2005 Dec]Scales 天平 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 695 Solved: 253 [Subm ...
- bzoj 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚(DP)
1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 941 Solved ...
- bzoj 1731 [Usaco2005 dec]Layout 排队布局——差分约束
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 对差分约束理解更深.还发现美妙博客:http://www.cppblog.com/me ...
- bzoj 1731: [Usaco2005 dec]Layout 排队布局【差分约束】
差分约束裸题,用了比较蠢的方法,先dfs_spfa判负环,再bfs_spfa跑最短路 注意到"奶牛排在队伍中的顺序和它们的编号是相同的",所以\( d_i-d_{i-1}>= ...
- BZOJ 1673 [Usaco2005 Dec]Scales 天平:dfs 启发式搜索 A*搜索
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1673 题意: 有n个砝码(n <= 1000),重量为w[i]. 你要从中选择一些砝 ...
最新文章
- Python爬虫小偏方:如何用robots.txt快速抓取网站?
- PostgreSQL(三)pgpool管理PostgreSQL集群下主机宕机后的主从切换
- vue 创建项目的命令
- 计算机秋招必备!广州互联网大厂企业整理清单!
- OpenCV示例学习(二): 基本图形绘制算子:line(),circle(),fillPoly(), ellipse()
- Android 高级UI设计笔记08:Android开发者常用的7款Android UI组件(转载)
- Delphi 防止程序多次运行《转》
- 浅入浅出 Android 安全 翻译完成!
- A股收盘:深证区块链50指数跌3.80%,爱迪尔等9股涨停
- tp auth 转载保存
- 闭包 进阶 javascript
- 【Spring学习笔记-0】Spring开发所需要的核心jar包
- 如果希望同时导入m中的所有成员_Python3.7知其然知其所以然-第十九章 模块导入...
- Coap协议学习(二)
- html主题标签是什么意思,HTML5所有标签汇总及标签意义解释
- 工作15年码农总结:学编程难吗?那只是你觉得难!
- 高三学习计划作文计算机专业,高三学习计划作文.docx
- 计算机应用格式工厂部分教案,格式工厂教学案.doc
- saas-export项目-系统日志管理-系统日志AOP配置
- c语言中:=和==的区别是什么?
热门文章
- php和python-python与php比较
- python语言入门-分分钟入门python语言
- ZS语音识别(智能语音识别工具)V1.3 绿色版
- python数据分析天气预报_数据分析----天气预报走向(pygal)
- html图片使用glide,jQuery响应式幻灯片插件jquery.glide.js(支持触摸轻量级)
- 【链表】剑指offer:从尾到头打印链表
- ffmpeg.c函数结构简单分析(画图)
- python爬虫怎么发布请求_http请求如何在python爬虫中实现?
- python中如何反解函数_PyTorch中反卷积的用法详解
- java适合年龄_Java实现三人年龄