POJ3278

快来打我* 2021-11-29 09:56 157阅读 0赞

  之前用数组存的每一个位置的步数情况,开的数组大小是100010,过了,后来想改成结构体写,结构体只用再定义一个标记数组,我标记数组也开的100010,然后就RE了,开成200000就过了

  完整代码

  

  1. #include <iostream>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5. int k,vis[200000];
  6. int u[3]={
  7. 1,2,3};
  8. struct node
  9. {
  10. int p,s;
  11. node(int a,int b)
  12. {
  13. p=a;
  14. s=b;
  15. }
  16. };
  17. void bfs(int n)
  18. {
  19. queue<node> q;
  20. q.push(node(n,0));
  21. while(!q.empty())
  22. {
  23. node t=q.front();
  24. q.pop();
  25. if(t.p==k)
  26. {
  27. cout<<t.s<<endl;
  28. return ;
  29. }
  30. for(int i=0;i<3;i++)
  31. {
  32. int tx=t.p;
  33. if(u[i]==1)//左移
  34. {
  35. if(!vis[tx-1] && tx-1>=0 && tx-1<=100000)
  36. {
  37. vis[tx-1]=1;
  38. q.push(node(tx-1,t.s+1));
  39. }
  40. }
  41. else if(u[i]==2)//右移
  42. {
  43. if(!vis[tx+1] && tx+1>=0 && tx+1<=100000)
  44. {
  45. vis[tx+1]=1;
  46. q.push(node(tx+1,t.s+1));
  47. }
  48. }
  49. else if(u[i]==3)//乘2
  50. {
  51. if(!vis[tx*2] && tx*2>=0 && tx*2<=100000)
  52. {
  53. vis[tx*2]=1;
  54. q.push(node(tx*2,t.s+1));
  55. }
  56. }
  57. }
  58. }
  59. }
  60. int main()
  61. {
  62. int n;
  63. cin>>n>>k;
  64. vis[n]=1;
  65. bfs(n);
  66. return 0;
  67. }

转载于:https://www.cnblogs.com/benzikun/p/11210612.html

发表评论

表情:
评论列表 (有 0 条评论,157人围观)

还没有评论,来说两句吧...

相关阅读

    相关 POJ3278

      之前用数组存的每一个位置的步数情况,开的数组大小是100010,过了,后来想改成结构体写,结构体只用再定义一个标记数组,我标记数组也开的100010,然后就RE了,开成20