zoukankan      html  css  js  c++  java
  • POJ1915(Knight Moves)

    题目链接

    宽度优先搜索。

    View Code
     1 #include <stdio.h>
     2 #include <memory.h>
     3 #include <queue>
     4 #define N 300
     5 using namespace std;
     6 typedef pair<int,int> node;
     7 queue<node> Q;
     8 int dx[8]={1,1,-1,-1,2,2,-2,-2};
     9 int dy[8]={2,-2,2,-2,1,-1,1,-1};
    10 int n;
    11 int step[N][N];
    12 int si,sj,ei,ej;
    13 int bfs()
    14 {
    15   int i,j,ni,nj,d;
    16   node pos;
    17   memset(step,0,sizeof(step));
    18   while(!Q.empty()) Q.pop();
    19   Q.push(make_pair(si,sj));
    20   step[si][sj]=1;
    21   while(!Q.empty())
    22   {
    23     pos=Q.front(),Q.pop();
    24     i=pos.first;
    25     j=pos.second;
    26     for(d=0;d<8;d++)
    27     {
    28        ni=i+dx[d],nj=j+dy[d];
    29        if(ni<0 || nj<0 || ni>=n || nj>=n || step[ni][nj])  continue;
    30        step[ni][nj]=step[i][j]+1;
    31        if(ni==ei && nj==ej) return step[i][j];
    32        Q.push(make_pair(ni,nj));
    33      }
    34   }
    35 }
    36 int main()
    37 {
    38   int t;
    39   scanf("%d",&t);
    40   while(t--)
    41   {
    42     scanf("%d%d%d%d%d",&n,&si,&sj,&ei,&ej);
    43     if(si==ei && sj==ej)  printf("0\n");
    44     else  printf("%d\n",bfs());
    45   }
    46   return 0;
    47 }
  • 相关阅读:
    linux 下怎么看postgresql安装到哪个目录了?
    sqlserver 存储image 语句
    thinkphp5.1 配置使用
    百度车牌识别demo
    elastticsearch 安装
    InDB开发
    John爆破密码
    域传送漏洞
    新远程下载方式(IME)
    SSH端口转发
  • 原文地址:https://www.cnblogs.com/algorithms/p/2469774.html
Copyright © 2011-2022 走看看