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);

}

}

}

}

时间: 2024-08-24 20:49:20

kattis-Ocean Currents的相关文章

UVA 11573 - Ocean Currents(BFS+优先队列)

UVA 11573 - Ocean Currents 题目链接 题意:给定一个海面,数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量.每次询问给一个起点一个终点.问起点到终点耗费的最小能量 思路:广搜,队列用优先队列.每次取能量最低的点出来进行状态的转移 代码: #include <cstdio> #include <cstring> #include <queue> using namespace std; const int d[8][2] = {{-1

hdu 2757 Ocean Currents【广度优先搜索】

Ocean Currents Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1561    Accepted Submission(s): 516 Problem Description For a boat on a large body of water, strong currents can be dangerous, but

UVA 11573 - Ocean Currents【BFS+优先队列】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2620 题意:给定一个海面,数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量,每次询问给一个起点一个终点,问起点到终点耗费的最小能量 思路:广搜,队列用优先队列,每次取能量最低的点. 代码: #include <stdio.h> #include <

【HDOJ】2757 Ocean Currents

简单BFS. 1 /* 2757 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 using namespace std; 8 9 #define MAXN 1005 10 11 typedef struct node_t { 12 int k, t; 13 node_t() {}

Organic Solar Cells - Theory and Practice

renewable engergy: Wind solar Tidal Wave Ocean Bio Etc. How much energy we gonna need as following years: The unit is of Tera Watts. And how much we can get from the surroundings: theoretically Much of the wind energy are coming from the center of oc

HDU 专题分类

[背包问题] 2602 Bone Collector 1114 Piggy-Bank 1203 I NEED A OFFER! 1171 Big Event in HDU 1059 Dividing 2844 Coins 2191 悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 2159 FATE 1561 The more, The Better 1011 Starship Troopers 2639 Bone Collector II 3033 I love sneakers! 2955

github student pack中的digital ocean可以使用银联卡支付

申请了 github student pack却因为一直没有visita信用卡,而无法使用digital ocean的 $50,一直到今天,用中国银行借记卡成功支付. 方法是: (1)注册paypal账号,不需要绑定银行卡或信用卡. (2)打开digital ocean的welcome页面,那里会提示需要打入$5,支付方式可选“信用卡/借记卡”或者 “paypal”,选择paypal (3)进入支付页面,登陆paypal账户,输入银联卡(我的是中国银行)的卡号,然后填入后边的一些相关信息.点击“

Kattis - Association for Computing Machinery

Association for Computing Machinery ACM (Association for Computing Machinery) organizes the International Collegiate Programming Contest (ICPC) worldwide every year. In the ICPC, a team of three students is presented with a problem set that contains 

Kattis downtime

链接:https://open.kattis.com/problems/downtime 题意:n个要解决的进程,每个服务器可以解决k个进程,每个进程花费1000MS,给出n个进程的开始时间,问最少要几个服务器 思路:我们可以求出每个进程的区间,然后看我们可以不重叠的解决多少个进程 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 struct node{ 5 int x; 6 int y; 7 }a[200005]; 8 bool cm