1.27总结

· · 个人记录

1.27总结

链接:https://www.luogu.com.cn/contest/305915#description

测试情况:

                      题号
                      分数
                   考察知识点
                     易错点
                       T1
                        0
               一维BFS最短路模板
             多组数据和vis数组标记
                       T2
                       94
               一维BFS最短路模板
                    特判剪枝
                       T3
                       100
                   BFS搜索数字
                       T4
                       100
                    BFS搜索数字

P1588 [USACO07OPEN] Catch That Cow S

错误代码:

#include<bits/stdc++.h>
using namespace std;
int t,x,y,vis[200005],l,r;
struct stu{
  int b,st;
};
queue<stu> q;
void bfs(){
  q.push({x,0});
  vis[x]=1;
  while(q.size()){
    stu h=q.front();
    q.pop();
    for(int i=1;i<=3;i++){
        int nb;
        if(i==1){
            nb=h.b+1;
          }
        if(i==2){
            nb=h.b-1;
          }
        if(i==3){
            nb=h.b*2;
          }
        if(nb>l&&nb<r&&vis[nb]==0){
          q.push({nb,h.st+1});
          if(nb==y){
            cout<<h.st+1<<'\n';
            return;
          }
        }
      }
  }
}
int main(){
  cin>>t;
  while(t--){
    while(q.size()){
        q.pop();
      }
    cin>>x>>y;
    for(int i=0;i<=2*max(y,x);i++){
        vis[i]=0;
    }
    l=0,r=2*max(y,x);
    bfs();
  }
  return 0;
}

错误原因:

if(nb>l&&nb<r&&vis[nb]==0){
    q.push({nb,h.st+1});
    if(nb==y){
        cout<<h.st+1<<'\n';
        return;
    }
}

这里没有标记vis

正确代码:

#include<bits/stdc++.h>
using namespace std;
int t,x,y,vis[200005],l,r;
struct stu{
  int b,st;
};
queue<stu> q;
void bfs(){
  q.push({x,0});
  vis[x]=1;
  while(q.size()){
    stu h=q.front();
    q.pop();
    for(int i=1;i<=3;i++){
        int nb;
        if(i==1){
            nb=h.b+1;
          }
        if(i==2){
            nb=h.b-1;
          }
        if(i==3){
            nb=h.b*2;
          }
        if(nb>l&&nb<r&&vis[nb]==0){
          q.push({nb,h.st+1});
          vis[nb]=1;
          if(nb==y){
            cout<<h.st+1<<'\n';
            return;
          }
        }
      }
  }
}
int main(){
  cin>>t;
  while(t--){
    while(q.size()){
        q.pop();
      }
    cin>>x>>y;
    for(int i=0;i<=2*max(y,x);i++){
        vis[i]=0;
    }
    l=0,r=2*max(y,x);
    bfs();
  }
  return 0;
}

T725738 魔板 Magic Squares (EZ)

错误代码(本题因为时间不足骗了12分)

正确思路:

纯属模拟过程

正确代码:

#include<bits/stdc++.h>
using namespace std;
using ULL=unsigned long long;
const int P=131;
const int MAX=100003;
string ans,a;
map<string,int> m;
struct stu{
  string s;
  int st;
};
queue<stu> q;
string trans(string s,int k){
  if(k==1){
    return s.substr(4)+s.substr(0,4);
  }
  if(k==2){
    return string({s[3],s[0],s[1],s[2],s[7],s[4],s[5],s[6]});
  }
  return string({s[0],s[5],s[1],s[3],s[4],s[6],s[2],s[7]});
}
int bfs(){
  q.push({"12348765",0});
  m["12348765"]=1;
  while(q.size()){
    stu h=q.front();
    q.pop();
    ///cout<<h.s<<endl;
    for(int k=1;k<=3;k++){
      string tmp=trans(h.s,k);
      ///cout<<tmp<<endl;
      if(m[tmp]==1){
        continue;
      }
      ///cout<<"!"<<'\n';
      m[tmp]=1;
      q.push({tmp,h.st+1});
      if(tmp==ans){
        return h.st+1;
      }
    }
  }
  return -1;
}
int main(){
    char tmp[8];
    for(int i=0;i<=7;i++){
        cin>>tmp[i];
    }
    for(int i=0;i<4;i++){
        ans+=tmp[i];
    }
    for(int i=7;i>=4;i--){
     ans+=tmp[i];
    }
    if(ans=="12348765"){
      cout<<0;
      return 0; 
    }
    cout<<bfs();
    return 0;
}