Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
还是Bfs水题好写.....#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define PI acos(-1.0) #define INF 0x3f3f3f3f #define ll long long #define lowbit(i) i&(-i) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=300+7; const int mod=1e9+7; const double eps = 1e-6; typedef pair<int,int> P; int maze[maxn][maxn]; int endx,endy; int dir[8][2]={-1,-2,-2,-1,-2,1,-1,2,1,-2,2,-1,2,1,1,2}; int len; int solve(int x,int y){ queue<P> qu; qu.push(P(x,y)); maze[x][y]=0; while(!qu.empty()){ pair<int,int> temp=qu.front(); qu.pop(); if(temp.first==endx&&temp.second==endy) return maze[endx][endy]; for(int i=0;i<8;i++){ int nx=temp.first+dir[i][0],ny=temp.second+dir[i][1]; if(nx<0||ny<0||nx>=len||ny>=len) continue; if(maze[nx][ny]>maze[temp.first][temp.second]+1){ qu.push(P(nx,ny)); maze[nx][ny]=maze[temp.first][temp.second]+1; } } } return 0; } int main(){ int n; scanf("%d",&n); while(n--){ memset(maze,INF,sizeof(maze)); scanf("%d",&len); int x,y; scanf("%d%d",&x,&y); scanf("%d%d",&endx,&endy); printf("%d\n",solve(x,y)); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator