poj 3170
来源:互联网 发布:在淘宝买狗狗安全吗 编辑:程序博客网 时间:2024/06/08 18:03
Knights of Ni
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2038 Accepted: 875
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.
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
Line 1: Two space-separated integers: W and H.
Lines 2..?: These lines describe the map, row by row. The first line describes the most northwest part of the map; the last line describes the most southeast part of the map. Successive integers in the input describe columns of the map from west to east. Each new row of a map's description starts on a new input line, and each input line contains no more than 40 space-separated integers. If W <= 40, then each input line describes a complete row of the map. If W > 40, then more than one line is used to describe a single row, 40 integers on each line except potentially the last one. No input line ever describes elements of more than one row.
The integers that describe the map come from this set:
0: Square through which Bessie can travel
1: Impassable square that Bessie cannot traverse
2: Bessie's starting location
3: Location of the Knights of Ni
4: Location of a shrubbery
Lines 2..?: These lines describe the map, row by row. The first line describes the most northwest part of the map; the last line describes the most southeast part of the map. Successive integers in the input describe columns of the map from west to east. Each new row of a map's description starts on a new input line, and each input line contains no more than 40 space-separated integers. If W <= 40, then each input line describes a complete row of the map. If W > 40, then more than one line is used to describe a single row, 40 integers on each line except potentially the last one. No input line ever describes elements of more than one row.
The integers that describe the map come from this set:
0: Square through which Bessie can travel
1: Impassable square that Bessie cannot traverse
2: Bessie's starting location
3: Location of the Knights of Ni
4: Location of a shrubbery
Output
Line 1: D, the minimum number of days it will take Bessie to reach a shrubbery and bring it to the Knights of Ni.
Sample Input
8 44 1 0 0 0 0 1 00 0 0 1 0 1 0 00 2 1 1 3 0 4 00 0 0 4 1 1 1 0
Sample Output
11
Hint
Explanation of the sample:
Width=8, height=4. Bessie starts on the third row, only a few squares away from the Knights.
Width=8, height=4. Bessie starts on the third row, only a few squares away from the Knights.
Bessie can move in this pattern to get a shrubbery for the Knights: N, W, N, S, E, E, N, E, E, S, S. She gets the shrubbery in the northwest corner and then makes her away around the barriers to the east and then south to the Knights.
题意:有一个人要通过一个森林,这个森林的出口有一群人守着,然后需要拿到一个棒子,才能通过,问最少的移动步数。
思路:简单bfs,人向着木棒走,求出起点到所有木棒的最短距离,然后守卫向木棒走,求出终点到木棒的最短距离,求和取最小值。注意没拿到木棒之前是不能走守卫那个点的。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>using namespace std;const int INF = 0x3f3f3f3f;struct node{ int x,y,tmp;};int dx[4]= {-1,1,0,0};int dy[4] = {0,0,-1,1};int n,m;int f[1024][1024];int st[1024][1024];int ed[1024][1024];void bfs1(int x,int y){ queue<node> pq; node u,v; u.x = x,u.y = y; u.tmp = 0; pq.push(u); st[x][y] = 0; while(!pq.empty()) { node u = pq.front();pq.pop(); for(int i = 0;i < 4;i++) { node v; v.x = u.x+dx[i]; v.y = u.y+dy[i]; if(0<=v.x&&v.x<n&&v.y<m&&v.y>=0&&f[v.x][v.y]!=1&&f[v.x][v.y]!=3&&(u.tmp+1<st[v.x][v.y])) { v.tmp = u.tmp+1; pq.push(v); st[v.x][v.y] = u.tmp+1; } } }}void bfs2(int x,int y){ queue<node> pq; node u,v; u.x = x,u.y = y; u.tmp = 0; pq.push(u); ed[x][y] = 0; while(!pq.empty()) { node u = pq.front();pq.pop(); for(int i = 0;i < 4;i++) { node v; v.x = u.x+dx[i]; v.y = u.y+dy[i]; if(0<=v.x&&v.x<n&&v.y<m&&v.y>=0&&f[v.x][v.y]!=1&&(u.tmp+1<ed[v.x][v.y])) { v.tmp = u.tmp+1; pq.push(v); ed[v.x][v.y] = u.tmp+1; } } }}int main(){ scanf("%d%d",&m,&n); int x2,y2,x3,y3; for(int i = 0;i < n;i++) { for(int j = 0;j < m;j++) { scanf("%d",&f[i][j]); if(f[i][j] == 2) { x2 = i;y2 = j; } if(f[i][j] == 3) { x3 = i;y3 = j; } } } for(int i = 0;i < n;i++) for(int j = 0;j < m;j++) { st[i][j] = INF; ed[i][j] = INF; } bfs1(x2,y2); bfs2(x3,y3); int minn = INF; for(int i = 0;i < n;i++) { for(int j = 0;j < m;j++) { if(f[i][j] == 4) { minn = min(st[i][j]+ed[i][j],minn); } } } printf("%d\n",minn);}
0 0
- poj 3170
- poj 3170
- POJ 3170 BFS 水题
- POJ 3170--双重BFS
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- [038]如何选择机器学习分类器?
- View
- HDU3652 B-number(数位DP)
- java web项目中的properties文件中的属性值在启动后进行修改(数据库密码加密等)
- volatile
- poj 3170
- len,counta,& “”连接符号,字符串长度,非空单元格数量
- 带着O_CREAT和O_RDWR进入linux系统调用open函数
- boost::xtime和::xtime会冲突
- Fragment做的导航滑动
- XListView上拉加载,下拉刷新
- Unix网络编程之socket编程(一)--socket入门简单程序
- Java命名规则
- shell-多个空格替换成一个逗号怎么写表达式