24h購物| | PChome| 登入
2013-06-04 08:30:01| 人氣402| 回應0 | 上一篇 | 下一篇

[UVA][最短路] 11573 - Ocean Currents

推薦 0 收藏 0 轉貼0 訂閱站台

Problem B: Ocean Currents

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 Specification

The lake is represented as a rectangular grid. The first line of input 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.

Sample Input

5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4

Output Specification

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.

Output for Sample Input

0
2
1

Ondřej Lhoták, Richard Peng

題目描述;
盤面上有海流,而被海流沖向的方位取決於當前那格的數字。
如果不順著海流走都會消耗 1 能量,問最少的能量從起點到終點。

基本上就是 priority_queue, 加了一個估價函數, 但只是希望在相同的時候以
最接近的那個抓出來拓展。
如果用 spfa 的話可能會更新所有盤面,不知成效如何。


#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
char g[1005][1005];
short used[1005][1005];
short dist[1005][1005];
int n, m;
struct E {
    short x, y, d, h;
    E(short a, short b, short c, short dd):
        x(a), y(b), d(c), h(dd){}
    bool operator<(const E &a) const {
        if(d != a.d)
            return d > a.d;
        return h > a.h;
    }
};
priority_queue<E, vector<E> > pQ;
void sol(int sx, int sy, int ex, int ey) {
    static int times = 0;
    times++;
    while(!pQ.empty())  pQ.pop();
    E tn(0,0,0,0);
    pQ.push(E(sx, sy, 0, ex-sx+ey-sy));
    int dx[] = {-1,-1,0,1,1,1,0,-1};
    int dy[] = {0,1,1,1,0,-1,-1,-1};
    int i, cost, tx, ty, dir;
    used[sx][sy] = times;
    dist[sx][sy] = 0;
    while(!pQ.empty()) {
        tn = pQ.top(), pQ.pop();
        if(tn.x == ex && tn.y == ey) {
            printf("%d\n", tn.d);
            break;
        }
        dir = g[tn.x][tn.y] -'0';
        short &o = dist[tn.x][tn.y];
        for(i = 0; i < 8; i++) {
            tx = tn.x + dx[i], ty = tn.y + dy[i];
            if(tx < 0 || tx >= n || ty < 0 || ty >= m)
                continue;
            cost = 0;
            if(dir != i)
                cost = 1;
            if(dist[tx][ty] > o+cost || used[tx][ty] != times) {
                used[tx][ty] = times;
                dist[tx][ty] = o+cost;
                pQ.push(E(tx, ty, dist[tx][ty], ex-tx+ey-ty));
            }
        }
    }
}
int main() {
    int i, q, sx, sy, ex, ey;
    while(scanf("%d %d", &n, &m) == 2) {
        while(getchar() != '\n');
        for(i = 0; i < n; i++)
            gets(g[i]);
        scanf("%d", &q);
        while(q--) {
            scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
            sx--, sy--, ex--, ey--;
            sol(sx, sy, ex, ey);
        }
    }
    return 0;
}

台長: Morris
人氣(402) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][math] 911 - Multinomial Coefficients
此分類上一篇:[UVA][dfs、剪枝] 10890 - Maze

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文