hdu 2757(优先队列+广搜)
2013-03-20 19:27
232 查看
Ocean Currents
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1148 Accepted Submission(s): 382
Problem Description
For a boat on a large body of water, strong currents can be dangerous, but with careful planning, they can be harnessed to help the boat reach its destination. Your job is to help in that planning.
At each location, the current flows in some direction. The captain can choose to either go with the flow of the current, using no energy, or to move one square in any other direction, at the cost of one energy unit. The boat always moves in one of the following eight directions: north, south, east, west, northeast, northwest, southeast, southwest. The boat cannot leave the boundary of the lake. You are to help him devise a strategy to reach the destination with the minimum energy consumption.
Input
The lake is represented as a rectangular grid. The first line of each test chunk contains two integers r and c, the number of rows and columns in the grid. The grid has no more than 1000 rows and no more than 1000 columns. Each of the following r lines contains exactly c characters, each a digit from 0 to 7 inclusive. The character 0 means the current flows north (i.e. up in the grid, in the direction of decreasing row number), 1 means it flows northeast, 2 means east (i.e. in the direction of increasing column number), 3 means southeast, and so on in a clockwise manner:
7 0 1
\|/
6-*-2
/|\
5 4 3
The line after the grid contains a single integer n, the number of trips to be made, which is at most 50. Each of the following n lines describes a trip using four integers rs, cs, rd, cd, giving the row and column of the starting point and the destination of the trip. Rows and columns are numbered starting with 1.
Please process to the end of the data file.
Output
For each trip, output a line containing a single integer, the minimum number of energy units needed to get from the starting point to the destination.
Sample Input
5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4
5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4
Sample Output
0
2
1
0
2
1
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1148 Accepted Submission(s): 382
Problem Description
For a boat on a large body of water, strong currents can be dangerous, but with careful planning, they can be harnessed to help the boat reach its destination. Your job is to help in that planning.
At each location, the current flows in some direction. The captain can choose to either go with the flow of the current, using no energy, or to move one square in any other direction, at the cost of one energy unit. The boat always moves in one of the following eight directions: north, south, east, west, northeast, northwest, southeast, southwest. The boat cannot leave the boundary of the lake. You are to help him devise a strategy to reach the destination with the minimum energy consumption.
Input
The lake is represented as a rectangular grid. The first line of each test chunk contains two integers r and c, the number of rows and columns in the grid. The grid has no more than 1000 rows and no more than 1000 columns. Each of the following r lines contains exactly c characters, each a digit from 0 to 7 inclusive. The character 0 means the current flows north (i.e. up in the grid, in the direction of decreasing row number), 1 means it flows northeast, 2 means east (i.e. in the direction of increasing column number), 3 means southeast, and so on in a clockwise manner:
7 0 1
\|/
6-*-2
/|\
5 4 3
The line after the grid contains a single integer n, the number of trips to be made, which is at most 50. Each of the following n lines describes a trip using four integers rs, cs, rd, cd, giving the row and column of the starting point and the destination of the trip. Rows and columns are numbered starting with 1.
Please process to the end of the data file.
Output
For each trip, output a line containing a single integer, the minimum number of energy units needed to get from the starting point to the destination.
Sample Input
5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4
5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4
Sample Output
0
2
1
0
2
1
#include<cstdio> #include<cstring> #include<queue> using namespace std; #define inf 8000 #define clr(x)memset(x, inf, sizeof(x)); int f[16] = {-1,-1,0,1,1,1,0,-1,0,1,1,1,0,-1,-1,-1}; typedef struct Node { int x; int y; int step; friend bool operator < (Node a, Node b) { return a.step > b.step; } }QNd; QNd st, nx, tmp, in; //bool cal() char s[1001]; int step[1001][1001]; int a[1001][1001]; int main(void) { int r; int c; while (scanf("%d%d", &r, &c) != EOF) { int i; int j; for (i=1; i<=r; i++) { scanf("%s", s); for (j=0; j<c; j++) { a[i][j+1] = s[j] - '0'; } } int n; scanf("%d", &n); priority_queue<QNd>que; while (n--) { scanf("%d%d%d%d", &st.x, &st.y, &nx.x, &nx.y); clr(step); st.step = 0; step[st.x][st.y] = 0; que.push(st); while (!que.empty()) { tmp = que.top(); if (tmp.x==nx.x && tmp.y==nx.y) { printf("%d\n", step[tmp.x][tmp.y]); break; } for (i=0; i<8; i++) { in.x = f[i] + tmp.x; in.y = f[i+8] + tmp.y; if (in.x>=1 && in.x<=r && in.y >=1 && in.y<=c && step[in.x][in.y]>step[tmp.x][tmp.y]+(i!=a[tmp.x][tmp.y])) { step[in.x][in.y] = step[tmp.x][tmp.y] + (i!=a[tmp.x][tmp.y]); in.step = step[in.x][in.y]; que.push(in); } } que.pop(); } } } return 0; }
相关文章推荐
- hdu 2757 广搜+优先队列
- HDU 2757 优先队列+广搜
- hdu 2757 Ocean Currents(优先队列+bfs)
- HDU 2757 Ocean Currents[优先队列+广搜]
- hdu 2757 Ocean Currents(优先队列+bfs)
- HDU 1026 Ignatius and the Princess I (优先队列+BFS(广度优先搜索))
- HDU 2850 Load Balancing (贪心+优先队列)
- HDU:4006 The kth great number(优先队列)
- hdu-5360Hiking(优先队列+贪心)
- hdu 5521 Meeting(优先队列+dijkstra)(建图思想)
- 【hdu 5350 2015多校赛】MZL's munhaff function (哈夫曼数,优先队列)
- hdu 2112 最短路 dijkstra优先队列
- HDU_4006_The kth great number_优先队列
- hdu 1509 Windows Message Queue (优先队列)
- HDU 3442 Three Kingdoms BFS + 优先队列
- HDU 1026 Ignatius and the Princess I(广搜+路径记录+优先队列)
- hdu 2757 单调优先队列 bfs
- [HDU] 1242 Rescue -不使用优先队列来找寻最短路径
- hdu 4393 优先队列
- hdu 2757 DNA repair AC自动机dp--------完全不懂--------