worm

为什么我的眼里饱含泪水?因为我程序没写完!
随笔 - 5, 文章 - 2, 评论 - 10, 引用 - 0
数据加载中……

第一道广度搜索BFS纪念 poj 3278 源代码

   参考了别人的思路,做出了第一道BFS,虽然在大牛们看来不屑一顾,but about me,I really happy for it, I'm coming ! worm never give up!!
 1//============================================================================
 2// Name        : poj.cpp
 3// Author      :
 4// Version     :
 5// Copyright   : Your copyright notice
 6// Description : Hello World in C++, Ansi-style
 7//============================================================================
 8
 9#include <iostream>
10#include <queue>
11using namespace std;
12queue<int> q;
13int    result[100001];
14int visited[100001= {0};
15int BFS(int start,int end) {
16    if (start == end)
17        return 0;
18    q.push(start);
19    result[start] = 0;
20    visited[start] = 1;
21    while(!q.empty()) {
22        int temp = q.front();
23        q.pop();
24        int next;
25        for (int i = 0; i < 3++i) {
26            if (i == 0)
27                next = temp - 1;
28            if (i == 1)
29                next = temp + 1;
30            if (i == 2)
31                next = temp*2;
32            if(next > 100000 || next < 0{
33                continue;
34            }

35            if (visited[next] != 1{
36                q.push(next);
37                result[next] = result[temp] + 1;
38                visited[next] = 1;
39            }

40            if (next == end)
41                return result[next];
42        }

43    }

44    return 0;
45}

46int main() {
47    int n,k;
48    cin >> n >> k;
49    cout << BFS(n,k) << endl;
50    return 0;
51}

52

posted on 2009-03-07 18:31 WORM 阅读(1273) 评论(3)  编辑 收藏 引用

评论

# re: 第一道广度搜索BFS纪念 poj 3278 源代码  回复  更多评论   

25 for (int i = 0; i < 3; ++i) {
26 if (i == 0)
27 next = temp - 1;
28 if (i == 1)
29 next = temp + 1;
30 if (i == 2)
31 next = temp*2;
32 if(next > 100000 || next < 0) {
33 continue;
34 }
------------------------------------------------
广搜,这里我没看懂,可不可以解释下....好吧,我菜...
2009-06-16 00:26 | hj

# re: 第一道广度搜索BFS纪念 poj 3278 源代码  回复  更多评论   

膜拜下··
2010-08-19 09:28 | hm

# re: 第一道广度搜索BFS纪念 poj 3278 源代码  回复  更多评论   

你那段英语翻译过来:

但是关于我,我真的开心对它,我高潮了!蠕虫永远不放弃!
2011-08-09 17:08 | english teacher

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理