/*
宽度优先搜索啥时候才能杜绝小错误呢,以后ij一定不在搞混了
*/// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <strstream>#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>using namespace std;// typedef
typedef long long LL;
typedef unsigned long long ULL;//
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)#define FF(i,a) for(int i=0;i<(a);i+++)
#define FFD(i,a) for(int i=(a)-1;i>=0;i--)
#define Z(a) (a<<1)
#define Y(a) (a>>1)const double eps = 1e-6;
const double INFf = 1e10;
const int INFi = 1000000000;
const double Pi = acos(-1.0);template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{if(x>y) return x;return y;
}
template<class T> inline T TMIN(T x,T y)
{if(x<y) return x;return y;
}
template<class T> inline void SWAP(T &x,T &y)
{T t = x;x = y;y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{return TMAX(TMAX(x,y),z);
}// code begin
#define MAXN 410
int mp[MAXN][MAXN];
int used[MAXN][MAXN];
int N;
struct node
{node(){}node(int a,int b,int t){s=a;e=b;tm=t;}int s;int e;int tm;
};
queue<node> qn;
int dir[5][2] = {1,0,0,1,-1,0,0,-1,0,0};int main()
{read;write;int x,y,t;while(scanf("%d",&N)!=-1){memset(mp,-1,sizeof(mp));FORi(0,N,1){scanf("%d %d %d",&x,&y,&t);FORj(0,5,1){int xx = x+dir[j][0];int yy = y+dir[j][1];if( xx>=0 && xx<MAXN && yy>=0 && yy<MAXN){if(mp[yy][xx]==-1) mp[yy][xx] = t;else mp[yy][xx] = TMIN(mp[yy][xx],t);}}}// BFSif(mp[0][0]==0){printf("-1\n");}else{while(!qn.empty()) qn.pop();qn.push(node(0,0,0));memset(used,-1,sizeof(used));used[0][0]= 0;int ans = -1;while(!qn.empty()){node cur = qn.front();qn.pop();FORi(0,4,1){x = cur.s + dir[i][0];y = cur.e + dir[i][1];if( x>=0 && x<MAXN && y>=0 && y<MAXN){if(mp[y][x]==-1){ans = cur.tm+1;break;}if(cur.tm+1<mp[y][x] && (used[y][x]==-1 || cur.tm+1<used[y][x]) ){used[y][x] = cur.tm+1;qn.push(node(x,y,cur.tm+1));}}}if(ans!=-1) break;}printf("%d\n",ans);}}return 0;
}
转载于:https://www.cnblogs.com/ac2012/archive/2011/03/19/1989047.html