Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

还是Bfs水题好写.....

Posted by Bang1314 at 2018-03-27 22:21:14 on Problem 1915
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator