HDU 4198 Quick out of the Harbour(BFS+最小优先队列)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4198

 

大意:起点S到出口的最短花费。。。。。其中#为障碍物。。。。     '.'的花费为1 。。。@的花费为c+1。。。。c是第三个输入数据

 

师兄说过可以dijstra。。。但是我还没试过。。。。留着以后在试吧。

 

#include <iostream>
#include <queue>
using namespace std;

struct point
{
int x, y, v;
friend bool operator < (point a, point b)
{
return a.v > b.v; //重载小于号使得小的先出队列
}
};


char g[555][555];
int xx[8] = {1,0,0,-1};
int yy[8] = {0,1,-1,0};
int sum;
int n, m;
int c;
int tx, ty;

int Bfs(int x, int y)//广搜目的找最短路。
{
point q1, q2;
priority_queue <point> Q;
int i;
q1.x = x;
q1.y = y;
q1.v = 1;
Q.push(q1);

while (!Q.empty())
{
q2 = Q.top();
Q.pop();
g[q2.x][q2.y] = 0;

if (q2.x == tx && q2.y == ty)//出口
{
return q2.v;
}
for (i = 0; i < 8; i++)
{
q1.x = q2.x + xx[i];
q1.y = q2.y + yy[i];
if (g[q1.x][q1.y] == '.')
{
q1.v = q2.v + 1;
}
else
{
q1.v = q2.v + c;
}
if (q1.x >=0 && q1.y >=0 && q1.x < n && q1.y < m && g[q1.x][q1.y] && g[q1.x][q1.y] != '#')
{
Q.push(q1);
g[q1.x][q1.y] = 0;

}
}
}


}

int main()
{
int i, j;
int sx,sy;
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d%d", &n, &m, &c);
c++;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
scanf(" %c" ,&g[i][j]);
if (g[i][j] == 'S')
{
sx = i, sy = j;
}
if ((i == 0 || i == n-1 || j == 0 || j == n-1) && g[i][j] != '#')
{
tx = i, ty = j;
}
}
}

// sum = 1;

printf("%d\n", Bfs(sx, sy));

}
return 0;
}



posted on 2012-04-03 19:18  [S*I]SImMon_WCG______*  阅读(413)  评论(0编辑  收藏  举报

导航