hdu 4198:Quick out of the Harbour解题报告

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

  

时间: 2024-10-15 18:25:42

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

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 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 mo

HDU 4198 Quick out of the Harbour(优先队列 + bfs)

解题思路: 直接的bfs,因为和时间有关,需要采用优先队列. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include

hdu - 5349 MZL&#39;s simple problem(解题报告)

A - MZL's simple problem Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number

hdu 1166(敌兵布阵) 解题报告

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 46897    Accepted Submission(s): 19876 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务

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

Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16281 Accepted Submission(s): 7206 Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了

hdu 4956 Poor Hanamichi 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956(它放在题库后面的格式有一点点问题啦,所以就把它粘下来,方便读者观看) 题目意思:给出一个范围 [l, r] 你, 问是否能从中找到一个数证明 Hanamichi’s solution 的解法是错的. Hanamichi’s solution 是这样的: 对于某个数 X,从右往左数它的每一位数字(假设第一位是从0开始数).它 偶数位的数字之和 -  奇数位的数字之和  = 3  而且 这个 X

Valentine&#39;s Day Round 1001.Ferries Wheel(hdu 5174)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5174 题目意思:给出 n 个人坐的缆车值,假设有 k 个缆车,缆车值 A[i] 需要满足:A[i−1]<A[i]<A[i+1](1<i<K).现在要求的是,有多少人满足,(他坐的缆车的值 + 他左边缆车的值) % INT_MAX == 他右边缆车的值. 首先好感谢出题者的样例三,否则真的会坑下不少人.即同一部缆车可以坐多个人.由于缆车的值是唯一的,所以可以通过排序先排出缆车的位置.求出

hdu 1166 敌兵布阵 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目意思:给出 N 个数你,通过对某些数进行更改(或者 + 或者 -),当输入的是 Query 的时候,需要计算出 某个区间的和. 树状数组第一题,算是模板吧 ^_^ 有个小细节,wa 了几次,细心~细心~~~细心 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <