http://poj.org/problem?id=3278

    1. //poj3278
    2. //在n的位置上,抓到k的位置上的牛,牛不动,需要几步,记录过程
    3. #include <cmath>
    4. #include <cstdio>
    5. #include <iostream>
    6. #include <algorithm>
    7. #include <queue>
    8. #include <map>
    9. #include <cstring>
    10. #include <vector>
    11. #include <stack>
    12. using namespace std;
    13. #define ll long long
    14. #define ff first
    15. #define ss second
    16. #define mp make_pair
    17. #define ph push
    18. #define all(x) (x).begin(), (x).end()
    19. int n,k;
    20. const int N=100006;
    21. struct step{
    22. int x;
    23. int steps;
    24. step(int xx,int s):x(xx),steps(s){}
    25. };
    26. queue<step> q;
    27. int vis[N];
    28. bool f(int x){
    29. return x>=0&&x<N;
    30. }
    31. void bfs(){
    32. while(!q.empty()){
    33. step s=q.front();
    34. if(s.x==k){
    35. cout<<s.steps<<endl;
    36. return ;
    37. }
    38. if(f(s.x-1)&&!vis[s.x-1]){
    39. q.ph(step(s.x-1,s.steps+1));
    40. vis[s.x-1]=1;
    41. }
    42. if(f(s.x+1)&&!vis[s.x+1]){
    43. q.ph(step(s.x+1,s.steps+1));
    44. vis[s.x+1]=1;
    45. }
    46. if(f((s.x)<<1)&&!vis[(s.x)<<1]){
    47. q.ph(step((s.x)<<1,s.steps+1));
    48. vis[(s.x)<<1]=1;
    49. }
    50. q.pop();
    51. }
    52. }
    53. int main() {
    54. ios::sync_with_stdio(false);
    55. cin>>n>>k;
    56. memset(vis,0,sizeof(vis));
    57. q.ph(step(n,0));
    58. bfs();
    59. }

    http://poj.org/problem?id=3984

    1. //poj3984
    2. //一个5*5的迷宫,1代表墙,0代表路 [1][1]->[5][5]最短
    3. #include <cmath>
    4. #include <cstdio>
    5. #include <iostream>
    6. #include <algorithm>
    7. #include <queue>
    8. #include <map>
    9. #include <cstring>
    10. #include <vector>
    11. #include <stack>
    12. using namespace std;
    13. #define ll long long
    14. #define ff first
    15. #define ss second
    16. #define mp make_pair
    17. #define ph push
    18. #define pb push_back
    19. #define all(x) (x).begin(), (x).end()
    20. const int INF = 1e10;
    21. struct nod {
    22. int x, y;
    23. int father;
    24. nod(int xx = 0, int yy = 0, int f = 0) : x(xx), y(yy), father(f) {}
    25. };
    26. int mi[8][8];
    27. nod q[200];
    28. int head, tail;
    29. int dx[] = {-1, 1, 0, 0};
    30. int dy[] = {0, 0, -1, 1};
    31. void bfs() {
    32. while (head != tail) {
    33. nod nd = q[head];
    34. if (nd.x == 5 && nd.y == 5) {
    35. vector<nod> temp;
    36. while (true) {
    37. temp.pb(nod(nd.x, nd.y, 0));
    38. if (nd.father == -1) {
    39. break;
    40. }
    41. nd = q[nd.father];
    42. }
    43. for (int i = temp.size() - 1; i >= 0; --i) {
    44. cout << "(" << temp[i].x - 1 << ", " << temp[i].y - 1 << ")" << endl;
    45. }
    46. return;
    47. }
    48. for (int i = 0; i < 4; i++) {
    49. int x = nd.x + dx[i], y = nd.y + dy[i];
    50. if (!mi[x][y]) {
    51. q[tail++] = nod(x, y, head);
    52. mi[x][y] = 1;
    53. }
    54. }
    55. ++head;
    56. }
    57. }
    58. int main() {
    59. ios::sync_with_stdio(false);
    60. memset(mi, INF, sizeof(mi));
    61. for (int i = 1; i <= 5; ++i) {
    62. for (int j = 1; j <= 5; j++) {
    63. cin >> mi[i][j];
    64. }
    65. }
    66. head = 0;
    67. tail = 1;
    68. q[0] = nod(1, 1, -1);
    69. bfs();
    70. }

    https://www.luogu.org/problemnew/show/P1443

    1. #include <cmath>
    2. #include <cstdio>
    3. #include <iostream>
    4. #include <algorithm>
    5. #include <queue>
    6. #include <map>
    7. #include <cstring>
    8. #include <vector>
    9. #include <stack>
    10. #include <string>
    11. using namespace std;
    12. #define ll long long
    13. #define ff first
    14. #define ss second
    15. #define mp make_pair
    16. #define ph push
    17. #define pb push_back
    18. #define all(x) (x).begin(), (x).end()
    19. int n,m,a,b;
    20. int ma[405][405];
    21. queue<pair<pair<int,int>,int> > q;
    22. bool tf(int x,int y){
    23. return x>=1&&x<=n&&y>=1&&y<=m;
    24. }
    25. int dx[]={-2,-2,2,2,-1,-1,1,1};
    26. int dy[]={1,-1,1,-1,2,-2,2,-2};
    27. void bfs(){
    28. while(!q.empty()){
    29. pair<pair<int,int>,int> p=q.front();
    30. for(int i=0;i<8;++i){
    31. int x=p.ff.ff+dx[i],y=p.ff.ss+dy[i];
    32. if(tf(x,y)&&ma[x][y]==-1){
    33. q.ph(mp(mp(x,y),p.ss+1));
    34. ma[x][y]=p.ss+1;
    35. }
    36. }
    37. q.pop();
    38. }
    39. for(int i=1;i<=n;++i){
    40. for(int j=1;j<=m;++j){
    41. printf("%-5d",ma[i][j]);
    42. }
    43. cout<<endl;
    44. }
    45. }
    46. int main() {
    47. // ios::sync_with_stdio(false);
    48. cin>>n>>m>>a>>b;
    49. memset(ma,-1,sizeof(ma));
    50. q.ph(mp(mp(a,b),0));
    51. ma[a][b]=0;
    52. bfs();
    53. return 0;
    54. }