2243是骑士问题,八个格子的,BFS,因为要最短路经,所以没有用A*,A*跑不出来,太慢了,因为要搜索到所有解啊!一直更新最优,而BFS,一层一层搜索,第一次得到的便是最短的了!300格子,标记的话,BFS遍历所有时间复杂度也算可以!500MS过!稍微剪枝即可!时间注意!要标记每层已经走过的情况时候要在入队的时候标记!大大降低复杂度!因为出队的时候,有些已经在队里了,但是还没有被标记,现在又让他入队!

1915,也是简单BFS过的,暴搜即可。标记一下。但是先用启发搜索怎么也过不了,TLE,用跑2W次就结束,但是WA,悲剧,用欧几里得距离作启发函数啊,奇怪,在几步之内到达目标附近,可能不是最优解了,因为这样不能像一层一层那样标记了,只能更新最小的情况!很多剪枝的时候要注意语句顺序!还有更新与剪枝的顺序!

  1. #include<iostream> //bfs
  2. #include<string>
  3. #include<queue>
  4. #include<cstring>
  5. using namespace std;
  6. int absnum(int x,int y)
  7. {
  8. if(x>y) return x-y;
  9. else return y-x;
  10. }
  11. bool mark[8][8];
  12. int a[8][8];
  13. int f[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};
  14. int minpath=15;
  15. struct xy
  16. {
  17. int x,y;
  18. int count;
  19. };
  20. void bfs(string from,string end)
  21. {
  22. queue<xy>q;
  23. xy start;
  24. start.x=from[0]-'a';
  25. start.y=from[1]-'1';
  26. start.count=0;
  27. q.push(start);
  28. int endx=end[0]-'a';
  29. int endy=end[1]-'1';
  30. if((start.x==endx&&absnum(start.y,endy)==1)||(start.y==endy&&absnum(start.x,endx)==1))
  31. {
  32. minpath=3;return;
  33. }
  34. while(!q.empty())
  35. {
  36. xy getfront=q.front();q.pop();
  37. mark[getfront.x][getfront.y]=1;
  38. if(getfront.count>6)return; //无启发可以这样,一层一层,最多走7次。
  39. if(getfront.x==endx&&getfront.y==endy)
  40. {
  41. if(minpath>getfront.count)minpath=getfront.count;
  42. }
  43. for(int i=0;i<8;i++)
  44. {
  45. xy next(getfront);
  46. next.x+=f[i][0];
  47. next.y+=f[i][1];
  48. if(next.x>=0&&next.x<8&&next.y>=0&&next.y<8&&mark[next.x][next.y]==0)
  49. {
  50. if(next.count>minpath)continue; //已经多了就不用了
  51. next.count++;
  52. q.push(next);
  53. mark[next.x][next.y]=1; //在这里标记!
  54.  
  55. }
  56. }
  57. }
  58. }
  59. int main()
  60. {
  61. string from,end;
  62. while(cin>>from>>end)
  63. {
  64. minpath=15;
  65. memset(mark,0,sizeof(mark));
  66. bfs(from,end);
  67. cout<<"To get from "<<from<<" to "<<end<<" takes "<<minpath<<" knight moves."<<endl;
  68. }
  69. return 0;
  70. }
  1. #include<iostream> //bfs1915爆搜过
  2. #include<string>
  3. #include<queue>
  4. #include<cstring>
  5. #include<cmath>
  6. using namespace std;
  7. int absnum(int x,int y)
  8. {
  9. if(x>y) return (x-y);
  10. else return (y-x);
  11. }
  12. int mark[301][301];
  13. int f[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};
  14. int minpath=300;
  15. struct xy
  16. {
  17. int x,y;
  18. int count;
  19. double juli;
  20. bool operator <(const xy &a)const
  21. {
  22. return a.juli<juli;
  23. }
  24. };
  25. double hamit(xy f,xy e)
  26. {
  27. double res=0;
  28. int tx=absnum(f.x,e.x);
  29. int ty=absnum(f.y,e.y);
  30. res=sqrt(0.0+tx*tx+ty*ty);
  31. return res;
  32. }
  33. xy from,end;int daxiao;
  34. void bfs(xy from,xy end)
  35. {
  36. priority_queue<xy>q;
  37. xy start;
  38. start.x=from.x;
  39. start.y=from.y;
  40. start.count=0;
  41. start.juli=hamit(start,end);
  42. q.push(start);
  43. mark[start.x][start.y]=-1;
  44. int counted=0;
  45. while(!q.empty())
  46. {
  47. xy getfront=q.top();
  48. counted++;
  49. q.pop();
  50. if(counted>10000)
  51. {
  52. if(hamit(getfront,end)>6)continue;
  53. }
  54. if(getfront.x==end.x&&getfront.y==end.y)
  55. {
  56. if(minpath>getfront.count)
  57. {
  58. minpath=getfront.count;
  59. }
  60. continue;
  61. }
  62. for(int i=0;i<8;i++)
  63. {
  64. xy next(getfront);
  65. next.x+=f[i][0];
  66. next.y+=f[i][1];
  67. if(next.x>=0&&next.x<daxiao&&next.y>=0&&next.y<daxiao&&next.count<mark[next.x][next.y])
  68. {
  69. next.juli=hamit(next,end);
  70. next.count++;
  71. if(next.count>=minpath)continue; //比最优差,剪枝!
  72. if(next.count>daxiao/2+3)continue;
  73. if(mark[next.x][next.y]>next.count)
  74. {
  75. mark[next.x][next.y]=next.count;
  76. }
  77. if(next.x==end.x&&next.y==end.y)
  78. {
  79. if(minpath>next.count)
  80. {
  81. minpath=next.count;
  82. }
  83. continue;
  84. }
  85. q.push(next);
  86. }
  87. }
  88. }
  89. return;
  90. }
  91. int main()
  92. {
  93. int na;cin>>na;
  94. while(na--)
  95. {
  96. cin>>daxiao;
  97. cin>>from.x>>from.y>>end.x>>end.y;
  98. minpath=daxiao;
  99. memset(mark,0x3f3f3f3f,sizeof(mark));
  100. bfs(from,end);
  101. cout<<minpath<<endl;
  102. }
  103. return 0;
  104. }
  1. #include<iostream>     //bfs,A*/WA不解ing
  2. #include<string>
  3. #include<queue>
  4. #include<cstring>
  5. #include<cmath>
  6. using namespace std;
  7. int  absnum(int x,int y)
  8. {
  9.     if(x>y) return (x-y);
  10.     else return (y-x);
  11. }
  12. int mark[301][301];
  13. int f[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};
  14. int minpath=300;
  15. struct xy
  16. {
  17.     int x,y;
  18.     int count;
  19.     double juli;
  20.     bool operator <(const xy &a)const
  21.     {
  22.         return a.juli<juli;
  23.     }
  24. };
  25. double hamit(xy f,xy e)
  26. {
  27.      double res=0;
  28.      int tx=absnum(f.x,e.x);
  29.      int ty=absnum(f.y,e.y);
  30.     res=sqrt(0.0+tx*tx+ty*ty);
  31.     return res;
  32. }
  33. xy from,end;int daxiao;
  34. void bfs(xy from,xy end)
  35. {
  36.     priority_queue<xy>q;
  37.     xy start;
  38.     start.x=from.x;
  39.     start.y=from.y;
  40.     start.count=0;
  41.     start.juli=hamit(start,end);
  42.     q.push(start);
  43.     mark[start.x][start.y]=-1;
  44.    // int counted=0;
  45.     while(!q.empty())
  46.     {
  47.         xy getfront=q.top();
  48.        // counted++;
  49.         //cout<<counted<<"kkk"<<endl;
  50.         q.pop();
  51.       /*  if(counted>20000)  //到达2W次,差不多可以出来!
  52.         {
  53.             return;
  54.         }*/
  55.       //  cout<<getfront.count<<":"<<getfront.x<<"-"<<getfront.y<<endl;
  56.        // if( mark[getfront.x][getfront.y]>getfront.count) mark[getfront.x][getfront.y]=getfront.count;
  57.         for(int i=0;i<8;i++)
  58.         {
  59.             xy next(getfront);
  60.             next.x+=f[i][0];
  61.             next.y+=f[i][1];
  62.             if(next.x>=0&&next.x<daxiao&&next.y>=0&&next.y<daxiao)
  63.             //有优先队列后已经不是BFS本质,步数多的以及访问过,少的还可访问!不是简单标记!
  64.             {
  65.                 next.juli=hamit(next,end);
  66.                       next.count++;
  67.                   if(next.count>=mark[next.x][next.y])continue; //注意这几个语句之间顺序!continue很重要!
  68.                    mark[next.x][next.y]=next.count;
  69.                  if(next.x==end.x&&next.y==end.y)
  70.                      {
  71.                            if(minpath>next.count)
  72.                             {
  73.                              minpath=next.count;
  74.                             }
  75.                         continue;
  76.                     }
  77.                   if(next.count>=minpath)continue;     //比最优差,剪枝!
  78.                  // if(next.count>daxiao/2+3)continue;
  79.                 q.push(next);
  80.             }
  81.         }
  82.     }
  83.     return;
  84. }
  85. int main()
  86. {
  87.     int na;cin>>na;
  88.     while(na--)
  89.     {
  90.         cin>>daxiao;
  91.         cin>>from.x>>from.y>>end.x>>end.y;
  92.         minpath=daxiao;
  93.         memset(mark,0x3f3f3f3f,sizeof(mark));
  94.         bfs(from,end);
  95.         cout<<minpath<<endl;
  96.     }
  97.     return 0;
  98. }

poj2243+poj1915骑士问题的更多相关文章

  1. COGS746. [网络流24题] 骑士共存

    骑士共存问题«问题描述:在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘 上某些方格设置了障碍,骑士不得进入. «编程任务:对于给定的n*n个方格的国际象棋棋盘和障碍标志 ...

  2. 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS

    [Usaco2005 Dec]Knights of Ni 骑士 Description  贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...

  3. 骑士游历/knight tour - visual basic 解决

    在visual baisc 6 how to program 中文版第七章的练习题上看到了这个问题,骑士游历的问题. 在8x8的国际象棋的棋盘上,骑士(走法:一个方向走两格,另一个方向一格)不重复走完 ...

  4. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  5. BZOJ 1040 【ZJOI2008】 骑士

    Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...

  6. 【BZOJ-1040】骑士 树形DP + 环套树 + DFS

    1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3312  Solved: 1269[Submit][Status ...

  7. BZOJ1040 [ZJOI2008]骑士

    Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各 界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战 ...

  8. BFS 骑士的移动

    骑士的移动 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83498#problem/E 题目: Description A f ...

  9. LA 3523 圆桌骑士

    题目链接:http://vjudge.net/contest/141787#problem/A http://poj.org/problem?id=2942 此题很经典 知识点:DFS染色,点-双连通 ...

随机推荐

  1. obj.style 和currentstyle 等区别

    版权声明:本文为博主原创文章,未经博主允许不得转载. 获取样式  obj.style   和currentstyle  等区别   obj.style只能获得内嵌样式(inline Style)就是写 ...

  2. CCF|火车购票|Java|80分

    import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Sc ...

  3. android studio 生成 jniLibs 目录

    现在一般的项目都会加入第三方jar包,第三方jar包我们会新建一个文件夹:libs,然后jar包都放在这个文件夹中. 但我们会发现,只是新建一个文件加之后,在AndroidStudio的左侧并不会出现 ...

  4. vijos 1772 巧妙填数

    描述 将1,2,\cdots,91,2,⋯,9共99个数分成三组,分别组成三个三位数,且使这三个三位数构成1:2:31:2:3的比例. 试求出所有满足条件的三个三位数.例如:三个三位数192,384, ...

  5. Java异常归纳

      1.使用Tomcat运行“播报哥架构”出现的两大异常 1.1 监听器异常 详细情况:部署好Maven项目,启动TOMCAT提示如下错误 java.lang.ClassNotFoundExcepti ...

  6. axios 里面 then 默认写的function里面没有this,改成箭头函数后就可以用this了

    ,methods:{ loadJson:function(){ //this.jsonTest = "jjj" this.$http.get('http://localhost:3 ...

  7. 查看python关键字

    打开命令窗口 输入python-——help()——keywords

  8. 转行做web前端,该如何进行短期快速自学,达到高新就业水平

    就目前来说,毕业生如果想毕业就找到高薪的工作,互联网成为了第一个选择,在所有的职业中,不靠任何关系,全凭自己的能力就业,就是程序开发,而web前端开发是目最很热门的行业,在未来五年之内,web前端开发 ...

  9. Microsoft Windows Server

    Microsoft Windows Server Microsoft Windows Microsoft Windows 是微软推出的个人版操作系统: Microsoft Windows Server ...

  10. U盘制作安装盘后容量不能恢复的解决方案

    diskpartlist diskselect disk 0/1 --看具体U盘是0还是1clean