数格子,,简单bfs

来源:互联网 发布:windows tftp 服务器 编辑:程序博客网 时间:2024/05/07 13:13

第一篇博客要写一下bfs,数格子=_=

题目:

#include <iostream>#include <queue>#include <memory.h>using namespace std;int a[100];int n;struct node{    int w;    int t;};int bfs(){    queue<node> Q;    node s;    s.w = s.t = 0;    Q.push(s);    while(!Q.empty())    {        node now = Q.front();        Q.pop();        int i;        for(i = 1; i <= a[now.w]; i++)        {            node newl;            newl.w = now.w + i;            newl.t = now.t + 1;            Q.push(newl);            if(newl.w == n-1)                return newl.t;        }    }    return -1;}int main(){    int t;    cin>>t;    while(t--)    {        int i, ans;        cin>>n;        for(i = 0; i < n; i++)            cin>>a[i];        ans = bfs();        cout<<ans<<endl;    }    return 0;}

具体待解决的问题是循环体中始量的设定和终止,

以及就具体题而言的进队入队=_+.

0 0
原创粉丝点击