poj2243+poj1915骑士问题
2243是骑士问题,八个格子的,BFS,因为要最短路经,所以没有用A*,A*跑不出来,太慢了,因为要搜索到所有解啊!一直更新最优,而BFS,一层一层搜索,第一次得到的便是最短的了!300格子,标记的话,BFS遍历所有时间复杂度也算可以!500MS过!稍微剪枝即可!时间注意!要标记每层已经走过的情况时候要在入队的时候标记!大大降低复杂度!因为出队的时候,有些已经在队里了,但是还没有被标记,现在又让他入队!
1915,也是简单BFS过的,暴搜即可。标记一下。但是先用启发搜索怎么也过不了,TLE,用跑2W次就结束,但是WA,悲剧,用欧几里得距离作启发函数啊,奇怪,在几步之内到达目标附近,可能不是最优解了,因为这样不能像一层一层那样标记了,只能更新最小的情况!很多剪枝的时候要注意语句顺序!还有更新与剪枝的顺序!
- #include<iostream> //bfs
- #include<string>
- #include<queue>
- #include<cstring>
- using namespace std;
- int absnum(int x,int y)
- {
- if(x>y) return x-y;
- else return y-x;
- }
- bool mark[8][8];
- int a[8][8];
- int f[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};
- int minpath=15;
- struct xy
- {
- int x,y;
- int count;
- };
- void bfs(string from,string end)
- {
- queue<xy>q;
- xy start;
- start.x=from[0]-'a';
- start.y=from[1]-'1';
- start.count=0;
- q.push(start);
- int endx=end[0]-'a';
- int endy=end[1]-'1';
- if((start.x==endx&&absnum(start.y,endy)==1)||(start.y==endy&&absnum(start.x,endx)==1))
- {
- minpath=3;return;
- }
- while(!q.empty())
- {
- xy getfront=q.front();q.pop();
- mark[getfront.x][getfront.y]=1;
- if(getfront.count>6)return; //无启发可以这样,一层一层,最多走7次。
- if(getfront.x==endx&&getfront.y==endy)
- {
- if(minpath>getfront.count)minpath=getfront.count;
- }
- for(int i=0;i<8;i++)
- {
- xy next(getfront);
- next.x+=f[i][0];
- next.y+=f[i][1];
- if(next.x>=0&&next.x<8&&next.y>=0&&next.y<8&&mark[next.x][next.y]==0)
- {
- if(next.count>minpath)continue; //已经多了就不用了
- next.count++;
- q.push(next);
- mark[next.x][next.y]=1; //在这里标记!
- }
- }
- }
- }
- int main()
- {
- string from,end;
- while(cin>>from>>end)
- {
- minpath=15;
- memset(mark,0,sizeof(mark));
- bfs(from,end);
- cout<<"To get from "<<from<<" to "<<end<<" takes "<<minpath<<" knight moves."<<endl;
- }
- return 0;
- }
- #include<iostream> //bfs1915爆搜过
- #include<string>
- #include<queue>
- #include<cstring>
- #include<cmath>
- using namespace std;
- int absnum(int x,int y)
- {
- if(x>y) return (x-y);
- else return (y-x);
- }
- int mark[301][301];
- int f[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};
- int minpath=300;
- struct xy
- {
- int x,y;
- int count;
- double juli;
- bool operator <(const xy &a)const
- {
- return a.juli<juli;
- }
- };
- double hamit(xy f,xy e)
- {
- double res=0;
- int tx=absnum(f.x,e.x);
- int ty=absnum(f.y,e.y);
- res=sqrt(0.0+tx*tx+ty*ty);
- return res;
- }
- xy from,end;int daxiao;
- void bfs(xy from,xy end)
- {
- priority_queue<xy>q;
- xy start;
- start.x=from.x;
- start.y=from.y;
- start.count=0;
- start.juli=hamit(start,end);
- q.push(start);
- mark[start.x][start.y]=-1;
- int counted=0;
- while(!q.empty())
- {
- xy getfront=q.top();
- counted++;
- q.pop();
- if(counted>10000)
- {
- if(hamit(getfront,end)>6)continue;
- }
- if(getfront.x==end.x&&getfront.y==end.y)
- {
- if(minpath>getfront.count)
- {
- minpath=getfront.count;
- }
- continue;
- }
- for(int i=0;i<8;i++)
- {
- xy next(getfront);
- next.x+=f[i][0];
- next.y+=f[i][1];
- if(next.x>=0&&next.x<daxiao&&next.y>=0&&next.y<daxiao&&next.count<mark[next.x][next.y])
- {
- next.juli=hamit(next,end);
- next.count++;
- if(next.count>=minpath)continue; //比最优差,剪枝!
- if(next.count>daxiao/2+3)continue;
- if(mark[next.x][next.y]>next.count)
- {
- mark[next.x][next.y]=next.count;
- }
- if(next.x==end.x&&next.y==end.y)
- {
- if(minpath>next.count)
- {
- minpath=next.count;
- }
- continue;
- }
- q.push(next);
- }
- }
- }
- return;
- }
- int main()
- {
- int na;cin>>na;
- while(na--)
- {
- cin>>daxiao;
- cin>>from.x>>from.y>>end.x>>end.y;
- minpath=daxiao;
- memset(mark,0x3f3f3f3f,sizeof(mark));
- bfs(from,end);
- cout<<minpath<<endl;
- }
- return 0;
- }
- #include<iostream> //bfs,A*/WA不解ing
- #include<string>
- #include<queue>
- #include<cstring>
- #include<cmath>
- using namespace std;
- int absnum(int x,int y)
- {
- if(x>y) return (x-y);
- else return (y-x);
- }
- int mark[301][301];
- int f[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};
- int minpath=300;
- struct xy
- {
- int x,y;
- int count;
- double juli;
- bool operator <(const xy &a)const
- {
- return a.juli<juli;
- }
- };
- double hamit(xy f,xy e)
- {
- double res=0;
- int tx=absnum(f.x,e.x);
- int ty=absnum(f.y,e.y);
- res=sqrt(0.0+tx*tx+ty*ty);
- return res;
- }
- xy from,end;int daxiao;
- void bfs(xy from,xy end)
- {
- priority_queue<xy>q;
- xy start;
- start.x=from.x;
- start.y=from.y;
- start.count=0;
- start.juli=hamit(start,end);
- q.push(start);
- mark[start.x][start.y]=-1;
- // int counted=0;
- while(!q.empty())
- {
- xy getfront=q.top();
- // counted++;
- //cout<<counted<<"kkk"<<endl;
- q.pop();
- /* if(counted>20000) //到达2W次,差不多可以出来!
- {
- return;
- }*/
- // cout<<getfront.count<<":"<<getfront.x<<"-"<<getfront.y<<endl;
- // if( mark[getfront.x][getfront.y]>getfront.count) mark[getfront.x][getfront.y]=getfront.count;
- for(int i=0;i<8;i++)
- {
- xy next(getfront);
- next.x+=f[i][0];
- next.y+=f[i][1];
- if(next.x>=0&&next.x<daxiao&&next.y>=0&&next.y<daxiao)
- //有优先队列后已经不是BFS本质,步数多的以及访问过,少的还可访问!不是简单标记!
- {
- next.juli=hamit(next,end);
- next.count++;
- if(next.count>=mark[next.x][next.y])continue; //注意这几个语句之间顺序!continue很重要!
- mark[next.x][next.y]=next.count;
- if(next.x==end.x&&next.y==end.y)
- {
- if(minpath>next.count)
- {
- minpath=next.count;
- }
- continue;
- }
- if(next.count>=minpath)continue; //比最优差,剪枝!
- // if(next.count>daxiao/2+3)continue;
- q.push(next);
- }
- }
- }
- return;
- }
- int main()
- {
- int na;cin>>na;
- while(na--)
- {
- cin>>daxiao;
- cin>>from.x>>from.y>>end.x>>end.y;
- minpath=daxiao;
- memset(mark,0x3f3f3f3f,sizeof(mark));
- bfs(from,end);
- cout<<minpath<<endl;
- }
- return 0;
- }
poj2243+poj1915骑士问题的更多相关文章
- COGS746. [网络流24题] 骑士共存
骑士共存问题«问题描述:在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘 上某些方格设置了障碍,骑士不得进入. «编程任务:对于给定的n*n个方格的国际象棋棋盘和障碍标志 ...
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- 骑士游历/knight tour - visual basic 解决
在visual baisc 6 how to program 中文版第七章的练习题上看到了这个问题,骑士游历的问题. 在8x8的国际象棋的棋盘上,骑士(走法:一个方向走两格,另一个方向一格)不重复走完 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- BZOJ 1040 【ZJOI2008】 骑士
Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...
- 【BZOJ-1040】骑士 树形DP + 环套树 + DFS
1040: [ZJOI2008]骑士 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3312 Solved: 1269[Submit][Status ...
- BZOJ1040 [ZJOI2008]骑士
Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各 界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战 ...
- BFS 骑士的移动
骑士的移动 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83498#problem/E 题目: Description A f ...
- LA 3523 圆桌骑士
题目链接:http://vjudge.net/contest/141787#problem/A http://poj.org/problem?id=2942 此题很经典 知识点:DFS染色,点-双连通 ...
随机推荐
- obj.style 和currentstyle 等区别
版权声明:本文为博主原创文章,未经博主允许不得转载. 获取样式 obj.style 和currentstyle 等区别 obj.style只能获得内嵌样式(inline Style)就是写 ...
- CCF|火车购票|Java|80分
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Sc ...
- android studio 生成 jniLibs 目录
现在一般的项目都会加入第三方jar包,第三方jar包我们会新建一个文件夹:libs,然后jar包都放在这个文件夹中. 但我们会发现,只是新建一个文件加之后,在AndroidStudio的左侧并不会出现 ...
- vijos 1772 巧妙填数
描述 将1,2,\cdots,91,2,⋯,9共99个数分成三组,分别组成三个三位数,且使这三个三位数构成1:2:31:2:3的比例. 试求出所有满足条件的三个三位数.例如:三个三位数192,384, ...
- Java异常归纳
1.使用Tomcat运行“播报哥架构”出现的两大异常 1.1 监听器异常 详细情况:部署好Maven项目,启动TOMCAT提示如下错误 java.lang.ClassNotFoundExcepti ...
- axios 里面 then 默认写的function里面没有this,改成箭头函数后就可以用this了
,methods:{ loadJson:function(){ //this.jsonTest = "jjj" this.$http.get('http://localhost:3 ...
- 查看python关键字
打开命令窗口 输入python-——help()——keywords
- 转行做web前端,该如何进行短期快速自学,达到高新就业水平
就目前来说,毕业生如果想毕业就找到高薪的工作,互联网成为了第一个选择,在所有的职业中,不靠任何关系,全凭自己的能力就业,就是程序开发,而web前端开发是目最很热门的行业,在未来五年之内,web前端开发 ...
- Microsoft Windows Server
Microsoft Windows Server Microsoft Windows Microsoft Windows 是微软推出的个人版操作系统: Microsoft Windows Server ...
- U盘制作安装盘后容量不能恢复的解决方案
diskpartlist diskselect disk 0/1 --看具体U盘是0还是1clean