bfs测试

· · 算法·理论

先来分数

题目 pts
T1 T672724 100
T2 P1588 100
T3 P14326 100
T4 P12270 10
T5 T725774 11
T6 P2360 0
总分 321

呜呜呜,第6题就因为座标,爆0。

题目讲解

T1T672724

题意

一个n×n的迷宫,1代表不能走,0代表能走,求从起点走到终点要走多少步(保证一定有解)。

思路

nx,ny(数组偏移)

nx[4]={0,0,-1,1}
ny[4]={-1,1,0,0}(前后左右)

BFS

continue的条件:

  1. 越界
  2. 走过
  3. 障碍

return条件:

  1. 栈为空
  2. 走到终点

    code

    #include<bits/stdc++.h>
    #define ll long long
    #define usg unsigned long long
    #define ch char
    #define str string
    #define bl bool
    #define db double
    #define iswscc ios::sync_with_stdio(0);cin.tie();cout.tie();
    using namespace std;
    const ll N=2e5+2;
    ll n,sx,sy,ex,ey;
    struct node
    {
     ll x,y,s;
    };
    queue<node>q;
    ch a[1001][1001];
    ll t[1001][1001];
    ll nx[4]={0,0,-1,1};
    ll ny[4]={-1,1,0,0};
    ll bfs()
    {
     while(!q.empty())
     {
         node u=q.front();
         q.pop();
         for(ll i=0;i<4;i++)
         {
             ll xx=u.x+nx[i],yy=u.y+ny[i];
             if(xx<1||yy<1||xx>n||yy>n) continue;
             if(a[xx][yy]=='1'||t[xx][yy]) continue;
             if(xx==ex&&yy==ey) return u.s+1;
             q.push({xx,yy,u.s+1});
             t[xx][yy]=1;
         }
     }
     return -1;
    }
    int main()
    {
     iswscc
     cin>>n;
     for(ll i=1;i<=n;i++)
     {
         for(ll j=1;j<=n;j++) cin>>a[i][j];
     }
     cin>>sx>>sy>>ex>>ey;
     if(sx==ex&&sy==ey)
     {
         cout<<0;
         return 0;
     }
     q.push({sx,sy,0});
     t[sx][sy]=1;
     cout<<bfs();
     return 0;
    }

    T2P1588

    题意

  3. 移动到k+1
  4. 移动到k-1
  5. 移动到2×k

求从x到y要走几次。

思路

位置移动

node u=q.front();
for(ll i=1;i<=n;i++)
{
    ll xx;
    if(i==1) xx=u.x+1;
    else if(i==2) xx=u.x-1;
    else xx=u.x*2;
}

BFS

continue条件:

  1. 越界
  2. 来过

return条件:

  1. 栈为空
  2. 到达终点

    细节

  3. 边界是0和max(起点,终点)*2因为k可以×2。
  4. 注意清空。

    code

    #include<bits/stdc++.h>
    #define ll long long
    #define usg unsigned long long
    #define ch char
    #define str string
    #define bl bool
    #define db double
    #define iswscc ios::sync_with_stdio(0);cin.tie();cout.tie();
    using namespace std;
    const ll N=2e5+2;
    ll k,sx,ex;
    struct node
    {
     ll x,s;
    };
    queue<node>q;
    ll t[N];
    ll bfs()
    {
     while(!q.empty())
     {
         node u=q.front();
         q.pop();
         for(ll i=1;i<4;i++)
         {
             ll xx;
             if(i==1) xx=u.x+1;
             if(i==2) xx=u.x-1;
             if(i==3) xx=u.x*2;
             if(xx<1||xx>max(sx,ex)*2) continue;
             if(t[xx]) continue;
             if(xx==ex) return u.s+1;
             q.push({xx,u.s+1});
             t[xx]=1;
         }
     }
     return -1;
    }
    int main()
    {
     iswscc
     cin>>k;
     while(k--)
     {
         cin>>sx>>ex;
         q.push({sx,0});
         t[sx]=1;
         if(sx==ex) cout<<0;
         else cout<<bfs();
         cout<<"\n";
         while(!q.empty()) q.pop();
         memset(t,0,sizeof(t));
     }
     return 0;
    }

    T3 P14326

    题意

    给定一个由"."和"#"组成的一个N×M的矩阵,踩的顺序是".#.#……"或"#.#.#……"(只能走到前后左右)。问能否从(1,1)走到(n,m)

    BFS思路

    continue条件:

  5. 越界
  6. 来过
  7. 与之前一格相同

return条件:

  1. 栈为空
  2. 到达终点

    code

    #include<bits/stdc++.h>
    #define ll long long
    #define usg unsigned long long
    #define ch char
    #define str string
    #define bl bool
    #define db double
    #define iswscc ios::sync_with_stdio(0);cin.tie();cout.tie();
    using namespace std;
    const ll N=2e5+2;
    ll n,m;
    struct node
    {
     ll x,y,s;
    };
    queue<node>q;
    ch a[501][501];
    ll t[501][501];
    ll nx[4]={0,0,-1,1};
    ll ny[4]={-1,1,0,0};
    ll bfs()
    {
     while(!q.empty())
     {
         node u=q.front();
         q.pop();
         for(ll i=0;i<4;i++)
         {
             ll xx=u.x+nx[i];
             ll yy=u.y+ny[i];
             if(xx<1||yy<1||xx>n||yy>m) continue;
             if(t[xx][yy]||a[xx][yy]==a[u.x][u.y]) continue;
             if(xx==n&&yy==m) return u.s+1;
             q.push({xx,yy,u.s+1});
             t[xx][yy]=1;
         }
     }
     return -1;
    }
    int main()
    {
     iswscc
     cin>>n>>m;
     for(ll i=1;i<=n;i++)
     {
         for(ll j=1;j<=m;j++) cin>>a[i][j];
     }
     if(1==n&&1==m)
     {
         cout<<0;
         return 0;
     }
     q.push({1,1,0});
     t[1][1]=1;
     cout<<bfs();
     return 0;
    }

    T6 P2360

    其实没啥好讲的,只有3个点要讲:

  3. 因为是三维,所以结构体里面要添加z轴
  4. 坐标不是a[x][y][z],而是a[z][x][y];
  5. 数组偏移如下
    ll nx[6]={0,0,0,0,-1,1};
    ll ny[6]={0,0,-1,1,0,0};
    ll nz[6]={-1,1,0,0,0,0};

    总结

    还不错,前三道题都想到了正解,不过骗的分还不够多。