BFS B题
来源:互联网 发布:seo app 编辑:程序博客网 时间:2024/05/22 02:19
- #include<iostream>using namespace std;int a[5][5];struct Node{ int x,y,pre;} queue[50];int front=0;int rear=0;int view[5][5];int dis[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};void bfs(int beginX,int beginY,int endX,int endY){ queue[0].x=beginX,queue[0].y=beginY,queue[0].pre=-1; rear=rear+1; view[beginX][beginY]=1; while(front<rear) { for(int i=0; i<4; i++) { int newx=queue[front].x+dis[i][0]; int newy=queue[front].y+dis[i][1]; if(newx<0||newx>5||newy<0||newy>5||a[newx][newy]==1||view[newx][newy]==1) continue; queue[rear].x=newx; queue[rear].y=newy; queue[rear].pre=front; rear++; view[newx][newy]=1; if(newx==endX&&newy==endY) { return; } } front++; }}void print(Node now){ if(now.pre==-1) cout<<"("<<now.x<<", "<<now.y<<")"<<endl; else { print(queue[now.pre]); cout<<"("<<now.x<<", "<<now.y<<")"<<endl; }}int main(){ for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { cin>>a[i][j]; } } bfs(0,0,4,4); print(queue[rear-1]); return 0;}
阅读全文
0 0
- BFS B题
- FJNU校赛B题(dfs + bfs)
- Problem B bfs
- B - Prime Path(BFS)
- bfs--Codeforces,520B.
- codeforce 689B 【bfs】
- codeforces 320B bfs
- Problem B (BFS)
- Codeforces 329B bfs
- 四川省ACM省赛B题,优先队列+BFS Charitable Exchange
- BFS 宽搜 B题 - Igor In the Museum
- SCU2016-04 B题 枚举+ bfs最短路
- kuangbin 1B简单bfs(模板题)
- CodeForces 329B(bfs)
- CodeForces 329B(BFS)
- 【bfs && 如何想到bfs】2014 Benelux Algorithm B
- HDU_4771_Stealing Harry Potter's Precious(BFS+DFS)(13杭州现场赛B题)
- HDU 4771 2013亚洲区域赛杭州站 B题 状态压缩+bfs
- struts2简单入门-登陆示例
- QT+MSVC2010+Win7轻量级32位开发环境搭建
- Java是什么
- 关于searchView的黑框去除问题
- Matlab中的类Classdef
- BFS B题
- PHP获取用户真实 IP , 淘宝IP接口获得ip地理位置
- xml与java对象的相互转化
- eclipse或者myeclipse的console信息太多,看不到之前的信息
- 8.8test
- UVA
- gitlab pipelines job执行时日志较大报错
- angular modalInstance模态框
- JS学习-字符串