初识BFS POJ-3278 Catch That Cow FZU-2285 迷宫寻宝

  • 令人窒息的创新实验课让我们写程序。

  • POJ-3278 Catch That Cow

  • Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
    * Teleporting: FJ can move from any point X to the point 2 × X in a single minute.If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
    
  • Input

    Line 1: Two space-separated integers: N and K
    
  • Output

    Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
    
  • Sample Output

    5 17
    
  • Hint

    The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
    
  • 我的题解

    #include <iostream>
    #include <queue>
    using namespace std;
    #define maxn 100001int step[maxn];   //代表到达每个位置需要的步数
    bool visit[maxn]; //代表某个位置是否被访问
    queue<int> q;
    int BFS(int N, int K)
    {int head, next;step[N] = 0;visit[N] = true;q.push(N);while (!q.empty()){head = q.front();q.pop();for (int i = 0; i < 3; i++){if (i == 0) //代表往左1next = head - 1;if (i == 1) //代表往右1next = head + 1;if (i == 2) //代表乘2next = head * 2;if (next < 0 || next > maxn) //排除越界的情况continue;if (!visit[next]){q.push(next); //加入队列中,下次作为head弹出,准备下一轮搜索visit[next] = true;step[next] = step[head] + 1; //head表示前一个点,next表示后一个点,两者的步数当然差1}if (next == K)return step[next];}}return 0; //代表找不到(大概,应该都会找到吧
    }
    int main()
    {int N, K; //N是John初始的位置,K是牛的位置cin >> N >> K;if (N - K >= 0)cout << N - K << endl;elsecout << BFS(N, K) << endl;return 0;
    }
    
  • 然后是第二题 FZU-2285

    洪尼玛今天准备去寻宝,在一个n*n (n行, n列)的迷宫中,存在着一个入口、一些墙壁以及一个宝藏。由于迷宫是四连通的,即在迷宫中的一个位置,只能走到与它直接相邻的其他四个位置(上、下、左、右)。现洪尼玛在迷宫的入口处,问他最少需要走几步才能拿到宝藏?若永远无法拿到宝藏,则输出-1。
    
  • Input

    多组测试数据。每组数据输入第一行为正整数n,表示迷宫大小。接下来n行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'#'表示该位置为墙壁,字符'S'表示该位置为入口,字符'E'表示该位置为宝藏,输入数据中只有这四种字符,并且'S'和'E'仅出现一次。n≤1000
    
  • Output

    输出拿到宝藏最少需要走的步数,若永远无法拿到宝藏,则输出-1。
    
  • Sample Input

    5
    S.#..
    #.#.#
    #.#.#
    #...E
    #....
    
  • Sample Output

    7
    
  • 我的题解

    #include <iostream>
    #include <queue>
    #include <cstring>
    using namespace std;
    #define maxn 1000bool visit[maxn][maxn];
    int step[maxn][maxn];typedef struct
    {int i; //代表节点的横坐标int j; //纵坐标
    } node;queue<node> q; //定义一个节点为node的队列int BFS(char **maze, int n, node start, node end)
    {visit[start.i][start.j] = true;step[start.i][start.j] = 0;q.push(start);   //关于起点的初始化node head, next; //在过程中分别指代现在的位置和下一个位置while (!q.empty()){head = q.front();q.pop();for (int i = 0; i < 4; i++){if (i == 0) //代表往左next.i = head.i - 1, next.j = head.j;if (i == 1) //代表往右next.i = head.i + 1, next.j = head.j;if (i == 2) //代表往上next.i = head.i, next.j = head.j - 1;if (i == 3) //代表往下next.i = head.i, next.j = head.j + 1;if (next.i < 0 || next.i >= n || next.j < 0 || next.j >= n || maze[next.i][next.j] == '#') //越界或者碰到墙壁的情况排除continue;if (!visit[next.i][next.j]){visit[next.i][next.j] = true;step[next.i][next.j] = step[head.i][head.j] + 1; //next的步数比head的步数+1q.push(next);}if (maze[next.i][next.j] == 'E')return step[next.i][next.j];}}return -1;
    }
    int main()
    {int n; //表示迷宫的大小while (cin >> n){memset(visit, false, sizeof(visit));memset(step, 0, sizeof(step));while (!q.empty())q.pop();char **maze = new char *[n]; //动态创建二维数组mazefor (int i = 0; i < n; i++)maze[i] = new char[n];node start, end; //分别代表起点和终点,用来储存他们的坐标for (int i = 0; i < n; i++)scanf("%s", maze[i]);for (int i = 0; i < n; i++)for (int j = 0; j < n; j++){if (maze[i][j] == 'S')start.i = i, start.j = j;if (maze[i][j] == 'E')end.i = i, end.j = j;}cout << BFS(maze, n, start, end) << endl;}return 0;
    }
    
  • 水博客,逃(

初识BFS POJ-3278 Catch That Cow FZU-2285 迷宫寻宝相关推荐

  1. BFS POJ 3278 Catch That Cow

    题目传送门 1 /* 2 BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 3 */ 4 #include <cstdio> 5 #include <iostrea ...

  2. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 30924   Accepted: 9536 D ...

  3. POJ 3278 Catch That Cow BFS

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32071   Accepted: 9866 D ...

  4. POJ 3278 Catch That Cow(BFS)

    题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  5. bfs+dfs分析----poj 3278 Catch That Cow

    题目详情 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 115430   Accepted:  ...

  6. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  7. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35043   Accepted: 10800 ...

  8. poj 3278 Catch That Cow(广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45087   Accepted: 14116 ...

  9. poj 3278 Catch That Cow 广搜

    hdu 2717 Catch That Cow,题目链接 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...

  10. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 1 #include< ...

最新文章

  1. TiM:rDNA拷贝数的种内变化影响微生物群落分析吗?
  2. 美团点评业务之技术解密,日均请求数十亿次的容器平台
  3. 2021-04-17 深入理解SLAM技术 【3】 数学基础概述
  4. ABAP system landscape和vue项目webpack构建的一种实践
  5. MySQL For RedHat Linux(源码安装,附安装包)
  6. 【渝粤教育】 国家开放大学2020年春季 1050金融理论前沿课题 参考试题
  7. 解析得了数学,写得了诗书,这是个有趣的灵魂
  8. 有哪些类目适合刚创业的新手淘宝卖家做?
  9. centos下 安装jdk
  10. ListView中动态显示和隐藏HeaderFooter
  11. 自定义self.editButtonItem 改变自定义self.editButtonItem的背景图片
  12. Is necessary to close fille in python?
  13. Python2中使用input出现的NameError: name ‘***‘ is not defined问题原因及解决办法
  14. html绘制地铁线路图,地铁线路图怎么画?几个步骤助你轻松作出
  15. 合肥ibm服务器维修,合肥IBM/thinkpad笔记本维修
  16. 使用 DISM 或系统更新准备工具修复 Windows 更新错误
  17. 《电影院的爆米花为什么卖的贵》读书笔记之1——意外后果定律
  18. Java学习--网络,图形界面和数据库
  19. vim etc mysql my.cnf_初始化配置文件的使用:/etc/my.cnf
  20. 智慧公安雪亮工程大数据平台系统

热门文章

  1. 秋招面经第二弹:百度一面-大数据开发工程师
  2. Unsupervised Person Re-identification: Clustering and Fine-tuning
  3. 安装office2007安装程序找不到office.zh-cn\*三种详细解决方法
  4. tensorflow.python.framework.errors_impl.InvalidArgumentError exception str() failed解决办法
  5. 光纤分类——多模和单模
  6. S32K系列之ADC
  7. JAVA图形面积与周长(抽象类)
  8. 幻灯片制作“高手”的自我总结
  9. JLH统计法统计飞龙
  10. B. Alyona and a Narrow Fridge 【 思维题 】