Quick out of the Harbour

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1441    Accepted Submission(s): 575

Problem Description
Captain Clearbeard decided to go to the harbour for a few days so his crew could inspect and repair the ship. Now, a few days later, the pirates are getting landsick(Pirates get landsick when they don't get enough of the ships' rocking motion. That's why pirates often try to simulate that motion by drinking rum.). Before all of the pirates become too sick to row the boat out of the harbour, captain Clearbeard decided to leave the harbour as quickly as possible.
Unfortunately the harbour isn't just a straight path to open sea. To protect the city from evil pirates, the entrance of the harbour is a kind of maze with drawbridges in it. Every bridge takes some time to open, so it could be faster to take a detour. Your task is to help captain Clearbeard and the fastest way out to open sea.
The pirates will row as fast as one minute per grid cell on the map. The ship can move only horizontally or vertically on the map. Making a 90 degree turn does not take any extra time.

Input
The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:
1. One line with three integers, h, w (3 <= h;w <= 500), and d (0 <= d <= 50), the height and width of the map and the delay for opening a bridge.
2.h lines with w characters: the description of the map. The map is described using the following characters:
—"S", the starting position of the ship.
—".", water.
—"#", land.
—"@", a drawbridge.
Each harbour is completely surrounded with land, with exception of the single entrance.

Output
For every test case in the input, the output should contain one integer on a single line: the travelling time of the fastest route to open sea. There is always a route to open sea. Note that the open sea is not shown on the map, so you need to move outside of the map to reach open sea.

Sample Input
2
6 5 7
#####
#S..#
#@#.#
#...#
#@###
#.###
4 5 3
#####
#S#.#
#@..#
###@#

Sample Output
16
11

这个题大概的意思就是一艘船要从S点出发,到达唯一出口的最短时间,只能走'.'(有水的地方)或者'@'标记的地方,不能走'#'标记的地方,因为有'.'花费的时间为1,'@'花费的时间为第三个参数c+1,所以考虑优先队列。

下面是暑假培训c++代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
char mp[502][502];
int vis[502][502];
int dir[][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int n,m,c,ex,ey;
struct node
{
int x,y,step;
};
bool operator < (node t1,node t2)
{
return t1.step>t2.step;
}
priority_queue<node>q;
int bfs()
{
node t,next;
int tx;
while(!q.empty())
{
t=q.top();
q.pop();
if(t.x==ex&&t.y==ey) return t.step;
for(int i=0; i<4; i++)
{
next.step=t.step;
next.x=t.x+dir[i][0];
next.y=t.y+dir[i][1];
if(next.x<0||next.x>=n||next.y<0||next.y>=m||vis[next.x][next.y]||mp[next.x][next.y]=='#')
continue;
if(mp[next.x][next.y]=='@')
next.step+=(c+1);
if(mp[next.x][next.y]=='.')
next.step++;
vis[next.x][next.y]=1;
q.push(next);
}
}
return 0; }
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
node t;
memset(mp,0,sizeof(mp));
memset(vis,0,sizeof(vis));
while(!q.empty()) q.pop();
scanf("%d%d%d",&n,&m,&c);
for(int i=0; i<n; i++)
{
scanf("%s",mp[i]);
for(int j=0; j<m; j++)
{
if(mp[i][j]=='S')
{
t.x=i,t.y=j,t.step=0;
}
if((i==0||i==n-1||j==0||j==m-1)&&mp[i][j]!='#')
{
ex=i,ey=j;
}
}
}
q.push(t);
vis[t.x][t.y]=1;
printf("%d\n",bfs()+1);
}
return 0;
}

  这里是最近写的JAVA代码(弄了好久,也积累了许多,发现class 和struct 不是一个东西,后面贴了个问题代码,有兴趣的朋友可以断点调试下,你会发现一个神奇的事情,也许是我觉得神奇吧,哈哈)有兴趣用JAVA写这种代码的人很少吧,我在网上搜都是用的c++,我就第一个吃螃蟹的人,也为以后需要的朋友提供个借鉴:

import java.util.PriorityQueue;
import java.util.Scanner; public class Main {
static class node implements Comparable<node>{
int x, y, step;
public node(){ }
public node(int x,int y,int step){
this.x=x;
this.y=y;
this.step=step;
}
@Override
public int compareTo(node o) {
if(this.step>o.step) return 1;
return -1;
}
} static int n, m, value;
static int startx, starty, endx, endy;
static char[][] map;
static boolean[][] visit;
static int dir[][] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }; public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tcase = sc.nextInt();
while (tcase-- > 0) {
n = sc.nextInt();
m = sc.nextInt();
value = sc.nextInt();
map = new char[n][m];
visit = new boolean[n][m];
for (int i = 0; i < n; i++) {
String str = sc.next();
map[i] = str.toCharArray();
for (int j = 0; j < m; j++) {
if (map[i][j] == 'S') {
startx = i;
starty = j;
}
if (map[i][j] != '#'
&& (i == 0 || i == n - 1 || j == 0 || j == m - 1)) {
endx = i;
endy = j;
}
}
}
System.out.println(BFS(startx,starty));
} } private static int BFS(int startx2, int starty2) {
PriorityQueue<node> q =new PriorityQueue<node>();
node t = new node(startx2,starty2,1); //step初始值为1,S点也要算进去
q.add(t);
visit[t.x][t.y]=true;
while(!q.isEmpty()){
t = q.remove();
if(t.x==endx&&t.y==endy) {
return t.step;
}
for(int i=0;i<4;i++){
int step = t.step;
int x = t.x+dir[i][0];
int y = t.y+dir[i][1];
if(x<0||x>n-1||y<0||y>m-1||visit[x][y]||map[x][y]=='#') continue;
if(map[x][y]=='@'){
step+=(value+1);
}
if(map[x][y]=='.'){
step++;
}
visit[x][y]=true;
q.add(new node(x,y,step));
}
}
return 0;
}
}

问题代码:

//问题代码
import java.util.PriorityQueue;
import java.util.Scanner; public class Main {
static class node implements Comparable<node>{
int x, y, step;
@Override
public int compareTo(node o) {
if(this.step>o.step) return 1;
return -1;
}
} static int n, m, value;
static int startx, starty, endx, endy;
static int[][] map;
static boolean[][] visit;
static int dir[][] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }; public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tcase = sc.nextInt();
while (tcase-- > 0) {
n = sc.nextInt();
m = sc.nextInt();
value = sc.nextInt();
map = new int[n][m];
visit = new boolean[n][m];
for (int i = 0; i < n; i++) {
String str = sc.next();
char[] x = str.toCharArray();
for (int j = 0; j < m; j++) {
if (x[j] == 'S') {
startx = i;
starty = j;
map[i][j] = 1;
}
if (x[j] == '@') {
map[i][j] = value+ 1;
}
if (x[j] == '#') {
map[i][j] = -1;
}
if (x[j] == '.') {
map[i][j] = 1;
}
if (x[j] != '#'
&& (i == 0 || i == n - 1 || j == 0 || j == m - 1)) {
endx = i;
endy = j;
map[i][j] = 0;
}
}
}
System.out.println(BFS(startx,starty));
} } private static int BFS(int startx2, int starty2) {
PriorityQueue<node> q =new PriorityQueue<node>();
node t = new node();
t.x=startx;
t.y=starty;
t.step = 0;
q.add(t);
visit[t.x][t.y]=true;
while(!q.isEmpty()){
t = q.remove();
//System.out.println(t.x+" "+t.y+" "+t.step);
if(t.x==endx&&t.y==endy) {
return t.step;
}
node t1 = new node();
for(int i=0;i<4;i++){
t1.step = t.step;
t1.x = t.x+dir[i][0];
t1.y = t.y+dir[i][1];
if(t1.x<0||t1.x>n-1||t1.y<0||t1.y>m-1||visit[t1.x][t1.y]||map[t1.x][t1.y]==-1) continue;
t1.step +=map[t1.x][t1.y];
//System.out.println("t1:"+t1.x+" "+t1.y+" "+t1.step);
visit[t1.x][t1.y]=true;
q.add(t1);
//System.out.println(q.peek().x+" "+q.peek().y+" "+q.peek().step);
}
}
return 0;
}
}

  

hdu 4198:Quick out of the Harbour解题报告的更多相关文章

  1. hdu 4198 Quick out of the Harbour

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4198 Quick out of the Harbour Description Captain Cle ...

  2. HDU - 4198 Quick out of the Harbour (BFS+优先队列)

    Description Captain Clearbeard decided to go to the harbour for a few days so his crew could inspect ...

  3. hdu 4198 Quick out of the Harbour(BFS+优先队列)

    题目链接:hdu4198 题目大意:求起点S到出口的最短花费,其中#为障碍物,无法通过,‘.’的花费为1 ,@的花费为d+1. 需注意起点S可能就是出口,因为没考虑到这个,导致WA很多次....... ...

  4. ACM:HDU 2199 Can you solve this equation? 解题报告 -二分、三分

    Can you solve this equation? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...

  5. HDU 4303 Hourai Jeweled 解题报告

    HDU 4303 Hourai Jeweled 解题报告 评测地址: http://acm.hdu.edu.cn/showproblem.php?pid=4303 评测地址: https://xoj. ...

  6. 【解题报告】 Leapin' Lizards HDU 2732 网络流

    [解题报告] Leapin' Lizards HDU 2732 网络流 题外话 在正式讲这个题目之前我想先说几件事 1. 如果大家要做网络流的题目,我在网上看到一个家伙,他那里列出了一堆网络流的题目, ...

  7. HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. 2014 ACM/ICPC 鞍山赛区现场赛 D&amp;I 解题报告

    鞍山现场赛结束了呢-- 我们出的是D+E+I三道题-- 吾辈AC掉的是D和I两道,趁着还记得.先在这里写一写我写的两道水题D&I的解题报告吧^_^. D题的意思呢是说星云内有一堆排成一条直线的 ...

  9. 【百度之星2014~初赛(第二轮)解题报告】Chess

    声明 笔者近期意外的发现 笔者的个人站点http://tiankonguse.com/ 的非常多文章被其他站点转载.可是转载时未声明文章来源或參考自 http://tiankonguse.com/ 站 ...

随机推荐

  1. Mac将应用拖入Finder工具栏

    在Finder的工具栏上放一下应用,方便打开对应的文件,可以 Command + 鼠标拖动应用,将应用拖入Finder工具栏中. 本人的Finder工具栏上添加了vscode这个应用

  2. [NOIP 2005] 运输计划

    link 这是一道假的图论 思维难度很低,代码量偏高 就是一道板子+二分 树上差分就AC了 注意卡常即可 二分枚举答案x,为时间长度 将每一个长度大于x的计划链长记录下来(有几个,总需要减少多少长度) ...

  3. BST POJ - 2309 思维题

    Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, ...

  4. bzoj1026 windy数 数位DP

    windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? Input 包含两个整数,A ...

  5. Beautiful Soup的一些中文资料

    如果你着急用的话,可以看下这个简略版的,非常简单: 转自  人世间http://rsj217.diandian.com/post/2012-11-01/40041235132 当然,强烈推荐你看一下的 ...

  6. centos7-每天定时备份 mysql数据库

    centos7-每天定时备份 mysql数据库 第一步:编写数据库备份脚本database_mysql_shell.sh #!/bin/bash DATE=`date +%Y%m%d%H%M` #ev ...

  7. 鸽巢排序Pigeonhole sort

    原理类似桶排序,同样需要一个很大的鸽巢[桶排序里管这个叫桶,名字无所谓了] 鸽巢其实就是数组啦,数组的索引位置就表示值,该索引位置的值表示出现次数,如果全部为1次或0次那就是桶排序 例如 var pi ...

  8. mysql 并发下数据不一致的问题分析及解决

    MySQL 5.6 , InnoDB存储引擎,默认事务隔离级别(REPEATABLE-READ) 初始sql 脚本如下: CREATE DEFINER=`root`@`localhost` PROCE ...

  9. Oracle 导出空表的新方法(彻底解决)

    背景 使用Exp命令在oracle 11g 以后不导出空表(rowcount=0),是最近在工作中遇到一个很坑的问题,甚至已经被坑了不止一次,所以这次痛定思痛,准备把这个问题彻底解决.之所以叫新方法, ...

  10. 海康解码器对接总结(java 版)

    本文只是对接海康解码器的动态解码功能,即配置解码器大屏上指定的某个窗口去解某一路IP视频源. 1. 首先,定义所需的结构体与接口.海康SDK中包含的结构体与接口非常之多,在官方的例子中,实现了大部分的 ...