首页 > 其他分享 >HDU 4198 Quick out of the Harbour

HDU 4198 Quick out of the Harbour

时间:2022-11-09 20:37:41浏览次数:41  
标签:map HDU int Harbour harbour mp sea Quick open


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<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,j,k) for (int i = j; i <= k; i++)
const int N = 5e2 + 10;
char s[N];
int mp[N][N],f[N][N];
int n, m, T, d;
int a[4]={0,0,1,-1};
int b[4]={1,-1,0,0};

struct point
{
int x,y;
point(int x=0,int y=0):x(x),y(y){}
bool operator<(const point a) const
{
return f[x][y]>f[a.x][a.y];
}
};

priority_queue<point> p;

int bfs()
{
while (!p.empty())
{
point q=p.top(); p.pop();
rep(i,0,3)
{
int x=q.x+a[i],y=q.y+b[i];
if (x<1||x>n||y<1||y>m) return f[q.x][q.y]+1;
if (mp[x][y]&&!f[x][y])
{
f[x][y]=f[q.x][q.y]+mp[x][y];
p.push(point(x,y));
}
}
}
}

int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d", &n, &m,&d);
memset(f,0,sizeof(f));
memset(mp,0,sizeof(mp));
rep(i,1,n)
{
scanf("%s",s+1);
rep(j,1,m)
{
if (s[j]=='S') p.push(point(i,j));
if (s[j]=='.') mp[i][j]=1;
if (s[j]=='@') mp[i][j]=d+1;
}
}
printf("%d\n",bfs());
while (!p.empty()) p.pop();
}
return 0;
}



标签:map,HDU,int,Harbour,harbour,mp,sea,Quick,open
From: https://blog.51cto.com/u_15870896/5838727

相关文章

  • HDU 2589 正方形划分
    ProblemDescription一个边长为L的正方形可以分成L*L个小正方形.有N个石子放在N个小正方形里,能否将它分成N个正方形,使得每个正方形里恰有一个石子且这N个正方......
  • HDU 3085 Nightmare Ⅱ
    ProblemDescriptionLastnight,littleerriyuehadahorriblenightmare.Hedreamedthatheandhisgirlfriendweretrappedinabigmazeseparately.Mo......
  • HDU 3313 Key Vertex
    ProblemDescriptionYouneedwalkingfromvertexStovertexTinagraph.IfyouremoveonevertexwhichstopsyoufromwalkingfromStoT,thatverte......
  • HDU 3316 Mine sweeping
    ProblemDescriptionIthinkmostofyouareusingsystemnamedofxporvistaorwin7.Andthesesystemisconsistofafamousgamewhatisminesweeping......
  • HDU 3355 Hop — Don’t Walk!
    ProblemDescriptionKERMITTHEFROGisaclassicvideogamewithasimplecontrolandobjectivebutrequiresagooddealofthinking.Youcontrolanani......
  • HDU 3427 Clickomania
    ProblemDescriptionClickomaniaisapuzzleinwhichonestartswitharectangulargridofcellsofdifferentcolours.Ineachstep,aplayerselects("......
  • HDU 2209 翻纸牌游戏
    ProblemDescription有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌。但是麻烦的是,......
  • HDU 2216 Game III
    ProblemDescriptionZjtandSarawilltakepartinagame,namedGameIII.ZjtandSarawillbeinamaze,andZjtmustfindSara.Therearesomestrang......
  • HDU 1010 Tempter of the Bone
    DescriptionThedoggiefoundaboneinanancientmaze,whichfascinatedhimalot.However,whenhepickeditup,themazebegantoshake,andthedoggie......
  • HDU 1017 A Mathematical Curiosity
    DescriptionGiventwointegersnandm,countthenumberofpairsofintegers(a,b)suchthat0<a<b<nand(a^2+b^2+m)/(ab)isaninteger. Thi......