1026 Ignatius and the Princess I
http://acm.hdu.edu.cn/showproblem.php?pid=1026
这道题是经典的地图寻路搜索问题
可以用广度优先搜索来完成此题
AC CODE:
void bfs()
{
int i,head = 0,tail = 1;
state[head].x = 0; state[head].y =0; state[head].time = 0; state[head].last = -1;
v[0][0] = 0;
while (head!=tail && tail!=MaxQueue-5)
{
temp = state[head];
for (i=0;i<4;i++) //search for the 4 direction
{
int tx = temp.x + dir[i][0];
int ty = temp.y + dir[i][1];
if (tx >= 0 && tx < n && ty >=0 && ty < m && g[tx][ty]!='X')
{
int plus;
if (g[tx][ty] == '.') plus = 1;
else plus = g[tx][ty]-'0' + 1;
if (temp.time+plus < v[tx][ty]) // 如果有更小步数的走法
{
if (tx == n-1 && ty == m-1) flagForEnd = tail;
state[tail].x = tx; state[tail].y = ty;
v[tx][ty] = temp.time + plus;
state[tail].time = v[tx][ty];
state[tail].last = head;
tail++;
}
}
}
head++;
}
}
void init()
{
memset(v,1,sizeof(v));
}