POj1915
来源:互联网 发布:c语言可以编安卓软件吗 编辑:程序博客网 时间:2024/05/29 09:01
BFS裸题,用数组把8个方向存起来,然后直接BFS吧.(第一次写BFS,居然这么顺…)
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define MAXN 305using namespace std;struct node{ int x,y,step;};int n;int dis[8][2]={{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1}};queue<node> q;int vis[MAXN][MAXN],aim[2];int bfs(int x,int y){ int i; while(!q.empty()) { q.pop(); } node st; st.x=x; st.y=y; st.step=0; memset(vis,0,sizeof(vis)); q.push(st); vis[x][y]=1; while(!q.empty()) { node qs=q.front(); q.pop(); if(qs.x==aim[0]&&qs.y==aim[1]) { return qs.step; } node next; for(i=0;i<8;i++) { next.x=qs.x+dis[i][0]; next.y=qs.y+dis[i][1]; if(next.x==aim[0]&&next.y==aim[1]) { return qs.step+1; } if(next.x>=0&&next.x<n&&next.y>=0&&next.y<n&&vis[next.x][next.y]==0) { next.step=qs.step+1; q.push(next); vis[next.x][next.y]=1; } } } return -1;}int main(){ int T; cin>>T; while(T--) { cin>>n; int u,v; cin>>u>>v; cin>>aim[0]>>aim[1]; int ans=bfs(u,v); cout<<ans<<endl; } return 0;}
0 0
- POJ1915
- poj1915
- poj1915
- poj1915
- POj1915
- POJ1915
- poj1915
- HDU1372&&POJ1915
- poj1915 BFS
- poj1915 bfs
- POJ1915 BFS
- poj1915 & poj2243 Knight Moves
- poj1915 Knight Moves
- poj1915 - Knight Moves
- POJ1915 经典bfs
- (未)poj1915(bfs)
- poj2243+poj1915骑士问题
- POJ1915:Knight Moves(BFS)
- POJ2796---Feel Good(前缀和+单调栈维护)
- HDU-5211-Mutiple(BestCoder+以后做题还是用c++吧.......)
- TF-IDF模型的概率解释
- Android Bitmap内存限制问题
- Performance 之我的起步[一]
- POj1915
- JUnit测试案例
- 数据挖掘概论
- java底层分页使用
- 第八周项目 二 【项目2-Time类中的运算符重载】
- shell编程判断字符串是否相等
- SSH框架整合模板配置文件
- HDU_1285_确定比赛名次(拓扑排序)
- 容斥原理【模板】