题意:
跳马,给出起点和终点,求其moves;
分析:
和zoj1091 很像,几乎改下就能过。但是自己还是忘了清空队列了,造成一个WA.
#include <iostream> #include <queue> #include<cstdio> using namespace std; typedef struct point{ int x,y; int moves; }point; queue<point>Q; int e_x,e_y,l; int map[305][305]; int dir[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}}; int main() { int bfs(point s); int n,i,j,ans; int s_x,s_y; point start; scanf("%d",&n); while(n--) { scanf("%d",&l); scanf("%d%d%d%d",&s_x,&s_y,&e_x,&e_y); for(i=0;i<l;i++) for(j=0;j<l;j++) map[i][j]=0; while(!Q.empty()) Q.pop (); start.x=s_x; start.y=s_y; start.moves=0; ans=bfs(start); printf("%d\n",ans); } return 0; } int bfs(point s) { int i; map[s.x][s.y]=1; point head,t; Q.push(s); while(!Q.empty ()) { head=Q.front (); Q.pop (); if(head.x==e_x&&head.y==e_y) return head.moves; for(i=0;i<8;i++) { int x=head.x+dir[i][0]; int y=head.y+dir[i][1]; if(x>=0&&x<l&&y>=0&&y<l&&!map[x][y]) { map[x][y]=1; t.x=x; t.y =y; t.moves =head.moves+1; Q.push(t); } } } }