Sicily 1722. Hero

1722. Hero

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

The beautiful princess is caught by the demon again. Super Mario, her lover, has to take the responsibility to be the hero. The princess is incarcerated in a horrific castle which is smelly and full of monsters. She is very scared. Super Mario is very worried about the princess and wants to see the princess as soon as possible. Though he runs fast, he still wants to know how fast it will take to reach the place where the princess is.
The castle is a N*M matrix. Initially Mario and princess are located in different positions. Mario can move up, down, left, right, one step per second. Specially, there are some springs distributed in the castle (Don’t ask why, maybe the demon likes to play spring.^_^). These springs are supernatural and may be used as a tool to help Mario if used properly. Each of the spring has an attribute ?? spring power which is an integer number actually. When Mario enters a grid where there is a spring whose spring power is k, he will be sprung k grids following the direction he enters the grid in no time.


For example, supposed Mario is in (2,8), and there is a spring in grid (2,7) with 5 spring power. If Mario goes left one step, he will be sprung 5 grids left. So the final position of Mario is (2,2) and the time from (2,8) to (2,2) for Mario is just one second. 
Note that if Mario is sprung outside the matrix, he will be stopped by the wall of the castle and drop on the grid beside the wall. For example, supposed Mario is in (2,8), and there is a spring in grid (2,7) with 10 spring power. If Mario goes left one step, he will just sprung 6 grids left, then stop and drop in grid (2,1).
Moreover, if the position where Mario will land has a spring, he can be sprung again. That means Mario can be sprung consecutively until he lands on a grid without spring. And only the landing position may affect Mario’s action, all grids that Mario passes by when he is sprung have nothing to do with him. For example, supposed Mario is in (2,8) and there are two grid have springs – (2,7) and (2,5), both are 5 spring power. If Mario steps left, he will be sprung to position (2,2). So the spring in (2,5) does not work on him.
It is your task to tell Mario the minimum time to reach the princess. 

Input

Input contains several test cases and is terminated by EOF.
Each case begins with one line containing two integers, n and m (3 <= n,m <= 100), which are the row number and column number of the castle. Then follows a non-negative integer number k, indicating the number of springs. Then follows k lines, each line has three positive integers - x,y, and p, x,y is the coordinate of the spring (2<= x <=n-1,2 <= y <= m-1) and p is the spring power described above. These are followed by 2 lines lastly, the first line contains two integers, which represent the coordinate of Mario, and the other line is the coordinate of the princess.
You can assume all of the cases are legal. The initial positions of Mario, princess, and springs are distinct. All of the x coordinates are in the range of [1, n] and all of the y coordinates are in the range of [1, m]. And no spring is next to the wall of the castle.

Output

For each test case, just output one line presenting the minimum time Mario will spend. If Mario can not save the princess (Oh, my god*_* but it may happen), just print “Impossible“ in one line(without quotation marks) .

Sample Input

10 10
0
2 2
7 8
10 10
1
2 7 5
2 8
1 1
10 10
1
2 7 10
2 8 
1 1
10 10
2
2 7 5
2 5 5
2 8
1 1
10 10
4
7 9 2
7 7 2
6 8 2
8 8 2
2 2 
7 8

Sample Output

11
3
2
3

Impossible

// Problem#: 1722
// Submission#: 3585370
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;

bool vis[105][105][4];
int G[105][105];
int h, w;
int si, sj, ei, ej;
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};

struct step {
    int i, j, s;
    step(int ii = 0, int jj = 0, int ss = 0) {
        i = ii;
        j = jj;
        s = ss;
    }
};

inline bool isValid(int i, int j, int dir) {
    if (!(1 <= i && i <= h && 1 <= j && j <= w)) return false;
    if (G[i][j] == 0 && vis[i][j][0]) return false;
    if (G[i][j] && vis[i][j][dir]) return false;
    return true;
}

void BFS() {
    queue<step> q;
    memset(vis, false, sizeof(vis));
    vis[si][sj][0] = true;
    q.push(step(si, sj, 0));
    int ans = 88888888;
    while (!q.empty()) {
        step t = q.front();
        //printf("---%d %d---\n", t.i, t.j);
        q.pop();
        if (t.i == ei && t.j == ej) {
            if (ans > t.s) ans = t.s;
        }
        for (int i = 0; i < 4; i++) {
            int ni = t.i + dir[i][0];
            int nj = t.j + dir[i][1];
            if (isValid(ni, nj, i)) {
                if (G[ni][nj] == 0) {
                    q.push(step(ni, nj, t.s + 1));
                    vis[ni][nj][0] = true;
                } else {
                    while (G[ni][nj] && !vis[ni][nj][i]) {
                        vis[ni][nj][i] = true;
                        if (i == 0) {
                            nj += G[ni][nj];
                            if (nj >= w) nj = w;
                        } else if (i == 1) {
                            ni += G[ni][nj];
                            if (ni >= h) ni = h;
                        } else if (i == 2) {
                            nj -= G[ni][nj];
                            if (nj <= 1) nj = 1;
                        } else {
                            ni -= G[ni][nj];
                            if (ni <= 1) ni = 1;
                        }
                    }
                    q.push(step(ni, nj, t.s + 1));
                    vis[ni][nj][0] = true;
                }
            }
        }
    }
    if (ans == 88888888) printf("Impossible\n");
    else printf("%d\n", ans);
}


int main() {
    while (scanf("%d%d", &h, &w) != EOF) {
        memset(G, 0, sizeof(G));
        int k;
        scanf("%d", &k);
        while (k--) {
            int i, j;
            scanf("%d%d", &i, &j);
            scanf("%d", &G[i][j]);
        }
        scanf("%d%d%d%d", &si, &sj, &ei, &ej);
        BFS();
    }
    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值