1.26总结

· · 个人记录

1.26总结

链接:https://www.luogu.com.cn/contest/305730

测试情况:

                      题号
                      分数
                   考察知识点
                     易错点
                       T1
                       30
                 BFS最短路模板
                    多组数据
                       T2
                        0
            坐标偏移+BFS最短路模板
             map结构体要自定义排序
                       T3
                       50
             记录坐标+BFS最短路模板
                  没用二维数组
                       T4
                        7
                    洪水模板
                     ???

题目总结:

T725354 仙岛求药

错误代码:

#include<bits/stdc++.h>
using namespace std;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
char a[25][25];
int v[25][25];
int n,m,ex,ey,z;
struct stu{
  int x,y,st;
};
queue<stu> q;
void bfs(){
  q.push({ex,ey,1});
  v[ex][ey]=1;
  while(q.size()){
    stu h=q.front();
    q.pop();
    for(int i=0;i<4;i++){
      int nx=h.x+dx[i],ny=h.y+dy[i];
      if(v[nx][ny]==0&&nx>=1&&nx<=n&&ny>=1&&ny<=m&&a[nx][ny]!='#'){
        q.push({nx,ny,h.st+1});                                  
        v[nx][ny]=1;
        if(a[nx][ny]=='*'){
            z=h.st;
            return;
          }
      }
    }
  }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
            if(a[i][j]=='@'){
                ex=i,ey=j;
            }
        }
    }
    bfs();
    if(z==0){
      cout<<-1;
    }else{
      cout<<z;
    }
    return 0;
} 

错误思路:

没有注意到“输入有多组测试数据”,然后就只拿到30分……

正确思路:

增加上多组数据(注意初始化)

正确代码:

#include<bits/stdc++.h>
using namespace std;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
char a[25][25];
int v[25][25];
int n,m,ex,ey,z;
struct stu{
  int x,y,st;
};
queue<stu> q;
void bfs(){
  q.push({ex,ey,1});
  v[ex][ey]=1;
  while(q.size()){
    stu h=q.front();
    q.pop();
    for(int i=0;i<4;i++){
      int nx=h.x+dx[i],ny=h.y+dy[i];
      if(v[nx][ny]==0&&nx>=1&&nx<=n&&ny>=1&&ny<=m&&a[nx][ny]!='#'){
        q.push({nx,ny,h.st+1});                                  
        v[nx][ny]=1;
        if(a[nx][ny]=='*'){
            z=h.st;
            return;
          }
      }
    }
  }
}
int main(){
    while(1){
        cin>>n>>m;
        if(n==0&&m==0){
          return 0;
        }
        z=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                v[i][j]=0;
            }
        }
        while(q.size()){
          q.pop();
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>a[i][j];
                if(a[i][j]=='@'){
                    ex=i,ey=j;
                }
            }
        }
        bfs();
        if(z==0){
          cout<<-1;
        }else{
          cout<<z;
        }
        cout<<'\n';
    }
    return 0;
} 

P2873 [USACO07DEC] Mud Puddles S

错误代码:

#include<bits/stdc++.h>
using namespace std;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
struct m_p{
  int x,y;
};
map<pairt<int,int> ,int> m;
struct farmer{
  int x,y,st;
};
int x,y,n,z;
queue<farmer> q;
void bfs(){
  q.push({0,0,0});
  m[{0,0}]=1;
  while(q.size()){
    farmer h=q.front();
    q.pop();
    for(int i=0;i<4;i++){
      int nx=h.x+dx[i],ny=h.y+dy[i],nst=h.st+1;
      if(m[{nx,ny}]!=1){
        q.push({nx,ny,nst});
        if(nx==x&&ny==y){
            z=nst;
            return ;
          }
      }
    }
  }
}
int main(){
    cin>>x>>y>>n;
    for(int i=1;i<=n;i++){
      int a,b;
      cin>>a>>b;
      m[{a,b}]=1;
    }
    bfs();
    cout<<z;
    return 0;
}

错误原因:

map使用结构体要定义排序方式,但实际上还有一个错误:

      if(m[{nx,ny}]!=1){
        q.push({nx,ny,nst});
        if(nx==x&&ny==y){
          z=nst;
          return ;
        }
      }

这里没有标记,所以最终代码如下:

#include<bits/stdc++.h>
using namespace std;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
struct m_p{
  int x,y;
};
map<pair<int,int> ,int> m;
struct farmer{
  int x,y,st;
};
int x,y,n,z;
queue<farmer> q;
void bfs(){
  q.push({0,0});
  m[{0,0}]=1;
  while(q.size()){
    farmer h=q.front();
    q.pop();
    for(int i=0;i<4;i++){
      int nx=h.x+dx[i],ny=h.y+dy[i],nst=h.st+1;
      if(m[{nx,ny}]!=1){
        q.push({nx,ny,nst});
        m[{nx,ny}]=1;
        if(nx==x&&ny==y){
            z=nst;
            return ;
          }
      }
    }
  }
}
int main(){
    cin>>x>>y>>n;
    for(int i=1;i<=n;i++){
      int a,b;
      cin>>a>>b;
      m[{a,b}]=1;
    }
    bfs();
    cout<<z;
    return 0;
}

T725348 走出迷宫的最短路径

错误代码:

#include<bits/stdc++.h>
using namespace std;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
int n,m,a[155][155],ex,ey,bx,by,v[155][155],lastx[155],lasty[155];
struct stu{
  int x,y;
};
struct stu2{
  int x,y;
};
queue<stu> q;
vector<stu2> vec;
void bfs(){
    q.push({bx,by});
    v[bx][by]=1;
    while(q.size()){
      stu h=q.front();
      q.pop();
      for(int i=0;i<4;i++){
        int nx=h.x+dx[i],ny=h.y+dy[i];
        if(v[nx][ny]==0&&nx>=1&&nx<=n&&ny>=1&&ny<=m&&a[nx][ny]!=1){
          q.push({nx,ny});
          v[nx][ny]=1;
          lastx[nx]=h.x;
          lasty[ny]=h.y;
          if(nx==ex&&ny==ey){
            for(int i=nx,j=ny;;i=lastx[i],j=lasty[j]){
                vec.push_back({i,j});
                if(i==bx&&j==by){
                    return;
                  }
              }
          }
        }
      }
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
      for(int j=1;j<=m;j++){
        cin>>a[i][j];
      }
    }
    cin>>bx>>by>>ex>>ey;
    bfs();
    reverse(vec.begin(),vec.end());
    for(int i=0;i<vec.size();i++){
      cout<<"("<<vec[i].x<<","<<vec[i].y<<")->";
    }
    if(vec.size()==0){
      cout<<"no way";
    }
    return 0;
}

错误原因:

错误在于:

for(int i=nx,j=ny;;i=lastx[i],j=lasty[j]){
  vec.push_back({i,j});
  if(i==bx&&j==by){
    return;
  }
}

这里如果是:1,1->1,2->2,2的顺序 lastx[2]会是1,lasty[2]=1 !!! 所以应该改为2维数组

正确代码:

#include<bits/stdc++.h>
using namespace std;
const int N=255;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
struct stu{
  int x,y,st;
}pre[N][N],z[N*N];
bool vis[N][N];
int n,m,sx,sy,ex,ey,c;
int a[N][N];
int bfs(){
  memset(vis,0,sizeof vis);
  queue<stu> q;
  q.push(stu{sx,sy,0});
  vis[sx][sy]=1;
  while(q.size()){
    stu h=q.front();
    q.pop();
    if(h.x==ex&&h.y==ey){
      return h.st;
    }
    for(int i=0;i<4;i++){
      int nx=h.x+dx[i];
      int ny=h.y+dy[i];
      if(vis[nx][ny]){
        continue;
      }
      if(a[nx][ny]==1){
        continue;
      }
      if(nx<=0||nx>n||ny<=0||ny>m){
        continue;
      }
      vis[nx][ny]=1;
      pre[nx][ny]=stu{h.x,h.y};
      q.push(stu{nx,ny,h.st+1});
    }
  }
  return -1;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
      for(int j=1;j<=m;j++){
        cin>>a[i][j];
      }
    }
    cin>>sx>>sy;
    cin>>ex>>ey;
    if(bfs()!=-1){
        int x=ex,y=ey;
        while(x!=sx||y!=sy){
          c++;
          z[c]={x,y};
          int nx=pre[x][y].x;
          int ny=pre[x][y].y;
          x=nx,y=ny;
        }
        cout<<"("<<sx<<','<<sy<<")";
        for(int i=c;i>=1;i--){
          cout<<"->("<<z[i].x<<","<<z[i].y<<")";
        }
    }else{
      cout<<"no way";
    }
    return 0;
}