zoukankan      html  css  js  c++  java
  • hdoj_4198Quick 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): 692    Accepted Submission(s): 306


    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

    #include <cstring>
    #include <queue>
    #include <cstdio>
    #include <iostream>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 505;
    char maps[MAXN][MAXN];
    bool visited[MAXN][MAXN];
    int moves[4][2] = {-1,0,1,0,0,-1,0,1};
    int m, n, d;
    
    typedef struct Point
    {
    	int x;
    	int y;
    	int step;
    	bool operator < (const Point &p) const
    	{
    		return step > p.step;
    	}
    }Point;
    priority_queue<Point>Q;
    
    int BFS(int x, int y)
    {
    	while(!Q.empty())
    	{
    		Q.pop();
    	}
    	Point pre, next;
    	pre.x = x;
    	pre.y = y;
    	pre.step = 0;
    	Q.push(pre);
    	visited[pre.x][pre.y] = true;
    	while(!Q.empty())
    	{
    		pre = Q.top();
    		Q.pop();
    		if(pre.x == 0 || pre.y == 0 || pre.x == n - 1 || pre.y == m - 1) 
    		{
    			pre.step++;
    			return pre.step;
    		}
    		for(int i = 0; i < 4; i++)
    		{
    			next.x = pre.x + moves[i][0];
    			next.y = pre.y + moves[i][1];
    			if(next.x >= 0 &&next.x < n && next.y >= 0 && next.y < m && !visited[next.x][next.y] && maps[next.x][next.y] != '#')
    			{
    				visited[next.x][next.y] = true;
    				if(maps[next.x][next.y] == '.')
    				{
    					next.step = pre.step + 1;
    				}
    				else
    				{
    					next.step = pre.step + d + 1;
    				}
    				Q.push(next);
    			}
    		}
    	}
    	return 0;
    }
    
    int main()
    {
    	freopen("in.txt","r",stdin);
    	int t, x, y;
    	scanf("%d", &t);
    	while (t--)
    	{
    		scanf("%d %d %d", &n, &m, &d);
    		for (int i = 0; i < n; i++)
    		{
    			scanf("%s", maps[i]);
    		}
    		for (int i = 0 ; i < n; i++)
    		{
    			for(int j = 0; j < m; j++)
    			{
    				if(maps[i][j] == 'S')
    				{
    					x = i;
    					y = j;
    					break;
    				}
    			}
    		}
    		memset(visited, false, sizeof(visited));
    		printf("%d\n", BFS(x, y));
    	}
    	return 0;
    }

     
  • 相关阅读:
    迷你资源管理器
    简单工厂和单例的一些事
    面向对象的七大原则
    继承和多态的一些事
    体检套餐系统
    信仰
    魔兽争霸系统
    优化MySchool总结习题
    事务,视图及索引
    [LeetCode] Combinations
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834975.html
Copyright © 2011-2022 走看看