一个裸的bfs+优先队列,居然还MLE两次,WA两次,我也是服了自己了,哎
MLE:忘记book[tx][ty] = 1了,还找了半天哪里会MLE啊。。。
WA:没考虑到S就在边界的情况。。。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 505;
int row, col, d, sx, sy;
char pic[maxn][maxn];
bool book[maxn][maxn];
struct node
{
int x, y, s;
node() {}
node(int xx, int yy, int ss): x(xx), y(yy), s(ss) {}
bool operator < (const node &a) const
{
return s > a.s;
}
};
int bfs(void)
{
priority_queue<node> pq;
pq.push(node(sx, sy, 0));
book[sx][sy] = 1;
while(!pq.empty())
{
node u = pq.top(); pq.pop();
int next[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
for(int i = 0; i < 4; i++)
{
int tx = u.x+next[i][0];
int ty = u.y+next[i][1];
if(tx >= 0 && ty >= 0 && tx < row && ty < col && !book[tx][ty] && pic[tx][ty] != '#')
{
book[tx][ty] = 1;
int ts = u.s+1;
if(pic[tx][ty] == '@') ts += d;
if(tx == 0 || ty == 0 || tx == row-1 || ty == col-1) return ts+1;
pq.push(node(tx, ty, ts));
}
}
}
return 1;
}
int main(void)
{
//freopen("in.txt", "r", stdin);
int t;
cin >> t;
while(t--)
{
memset(book, 0, sizeof(book));
scanf("%d%d%d", &row, &col, &d);
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
{
scanf(" %c", &pic[i][j]);
if(pic[i][j] == 'S')
sx = i, sy = j;
}
printf("%d\n", bfs());
}
return 0;
}
Quick out of the Harbour
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1769 Accepted Submission(s): 717
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.
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.
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
Source
Recommend