zoukankan      html  css  js  c++  java
  • LOJ P10028 Knight Moves 题解

    每日一题 day66 打卡

    我很震惊一本通居然还有这么善良的题目。

    Analysis

    只需要一个广搜的模板就好了,值得注意的是做每个子任务之前记得清空队列。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 #define int long long
     7 #define maxn 310
     8 #define rep(i,s,e) for(register int i=s;i<=e;++i)
     9 #define dwn(i,s,e) for(register int i=s;i>=e;--i)
    10 using namespace std;
    11 inline int read()
    12 {
    13     int x=0,f=1;
    14     char c=getchar();
    15     while(c<'0'||c>'9') {if(c=='-') x=-x; c=getchar();}
    16     while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
    17     return f*x;
    18 }
    19 inline void write(int x)
    20 {
    21     if(x<0){putchar('-');x=-x;}
    22     if(x>9)write(x/10);
    23     putchar(x%10+'0');
    24 }
    25 int T;
    26 int n;
    27 int ex,ey; 
    28 int book[maxn][maxn];
    29 struct node
    30 {
    31     int x,y,step;
    32 }x[maxn];
    33 queue<node> q;
    34 int dx[9]={0,1,1,-1,-1,2,2,-2,-2};
    35 int dy[9]={0,2,-2,2,-2,1,-1,1,-1};
    36 signed main()
    37 {
    38     T=read();
    39     while(T--)
    40     {
    41         while(!q.empty()) q.pop();
    42         memset(book,0,sizeof(book));
    43         n=read();
    44         x[1].x=read();x[1].y=read();
    45         x[1].step=0;
    46         ex=read();ey=read();
    47         book[x[1].x][x[1].y]=1;
    48         if(x[1].x==ex&&x[1].y==ey) 
    49         {
    50             write(0);
    51             putchar('
    ');
    52             continue;
    53         }
    54         q.push(x[1]);
    55         while(!q.empty())
    56         {
    57             node now=q.front();
    58             q.pop();
    59             int nx=now.x,ny=now.y,ns=now.step;
    60             if(nx==ex&&ny==ey) 
    61             {
    62                 write(ns);
    63                 putchar('
    ');
    64                 break;
    65             }
    66             rep(i,1,8)
    67             {
    68                 int xx=nx+dx[i],yy=ny+dy[i];
    69                 if(book[xx][yy]==1||xx<0||xx>=n||yy<0||yy>=n) continue;
    70                 book[xx][yy]=1;
    71                 node sub;
    72                 sub.x=xx;sub.y=yy;sub.step=ns+1;
    73                 q.push(sub);
    74             }
    75         } 
    76     }
    77     return 0;
    78 }

    如有失误请各位大佬斧正(反正我不认识斧正是什么意思)

  • 相关阅读:
    使用zipkin2在SpringCloud2.0环境下追踪服务调用情况
    Spring Cloud负载均衡:使用Feign作客户端负载均衡
    Spring Cloud负载均衡:使用zuul作服务器端负载均衡
    Word模板替换
    【转】Eureka集群
    巧用JavaScript语言特性解耦页面间调用(观察者模式)
    MySQL 视图触发器事务存储过程函数
    MySQL py模块的链接Navicat可视化工具
    MySQL 单表查询多表查询
    MySQL 表与表之间建立关系
  • 原文地址:https://www.cnblogs.com/handsome-zyc/p/12363969.html
Copyright © 2011-2022 走看看