标签:模板 source tle off local board next struct strong
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1372
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13384 Accepted Submission(s): 7831
#include<bits/stdc++.h> using namespace std; #define max_v 10 int G[max_v][max_v]; int dir[8][2]= {{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}}; int step; int sx,sy,fx,fy; struct node { int x,y,step; }; void bfs() { memset(G,0,sizeof(G)); queue<node> q; node p,next; p.x=sx; p.y=sy; p.step=0; G[p.x][p.y]=1; q.push(p); while(!q.empty()) { p=q.front(); q.pop(); if(p.x==fx&&p.y==fy) { step=p.step; return ; } for(int i=0; i<8; i++) { next.x=p.x+dir[i][0]; next.y=p.y+dir[i][1]; if(next.x>=1&&next.y>=1&&next.x<=8&&next.y<=8&&G[next.x][next.y]==0) { next.step=p.step+1; G[next.x][next.y]=1; q.push(next); } } } } int main() { char c1,c2; int y1,y2; while(~scanf("%c%d %c%d",&c1,&y1,&c2,&y2)) { getchar(); sx=c1-‘a‘+1; sy=y1; fx=c2-‘a‘+1; fy=y2; bfs(); printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,y1,c2,y2,step); } return 0; }
HDU 1372 Knight Moves(最简单也是最经典的bfs)
标签:模板 source tle off local board next struct strong
原文地址:https://www.cnblogs.com/yinbiao/p/9350894.html