当前位置:   article > 正文

蓝桥杯每日一题(BFS)

蓝桥杯每日一题(BFS)

1562 微博转发

开始思路错误点:在用拉链法保存关注信息的时候,因为要看一个用户发的有多少转发的,所以要以用户为坑位,所有关注这个坑位的用户为链表。(开始弄反了)

e数组存某个用户的idx,ne是某个节点在链表上的下一个,h是坑位。

st状态数组的参数是用户,而不是idx

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //1562 微博转发
  4. const int N=1e3+10;
  5. int n,l;
  6. //拉链法存储
  7. int e[N],idx,ne[N],h[N];
  8. bool st[N];
  9. //e某个idx对应的用户是谁
  10. //ne 这个用户和其他人是否被i同时关注
  11. //h代表某个用户的关注列表的链表尾部
  12. void add(int u,int x)
  13. {
  14. // 把某个被关注的用户加入到某个用户的链表中
  15. e[idx]=x,ne[idx]=h[u],h[u]=idx++;
  16. }
  17. int bfs(int s)//找某个节点的第l层关注的用户个数
  18. {
  19. //之前都是只要有后继就加入到队列中
  20. //如何实现到规定的层的位置就终止呢
  21. //就是L层循环:每次循环都控制向外延伸一层
  22. memset(st,0,sizeof(st));
  23. queue<int>q;
  24. q.push(s);
  25. st[s]=1;
  26. int res=0;//前l层有多少个点
  27. for(int i=1;i<=l;i++)//向前走l层
  28. {
  29. int num=q.size();
  30. //num就是上一次循环扩展出来的点
  31. while(num--)
  32. {
  33. int t=q.front();
  34. q.pop();
  35. for(int j=h[t];j!=-1;j=ne[j])
  36. {
  37. if(!st[e[j]])
  38. {
  39. q.push(e[j]);
  40. res++;
  41. st[e[j]]=1;
  42. }
  43. }
  44. }
  45. }
  46. return res;
  47. }
  48. int main()
  49. {
  50. cin>>n>>l;
  51. memset(h,-1,sizeof(h));
  52. for(int i=1;i<=n;i++)
  53. {
  54. int x;
  55. cin>>x;
  56. while(x--)
  57. {
  58. int u;
  59. cin>>u;
  60. add(u,i);//i关注了u
  61. }
  62. }
  63. int qu;
  64. cin>>qu;
  65. while(qu--)
  66. {
  67. int user;
  68. cin>>user;
  69. cout<<bfs(user)<<endl;
  70. }
  71. }

//844走迷宫

自己是用pair并且second放走的步数,内存超限了。

下面是自己的做法

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //844走迷宫
  4. typedef pair<int,int>PII;
  5. typedef pair<PII,int> PIII;
  6. const int N=100;
  7. int p[N][N];
  8. int n,m;
  9. int s[4]={0,1,0,-1};
  10. int z[4]={1,0,-1,0};
  11. int dfs()
  12. {
  13. queue<PIII>q;
  14. q.push({{1,1},0});
  15. while(!q.empty())
  16. {
  17. PIII t=q.front();
  18. q.pop();
  19. PII zuo=t.first;
  20. int x=zuo.first;
  21. int y=zuo.second;
  22. for(int i=0;i<4;i++)
  23. {
  24. if(x+z[i]>=1&&x+z[i]<=n&&y+s[i]>=1&&y+s[i]<=m&&!p[x+z[i]][y+s[i]])
  25. {
  26. q.push({{x+z[i],y+s[i]},t.second+1});
  27. if(x+z[i]==n&&y+s[i]==m)
  28. {
  29. //cout<<t.second+1<<endl;
  30. return t.second+1;
  31. }
  32. }
  33. }
  34. }
  35. }
  36. int main()
  37. {
  38. cin>>n>>m;
  39. for(int i=1;i<=n;i++)
  40. {
  41. for(int j=1;j<=m;j++)
  42. {
  43. int num;
  44. cin>>num;
  45. if(num)p[i][j]=1;
  46. }
  47. }
  48. cout<< dfs();
  49. }

y做法:直接使用一个二维数组存某个坐标到达的时候走的步数。

845 八数码 

bfs求最短路的问题中,重点是怎么保存状态,y的做法:把矩阵转化为字符串,从初始位置到这个状态走的距离就是变化的次数

太巧妙了;

//看完y思路之后,最后怎么都是输出不对,最后找到是swap(还原的那个swap)应该也在if条件中,就是在4层的for循环中不一定都符合不越界的要求。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //845 八数码
  4. //既可以存状态,又可以存步数
  5. unordered_map<string ,int>d;//记录到这个状态转换了多少次
  6. queue<string>q;//所有的状态
  7. string start="12345678x";
  8. int ss[4]={-1, 0, 1, 0},z[4]={0, 1, 0, -1};
  9. int bfs(string s)
  10. {
  11. q.push(s);
  12. d[s]=0;
  13. while(q.size())
  14. {
  15. string t=q.front();
  16. q.pop();
  17. int p=t.find('x');
  18. int y=p/3;
  19. int x=p%3;
  20. int dis=d[t];
  21. if(t==start)return d[t];
  22. for(int i=0;i<4;i++)
  23. {
  24. // cout<<" ********"<<endl;
  25. int xx=x+z[i];
  26. int yy=y+ss[i];
  27. //cout<<xx<<" "<<yy<<" "<<"si::"<<ss[i]<<endl;
  28. if(xx>=0&&xx<3&&yy>=0&&yy<3)
  29. {
  30. swap(t[p],t[yy*3+xx]);
  31. //cout<<"yun"<<endl;
  32. if(!d.count(t))
  33. {
  34. d[t]=dis+1;
  35. q.push(t);
  36. }
  37. swap(t[p],t[yy*3+xx]);
  38. }
  39. //if(t==start)return d[t];
  40. }
  41. }
  42. return -1;
  43. }
  44. int main()
  45. {
  46. string s;
  47. for(int i=0;i<9;i++)
  48. {
  49. string c;
  50. cin>>c;
  51. s+=c;
  52. }
  53. //cout<<s<<endl;
  54. cout<<bfs(s)<<endl;
  55. }

//1233全球变暖

自己做的WA,先用 bfs看有几个岛屿,淹没操作后,再用bfs看。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //1233 全球变暖
  4. typedef pair<int,int>PII;
  5. const int N=1010;
  6. int n;
  7. int p[N][N],f[N][N];
  8. int st[N][N];
  9. int s[4]={1,0,0,-1},z[4]={0,1,-1,0};
  10. int bfs()
  11. {
  12. int cnt=0;
  13. memset(st,0,sizeof(st));
  14. queue<PII>q;
  15. for(int i=1;i<=n;i++)
  16. {
  17. for(int j=1;j<=n;j++)
  18. {
  19. if(!st[i][j]&&p[i][j])//没有被访问过的一片陆地
  20. {
  21. // cout<<"find that match if"<<endl;
  22. st[i][j]=1;
  23. cnt++;
  24. //cout<<"cnt "<<cnt<<endl;
  25. q.push({i,j});
  26. while(q.size())
  27. {
  28. PII t=q.front();
  29. //cout<<q.front().first<<" "<<q.front().second<<endl;
  30. q.pop();
  31. for(int k=0;k<4;k++)
  32. {
  33. int x=t.first+s[k];
  34. int y=t.second+z[k];
  35. if(x>=1&&x<=n&&y>=1&&y<=n&&p[x][y]&&!st[x][y])
  36. {
  37. q.push({x,y});
  38. st[x][y]=1;
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. return cnt;
  46. }
  47. int main()
  48. {
  49. cin>>n;
  50. for(int i=1;i<=n;i++)
  51. {
  52. for(int j=1;j<=n;j++)
  53. {
  54. char c;
  55. cin>>c;
  56. if(c=='#')p[i][j]=1,f[i][j]=1;
  57. }
  58. }
  59. //查看有几个岛屿
  60. int precnt=bfs();
  61. //淹没
  62. for(int i=1;i<=n;i++)
  63. {
  64. for(int j=1;j<=n;j++)
  65. {
  66. if(!f[i][j])//如果本来是一个海域
  67. {
  68. for(int k=0;k<4;k++)
  69. {
  70. //cout<<"dawn"<<endl;
  71. int x=i+s[k];
  72. int y=j+z[k];
  73. if(x>=1&&x<=n&&y>=1&&y<=n&&p[x][y])
  74. {
  75. p[x][y]=0;//被淹没
  76. }
  77. }
  78. }
  79. }
  80. }
  81. //还有几个岛屿
  82. int poscnt=bfs();
  83. int num=precnt-poscnt;
  84. cout<<num<<endl;
  85. }

y是在bfs的时候就在4的for循环中判断,这个连通块是否被淹没,是就加加。最后根据这个连通块的节点个数和被淹没的节点个数判断这个岛屿是否被淹没

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/302822?site
推荐阅读
相关标签
  

闽ICP备14008679号