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 |
我去,王记\n了,弄了一次PE,qwq#include <iostream> #include <stdio.h> #include <queue> using namespace std; int ans[303][303] = {0}; const int MAX = 2147483647; void init(int l){ for(int i = 0; i < l; i++){ for(int j = 0; j < l; j++){ ans[i][j] = MAX; } } } class pt{ public: int x, y; pt(int X, int Y): x(X), y(Y){} }; bool inRange(int l, int x, int y){ return (x >= 0 && x < l && y >= 0 && y < l); } int main() { int cases; scanf("%d", &cases); for(int ii = 0; ii < cases; ii++){ int l; scanf("%d", &l); int x0,y0,x1,y1; scanf("%d%d%d%d", &x0,&y0,&x1,&y1); if(x0 == x1 && y0 == y1) { printf("0\n"); continue; } init(l); pt start(x0,y0); queue<pt> qp; qp.push(start); ans[x0][y0] = 0; while(!qp.empty()){ pt thisP = qp.front(); qp.pop(); int cs = ans[thisP.x][thisP.y]; int X = thisP.x, Y = thisP.y; if(inRange(l, X+1, Y+2) && ans[X+1][Y+2] == MAX){ ans[X+1][Y+2] = cs + 1; if(X+1 == x1 && Y+2 == y1) break; qp.push(pt(X+1, Y+2)); } if(inRange(l, X+1, Y-2) && ans[X+1][Y-2] == MAX){ ans[X+1][Y-2] = cs + 1; if(X+1 == x1 && Y-2 == y1) break; qp.push(pt(X+1, Y-2)); } if(inRange(l, X-1, Y+2) && ans[X-1][Y+2] == MAX){ ans[X-1][Y+2] = cs + 1; if(X-1 == x1 && Y+2 == y1) break; qp.push(pt(X-1, Y+2)); } if(inRange(l, X-1, Y-2) && ans[X-1][Y-2] == MAX){ ans[X-1][Y-2] = cs + 1; if(X-1 == x1 && Y-2 == y1) break; qp.push(pt(X-1, Y-2)); } if(inRange(l, X+2, Y+1) && ans[X+2][Y+1] == MAX){ ans[X+2][Y+1] = cs + 1; if(X+2 == x1 && Y+1 == y1) break; qp.push(pt(X+2, Y+1)); } if(inRange(l, X+2, Y-1) && ans[X+2][Y-1] == MAX){ ans[X+2][Y-1] = cs + 1; if(X+2 == x1 && Y-1 == y1) break; qp.push(pt(X+2, Y-1)); } if(inRange(l, X-2, Y+1) && ans[X-2][Y+1] == MAX){ ans[X-2][Y+1] = cs + 1; if(X-2 == x1 && Y+1 == y1) break; qp.push(pt(X-2, Y+1)); } if(inRange(l, X-2, Y-1) && ans[X-2][Y-1] == MAX){ ans[X-2][Y-1] = cs + 1; if(X-2 == x1 && Y-1 == y1) break; qp.push(pt(X-2, Y-1)); } } printf("%d\n", ans[x1][y1]); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator