• ZISUOJ 数据结构--队列及其应用


    说明:

            基本都是bfs的常见模板题型,思路都很直接,不过后面有两道题很搞心态,它们给的坐标x、y是反的,导致刚开始一直错。题目还是要看仔细,不能先入为主。

    题目列表:

    问题 A: 围圈报数(完善程序) 

    参考题解:

    1. #include
    2. #include
    3. using namespace std;
    4. int n,m,k=1,tmp;
    5. queue<int> arr;
    6. int main()
    7. {
    8. cin>>n>>m;
    9. for(int i=1;i<=n;i++)
    10. arr.push(i);
    11. // _____(1)____//依次进入队列
    12. while(arr.size())
    13. // while(_____(2)_____)//判断队列里是否还有人
    14. {
    15. tmp=arr.front();
    16. if(k%m==0)
    17. cout<" ";
    18. else
    19. arr.push(tmp);
    20. // ______(3)______//如果不是第m个人,则重新入队
    21. // _____(4)_____//从队列里删除
    22. arr.pop();
    23. k++;
    24. }
    25. return 0;
    26. }

    问题 B: 围圈报数

    参考题解:

    1. #include
    2. #include
    3. using std::cin;
    4. using std::cout;
    5. int main() {
    6. cin.tie(nullptr)->sync_with_stdio(false);
    7. int n,m,count = 0;cin >> n >> m;
    8. std::queue<int> q;
    9. for(int i = 1;i<=n;i++) q.push(i);
    10. while(q.size()){
    11. auto t = q.front();
    12. count++;
    13. if(count%m==0) {
    14. cout << t << ' ';
    15. }else {
    16. q.push(t);
    17. }
    18. q.pop();
    19. }
    20. cout << std::endl;
    21. return 0;
    22. }

    问题 C: 报数相同次数circle

    参考题解:

    1. #include
    2. #include
    3. using std::cin;
    4. using std::cout;
    5. int main() {
    6. cin.tie(nullptr)->sync_with_stdio(false);
    7. int n;cin >> n;
    8. int a,b;cin >> a >> b;
    9. std::queue<int> q1,q2;
    10. for(int i = 1;i<=a;i++) q1.push(i);
    11. for(int i = 1;i<=b;i++) q2.push(i);
    12. int count = 0;
    13. for(int i = 1;i<=n;i++) {
    14. auto t1 = q1.front(),t2 = q2.front();
    15. if(t1==t2) count++;
    16. q1.push(t1),q2.push(t2);
    17. q1.pop(),q2.pop();
    18. }
    19. cout << count << std::endl;
    20. return 0;
    21. }

    问题 D: 最小倍数

    参考题解:

    1. #include
    2. #include
    3. using std::cin;
    4. using std::cout;
    5. using ll = long long;
    6. ll n,x;
    7. void bfs() {
    8. std::queue q;
    9. q.push(1);
    10. while(q.size()) {
    11. x = q.front();q.pop();
    12. if(x%n==0&&x>=n) {
    13. cout << x << '\n';
    14. return;
    15. }
    16. q.push(x*10);
    17. q.push(x*10+1);
    18. }
    19. cout << x << '\n';
    20. }
    21. int main() {
    22. cin.tie(nullptr)->sync_with_stdio(false);
    23. while(cin >> n,n) {
    24. bfs();
    25. }
    26. return 0;
    27. }

    问题 E: 迷宫的最短路径

    参考题解:

    1. #include
    2. #include
    3. #include
    4. using std::cin;
    5. using std::cout;
    6. int main() {
    7. cin.tie(nullptr)->sync_with_stdio(false);
    8. constexpr int N = 1e2+5;
    9. int sx = 1,sy = 1,ex = 1,ey = 1;
    10. struct node {
    11. int x,y,s;
    12. }t,t1;
    13. char g[N][N];
    14. bool vis[N][N];
    15. memset(vis,false,sizeof vis);
    16. int dx[] = {0,0,-1,1};
    17. int dy[] = {-1,1,0,0};
    18. std::queue q;
    19. int n,m;cin >> n >> m;
    20. for(int i = 1;i<=n;i++) {
    21. for(int j = 1;j<=m;j++) {
    22. cin >> g[i][j];
    23. if(g[i][j]=='S') {
    24. sx=i,sy=j;
    25. }else if(g[i][j]=='G') {
    26. ex=i,ey=j;
    27. }
    28. }
    29. }
    30. auto bfs = [&]()->void{
    31. t.x = sx,t.y = sy,t.s = 0;
    32. q.push(t);
    33. vis[sx][sy] = true;
    34. while(!q.empty()) {
    35. t = q.front();q.pop();
    36. if(t.x==ex&&t.y==ey) {
    37. cout << "The min steps are:" << t.s << "!\n";
    38. return;
    39. }
    40. for(int i = 0;i<4;i++) {
    41. int u = t.x+dx[i],v = t.y+dy[i];
    42. if(u<1||u>n||v<1||v>m||vis[u][v]||g[u][v]=='#') continue;
    43. vis[u][v] = true;
    44. t1.x = u,t1.y = v,t1.s = t.s+1;
    45. q.push(t1);
    46. }
    47. }
    48. cout << "sorry!\n";
    49. };
    50. bfs();
    51. return 0;
    52. }

    问题 F: 象棋中的马之进阶

    参考题解:

    1. #include
    2. #include
    3. #include
    4. using std::cin;
    5. using std::cout;
    6. int main() {
    7. cin.tie(nullptr)->sync_with_stdio(false);
    8. constexpr int N = 15;
    9. struct node {
    10. int x,y,s;
    11. }t,t1;
    12. int dx[] = {-1,1,2,2,1,-1,-2,-2};
    13. int dy[] = {2,2,1,-1,-2,-2,-1,1};
    14. int sx,sy,ex,ey;
    15. bool vis[N][N];
    16. memset(vis,false,sizeof vis);
    17. cin >> sy >> sx >> ey >> ex;
    18. std::queue q;
    19. auto bfs = [&]() ->void {
    20. t.x = sx,t.y = sy,t.s = 0;
    21. vis[sx][sy] = true;
    22. q.push(t);
    23. while(!q.empty()) {
    24. t = q.front();q.pop();
    25. if(t.x==ex&&t.y==ey) {
    26. cout << t.s << std::endl;
    27. return;
    28. }
    29. for(int i = 0;i<8;i++) {
    30. int u = t.x+dx[i],v = t.y+dy[i];
    31. if(u<1||u>10||v<1||v>9||vis[u][v]) continue;
    32. vis[u][v] = true;
    33. t1.x = u,t1.y = v,t1.s = t.s+1;
    34. q.push(t1);
    35. }
    36. }
    37. cout << 0 << std::endl;
    38. };
    39. bfs();
    40. return 0;
    41. }

     

    问题 G: 迷宫探险

    参考题解:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using std::cin;
    6. using std::cout;
    7. int main() {
    8. cin.tie(nullptr)->sync_with_stdio(false);
    9. constexpr int N = 1e2+5;
    10. struct node {
    11. int x,y,s;
    12. bool operator > (const node &W) const {
    13. return s > W.s;
    14. }
    15. }t,t1;
    16. char g[N][N];
    17. bool vis[N][N];
    18. int dx[] = {0,0,-1,1};
    19. int dy[] = {-1,1,0,0};
    20. int n,sx = 1,sy = 1,ex = 1,ey = 1;
    21. std::priority_queue,std::greater> pq;
    22. auto bfs = [&]() ->void {
    23. while(!pq.empty()) pq.pop();
    24. memset(vis,false,sizeof vis);
    25. t.x = sx,t.y = sy,t.s = 0;
    26. vis[sx][sy] = true;
    27. pq.push(t);
    28. while(!pq.empty()) {
    29. t = pq.top();pq.pop();
    30. if(t.x==ex&&t.y==ey) {
    31. cout << t.s << '\n';
    32. return;
    33. }
    34. for(int i = 0;i<4;i++) {
    35. int u = t.x+dx[i],v = t.y+dy[i];
    36. if(u<1||u>n||v<1||v>n||vis[u][v]||g[u][v]=='#') continue;
    37. vis[u][v] = true;
    38. int ds = 1;
    39. if(g[u][v]!='.') ds += int(g[u][v]^48);
    40. t1.x = u,t1.y = v,t1.s = t.s+ds;
    41. pq.push(t1);
    42. }
    43. }
    44. cout << -1 << '\n';
    45. };
    46. while(cin >> n) {
    47. ex = n,ey = n;
    48. for(int i = 1;i<=n;i++) {
    49. for(int j = 1;j<=n;j++) {
    50. cin >> g[i][j];
    51. }
    52. }
    53. bfs();
    54. }
    55. return 0;
    56. }

    问题 H: 迷宫

     

    参考题解:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using std::cin;
    6. using std::cout;
    7. int main() {
    8. cin.tie(nullptr)->sync_with_stdio(false);
    9. constexpr int N = 1e2+5;
    10. struct node {
    11. int x,y,s;
    12. }t,t1;
    13. char g[N][N];
    14. bool vis[N][N];
    15. int dx[] = {0,0,-1,1};
    16. int dy[] = {-1,1,0,0};
    17. int sx,sy,ex,ey,k,n,m;
    18. std::queue q;
    19. auto bfs = [&]()->void {
    20. memset(vis,false,sizeof vis);
    21. while(!q.empty()) q.pop();
    22. t.x = sx,t.y = sy,t.s = -1;
    23. vis[sx][sy] = true;
    24. q.push(t);
    25. while(!q.empty()) {
    26. t = q.front();q.pop();
    27. if(t.x==ex&&t.y==ey&&t.s<=k) {
    28. cout << "yes\n";
    29. return;
    30. }
    31. for(int i = 0;i<4;i++) {
    32. t1.x = t.x+dx[i],t1.y = t.y+dy[i];
    33. int &u = t1.x,&v = t1.y;
    34. while(u>=1&&u<=n&&v>=1&&v<=m&&g[u][v]=='.') {
    35. if(!vis[u][v]) {
    36. t1.s=t.s+1;
    37. vis[u][v] = true;
    38. q.push(t1);
    39. }
    40. u+=dx[i],v+=dy[i];
    41. }
    42. }
    43. }
    44. cout << "no\n";
    45. };
    46. int T = 1;cin >> T;
    47. while(T--) {
    48. cin >> n >> m;
    49. for(int i = 1;i<=n;i++) {
    50. for(int j = 1;j<=m;j++) {
    51. cin >> g[i][j];
    52. }
    53. }
    54. cin >> k >> sy >> sx >> ey >> ex;
    55. bfs();
    56. }
    57. return 0;
    58. }

  • 相关阅读:
    mysql修改root用户的密码
    ZEMAX | 在OpticStudio中通过几何光线追迹来模拟杨氏双缝干涉实验
    mac电脑系统清理软件CleanMyMac X2024破解版下载
    Android NDK 实现视音频播放器源码
    信息系统项目管理师必背核心考点(六十三)项目组合管理的主要过程&DIPP分析
    MySQL版数据库原理与应用期末复习重点(1)---关系代数(除运算和自连接查询、手写例题)
    【牛客刷题】带你在牛客刷题第一弹(C/C++语言基础题)
    基于Django的博客系统之增加手机验证码登录(九)
    第十六章:构建n(5,7)阶素数幻方
    【工具流】WSL2安装
  • 原文地址:https://blog.csdn.net/Beau_Will/article/details/138094632