POJ_3669
广搜一下即可。
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> #define MAXD 310 #define INF 0x3f3f3f3f int N, danger[MAXD][MAXD], dis[MAXD][MAXD]; int dx[] = {-1, 1, 0, 0, 0}, dy[] = {0, 0, -1, 1, 0}; int inside(int x, int y) { return x >= 0 && y >= 0; } void init() { int i, j, x, y, t, nx, ny; memset(danger, 0x3f, sizeof(danger)); for(i = 0; i < N; i ++) { scanf("%d%d%d", &x, &y, &t); for(j = 0; j < 5; j ++) { nx = x + dx[j], ny = y + dy[j]; if(!inside(nx, ny)) continue; danger[nx][ny] = std::min(danger[nx][ny], t); } } } struct St { int x, y; St(){} St(int _x, int _y) : x(_x), y(_y){} }; void solve() { int i, j, x, y; std::queue<St> q; if(danger[0][0] == 0) { printf("-1\n"); return ; } memset(dis, 0x3f, sizeof(dis)), dis[0][0] = 0; q.push(St(0, 0)); while(!q.empty()) { St st = q.front(); if(danger[st.x][st.y] == INF) break; q.pop(); for(i = 0; i < 4; i ++) { x = st.x + dx[i], y = st.y + dy[i]; if(inside(x, y) && dis[st.x][st.y] + 1 < dis[x][y] && danger[x][y] > dis[st.x][st.y] + 1) dis[x][y] = dis[st.x][st.y] + 1, q.push(St(x, y)); } } if(q.empty()) printf("-1\n"); else { St st = q.front(); printf("%d\n", dis[st.x][st.y]); } } int main() { while(scanf("%d", &N) == 1) { init(); solve(); } return 0; }