渣渣的我做算法只能做水题啊。。
又是一道BFS水题
代码虐我千百遍,我待代码如初恋。我对代码是真爱啊
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 22041 | Accepted: 10294 |
Description
Input
Output
Sample Input
3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1
Sample Output
5 28 0
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <limits.h> #include <ctype.h> #include <string.h> #include <string> #include <math.h> #include <queue> #include <stack> #include <vector> #include <set> #include <map> #include <algorithm> #include <iostream> #include <deque> using namespace std; #define MAXN 300 + 5 struct node{ int x; int y; int step; }; int vis[MAXN][MAXN]; int n; int fx,fy; int xx[8] = {1,2,2,1,-1,-2,-2,-1}; int yy[8] = {2,1,-1,-2,-2,-1,1,2}; void BFS(int a,int b){ queue<node>q; int i,j; int dx,dy; node front,rear; front.x = a; front.y = b; front.step = 0; vis[a][b] = 1; q.push(front); while(!q.empty()){ front = q.front(); q.pop(); if(front.x==fx && front.y==fy){ printf("%d\n",front.step); } for(i=0;i<8;i++){ dx = front.x + xx[i]; dy = front.y + yy[i]; if(dx>=0&&dx<n&&dy>=0&&dy<n){ if(vis[dx][dy] == 0){ rear.x = dx; rear.y = dy; rear.step = front.step+1; vis[dx][dy] = 1; q.push(rear); } } } } } int main(){ int x1,y1; //int y1,y2; int T; while(~scanf("%d",&T)){ while(T--){ memset(vis,0,sizeof(vis)); scanf("%d",&n); scanf("%d%d",&x1,&y1); scanf("%d%d",&fx,&fy); BFS(x1,y1); } } return 0; }
原文地址:http://blog.csdn.net/zcr_7/article/details/39153371