Sicily 1889. Max’s game

1889. Max’s game

Constraints

Time Limit: 3 secs, Memory Limit: 32 MB

Description

Max is lately playing a game. Given a n*m board, there are only two types of grid on it: # and @. And the rule is simple: Given a start position to Max, and Max can move to four direction for each step, up, down, left, right. When moving between two same type grids, it costs zero, otherwise, it costs one. Now, Max gets two different positions on the board: the start position and the target position, he wants to know how many costs he need to spend to achieve the game at least. 

Input

Input contains multiple test data sets.
For each data set, first comes two integers in one line: n, m (1 < = n, m <= 500), stands for the number of row and the number of column of the board. Then comes n lines with m grid characters per each. The characters are only @ or #. Then four integers in one line follow: x1, y1, x2, y2 ( 0<= x1, x2 < n, 0<= y1, y2 < m) which is the start position and the end position correspondingly. 
Input is terminated by EOF. 

Output

For each test data set, one output data set should be generated as follow:
Generates one line containing one integer which indicates the minimum cost Max need to take for the game. 

Sample Input

2 2
@#
#@
0 0 1 1
2 2
@@
@#
0 1 1 0

Sample Output

2
0

第一拿到就感觉和那道robot很像,大数据,用Dijkstra加优先队列解决,刚开始没有在枚举到达点的限制条件里加i < 4,导致了超时,然后加了就好了,0.9s- -:

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define MAX 505
#define INF 0x7f7f7f

char map[MAX][MAX];
int si, sj, ei, ej, h, w;

int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
bool vis[MAX * MAX];

bool is_valid(int ii, int jj) {
    if (ii < 0 || ii >= h || jj < 0 || jj >= w || vis[ii * w + jj])
        return false;
    return true;
}

struct step {
    int to;
    int cost;
};

step p[MAX * MAX][4];

void make_map() {
    memset(vis, false, sizeof(vis));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int temp = i * w + j;
            int oo = 0;
            for (int k = 0; k < 4; k++) {
                if (is_valid(i + dir[k][0], j + dir[k][1])) {
                    int c = (map[i][j] == map[i + dir[k][0]][j + dir[k][1]]) ? 0 : 1;
                    p[temp][oo].to = (i + dir[k][0]) * w + j + dir[k][1];
                    p[temp][oo++].cost = c;
                    //p[temp].push_back(step((i + dir[k][0]) * w + j + dir[k][1], c));
                    //printf("%d\n", p[temp][0].to);
                }
            }
            p[temp][oo].to = -1;
        }
    }
}

typedef pair <int, int> pr;

int Dijkstra() {
    
    int d[MAX * MAX];
    memset(d, INF, sizeof(d));
    d[si * w + sj] = 0;
    priority_queue<pr, vector<pr>, greater<pr> > q;
    q.push(pr(0, si * w + sj));
    pr top;
    
    while (!q.empty()) {
        top = q.top();
        q.pop();
        
        if (top.second == ei * w + ej)
            return d[top.second];
        
        if (vis[top.second] || d[top.second] < top.first)
            continue;
        
        vis[top.second] = true;
        
        for (int i = 0; i < 4 && p[top.second][i].to != -1; i++) {
            if (d[p[top.second][i].to] > d[top.second] + p[top.second][i].cost) {
                d[p[top.second][i].to] = d[top.second] + p[top.second][i].cost;
                q.push(pr(d[p[top.second][i].to], p[top.second][i].to));
            }
        }
    }
}
    
int main() {
    while (~scanf("%d %d\n", &h, &w)) {
        for (int i = 0; i < h; i++) {
            gets(map[i]);
        }
        make_map();
        scanf("%d %d %d %d", &si, &sj, &ei, &ej);
        printf("%d\n", Dijkstra());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值