Sicily 1889. Max’s game
来源:互联网 发布:淘宝个人资料在哪里 编辑:程序博客网 时间:2024/06/04 18:21
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 12 2@@@#0 1 1 0
Sample Output
20
第一拿到就感觉和那道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 0x7f7f7fchar 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;}
- Sicily 1889. Max’s game
- Sicily 1889. Max’s game (优先队列dijkstra、哈希)
- 1889. Max’s game
- Sicily 1796. Max's kingdom
- Sicily 1796. Max's kingdom
- Sicily 1796. Max's kingdom
- Sicily 1634. Relax! It's just a game
- Sicily 1282. Computer Game
- Sicily 4422. Boat Game
- sicily--1282. Computer Game
- Sicily 4422. Boat Game
- sicily 1087.Funny game
- Sicily: Candy Sharing Game
- Sicily 4422. Boat Game
- Sicily 1282 Computer Game
- Sicily 1900. Word Game
- Sicily 1737. Funny Game
- Sicily 1053. Peg Game
- 题目 1049 字符串去特定字符 九度Online Judge
- 黑马程序员——Java概述、环境配置
- 第三个第三个
- POJ2769(同余 + 暴力)
- 今天才发现
- Sicily 1889. Max’s game
- Ajax原理
- 第四届蓝桥杯预赛:第39台阶
- 新手如何使用eclipse编写Java(一)
- Sicily 1135. 飞越原野
- poj1125 floyd
- Sicily 1090. Highways
- cocos3——9.AssetsManager动态更新配置文件详解
- Sicily 1133. SPAM