【深度搜索】NYOJ58最小步数

来源:互联网 发布:手机扫描软件app 编辑:程序博客网 时间:2024/04/28 22:46

题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=58

这道题也还是一道很简单的深度搜索的问题,利用队列+深搜很容易写出来。

#include <iostream>#include <queue>using namespace std;struct node{int x,y,step;};int dir[4][2]={-1,0,0,1,1,0,0,-1};int v[9][9];int bfs(node s,node t,int map[9][9]){queue<node> Q;int i;node temp;Q.push(s);while(1){if(s.x==t.x && s.y==t.y)return s.step;for(i=0;i<4;i++){temp.x=s.x+dir[i][0];temp.y=s.y+dir[i][1];if(map[temp.x][temp.y]==0){temp.step=s.step+1;map[temp.x][temp.y]=1;Q.push(temp);}}s=Q.front();Q.pop();}}int main(){int T;node start,end;cin>>T;while(T--){int map[9][9]={  1,1,1,1,1,1,1,1,1,  1,0,0,1,0,0,1,0,1,  1,0,0,1,1,0,0,0,1,  1,0,1,0,1,1,0,1,1,  1,0,0,0,0,1,0,0,1,  1,1,0,1,0,1,0,0,1,  1,1,0,1,0,1,0,0,1,  1,1,0,1,0,0,0,0,1,  1,1,1,1,1,1,1,1,1  };//题目中的数据 cin>>start.x>>start.y>>end.x>>end.y;start.step=0;map[start.x][start.y]=1;cout<<bfs(start,end,map)<<endl;}return 0;}


0 0
原创粉丝点击