kattis-Ocean Currents

Posted Library of stultus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kattis-Ocean Currents相关的知识,希望对你有一定的参考价值。

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<?XML:NAMESPACE PREFIX = "[default] http://www.w3.org/1998/Math/MathML" NS = "http://www.w3.org/1998/Math/MathML" />r and cc, 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 rr lines contains exactly cc 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 nn, the number of trips to be made, which is at most 50. Each of the following nn lines describes a trip using four integers rsrs, cscs, rdrd, cdcd, giving the row and column of the starting point and the destination of the trip. Rows and columns are numbered starting with 1.

The line after the grid contains a single integer nn, the number of trips to be made, which is at most 50. Each of the following nn lines describes a trip using four integers rsrs, cscs, rdrd, cdcd, giving the row and column of the starting point and the destination of the trip. Rows and columns are numbered starting with 1.

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.

Sample Input 1
Sample Output 1

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

又是kattis系列题目,这道题前提如下:

  有一张图,这张图上每一个点上都有一个方向,若站在该点上向该方向移动,则不需要花费,不然则需要一点花费,现在给你两个点,要求找出两点之间花费最小的路径。

  上手进行分析,就会发现如果直接暴力的话肯定不行,因为路径的选择每一步就有8种,那最大可以达到8^1000次,那么需要一个规律,或者优化的方法。

  然后想到对于一个起点来说,花费为0的路径一定是优先的,而且所有花费为1的路径都是在花费为0的基础上达到的,以后的花费同理,那么每一次整理出来一个花费为当前最小的路径,查看是否为目标点,不是则更新周围点状态,继续做,这看起来似乎可行,虽然说找不到明显的根据,那瞎凑一发也好。

  然后就有了这个代码:

#include<stdio.h>

#include<string.h>

#include<iostream>

#include<vector>

#include<algorithm>

#include<string>

#include<queue>

#define size_ 3000

#define math 1000

using namespace std;

int dc[8] = {0,1,1,1,0,-1,-1,-1};

int dr[8] = {-1,-1,0,1,1,1,0,-1};

int vst[1005][1005] = { 0 };

int sto[1005][1005] = { 0 };

struct vert

{

int row;

int col;

friend bool operator ==(vert a,vert b)

{

if (a.col == b.col&&a.row == b.row) return 1;

else return 0;

};

};

struct path

{

int cos;

vert now;

friend bool operator <(path a, path b)

{

return a.cos > b.cos;

}

};

int check(path top, int col, int row)

{

if (top.now.col > col || top.now.col <= 0 || top.now.row > row || top.now.row <= 0||vst[top.now.row-1][top.now.col - 1])

return 0;

else

return 1;

}

int main(void)

{

int col, row;

cin >> col >> row;

for (int i = 0; i < row; i++)

{

string temp;

cin >> temp;

for (int j = 0; j < col; j++)

{

sto[i][j] = temp[j] - ‘0‘;

}

}

int n;

cin >> n;

for (int i = 0; i < n; i++)

{

vert s, d;

scanf("%d %d %d %d", &s.row, &s.col, &d.row, &d.col);

priority_queue<path>Q;

memset(vst, 0, sizeof(vst));

path temp;

temp.now = s;//intialization

temp.cos = 0;

Q.push(temp);

while (1)

{

if (Q.top().now == d)

{

cout << Q.top().cos<<endl;

/* for (int i = 0; i < Q.top().res.size(); i++)

cout << Q.top().res[i].row << " " << Q.top().res[i].col << endl;*/

//cout << endl;

break;

}

path top = Q.top();

vst[top.now.row - 1][top.now.col - 1] = 1;

Q.pop();

for (int i = 0; i < 8; i++)

{

path temp;

temp.cos = top.cos;

//temp.res = top.res;

temp.now = top.now;//copy

//temp.res.push_back(temp.now);//record verticle

temp.now.col += dc[i];//update

temp.now.row += dr[i];

if (!check(temp, col, row))continue;//check if over the bound

if (sto[top.now.row-1][top.now.col-1] != i)

temp.cos++;

Q.push(temp);

}

}

}

}

以上是关于kattis-Ocean Currents的主要内容,如果未能解决你的问题,请参考以下文章

本周最佳 Tame Impala [Currents]

TAME IMPALA 的“'CAUSE I'M A MAN”带你在 3D 世界里漫游

使用 DKIM 密钥在 phpmailer 中发送邮件

Progressbar WebView 类似 Chrome 浏览器

微信小程序代码片段

VSCode自定义代码片段——CSS选择器