/* * @author Panoss */ #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> #include<ctime> #include<stack> #include<queue> #include<list> using namespace std; #define DBG 1 #define fori(i,a,b) for(int i = (a); i < (b); i++) #define forie(i,a,b) for(int i = (a); i <= (b); i++) #define ford(i,a,b) for(int i = (a); i > (b); i--) #define forde(i,a,b) for(int i = (a); i >= (b); i--) #define forls(i,a,b,n) for(int i = (a); i != (b); i = n[i]) #define mset(a,v) memset(a, v, sizeof(a)) #define mcpy(a,b) memcpy(a, b, sizeof(a)) #define dout DBG && cerr << __LINE__ << " >>| " #define checkv(x) dout << #x"=" << (x) << " | "<<endl #define checka(array,a,b) if(DBG) { \ dout<<#array"[] | " << endl; forie(i,a,b) cerr <<"["<<i<<"]="<<array[i]<<" |"<<((i-(a)+1)%5?" ":"\n"); if(((b)-(a)+1)%5) cerr<<endl; } #define redata(T, x) T x; cin >> x #define MIN_LD -2147483648 #define MAX_LD 2147483647 #define MIN_LLD -9223372036854775808 #define MAX_LLD 9223372036854775807 #define MAX_INF 18446744073709551615 inline int reint() { int d; scanf("%d",&d); return d; } inline long relong() { long l; scanf("%ld",&l); return l; } inline char rechar() { scanf(" "); return getchar(); } inline double redouble() { double d; scanf("%lf", &d); return d; } inline string restring() { string s; cin>>s; return s; } char a[12][12]; int d[12][12]; ///存放每个坐标到起点的最短距离 bool flag[12][12]; const int dx[] = {1,0,-1,0}; ///四个方向增量 const int dy[] = {0,1,0,-1}; struct node { int x,y; }; bool IsCanplace(int row,int col) { if(row<12&&row>=0&&col>=0&&col<12&&a[row][col]==‘.‘&&flag[row][col]==false) return true; else return false; } void BFS(int start_x,int start_y,int end_x,int end_y) { mset(flag,false); mset(d,-1); node N; N.x = start_x,N.y = start_y; queue<node> Q; Q.push(N); d[N.x][N.y]=0; flag[N.x][N.y] = true; while(!Q.empty()) { N = Q.front(); Q.pop(); int x = N.x; int y = N.y; if(x == end_x && y == end_y) return ; fori(i,0,4) { int nx = x + dx[i]; int ny = y + dy[i]; if(IsCanplace(nx,ny)) { d[nx][ny] = d[x][y] + 1; flag[nx][ny] = true; N.x = nx; N.y = ny; Q.push(N); } } } } int main() { int start_x , start_y , end_x , end_y; cin>> start_x >> start_y >> end_x >> end_y; fori(i,0,12) scanf("%s",a[i]); BFS(start_x-1,start_y-1,end_x-1,end_y-1); if(d[end_x-1][end_y-1]!=-1) cout<<d[end_x-1][end_y-1]<<endl;
return 0; }
原文:http://www.cnblogs.com/Panoss/p/3741894.html