滴滴2017笔试bfs

来源:互联网 发布:查分数的软件 编辑:程序博客网 时间:2024/04/27 09:26
[编程题] 地下迷宫

时间限制:1秒

空间限制:32768K

小青蛙有一天不小心落入了一个地下迷宫,小青蛙希望用自己仅剩的体力值P跳出这个地下迷宫。为了让问题简单,假设这是一个n*m的格子迷宫,迷宫每个位置为0或者1,0代表这个位置有障碍物,小青蛙达到不了这个位置;1代表小青蛙可以达到的位置。小青蛙初始在(0,0)位置,地下迷宫的出口在(0,m-1)(保证这两个位置都是1,并且保证一定有起点到终点可达的路径),小青蛙在迷宫中水平移动一个单位距离需要消耗1点体力值,向上爬一个单位距离需要消耗3个单位的体力值,向下移动不消耗体力值,当小青蛙的体力值等于0的时候还没有到达出口,小青蛙将无法逃离迷宫。现在需要你帮助小青蛙计算出能否用仅剩的体力值跳出迷宫(即达到(0,m-1)位置)。 
输入描述:
输入包括n+1行:第一行为三个整数n,m(3 <= m,n <= 10),P(1 <= P <= 100)接下来的n行:每行m个0或者1,以空格分隔


输出描述:
如果能逃离迷宫,则输出一行体力消耗最小的路径,输出格式见样例所示;如果不能逃离迷宫,则输出"Can not escape!"。测试数据保证答案唯一

输入例子1:
4 4 101 0 0 11 1 0 10 1 1 10 0 1 1

输出例子1:
[0,0],[1,0],[1,1],[2,1],[2,2],[2,3],[1,3],[0,3]
一看就是dfs,或者是bfs的题目,但真没有自己独立完整写过一次dfs或者bfs的代码,今天终于花了一天时间,自己手动写出来,中间真是遇到太多坑了,虽然代码写的不简洁,但是下次遇到应该写起来不陌生了。
#include <iostream>using namespace std;#include <queue>#include <vector>#include <stack>struct point{int point_x;int point_y;//int pre_x;//int pre_y;int psum=0;point(int x, int y) :point_x(x), point_y(y){}};int main(){int n, m, P;while (cin >> n >> m >> P){vector<vector<int>> mymap(n, vector<int>(m, 0));vector<vector<int>> mymapflag(n, vector<int>(m, 0));/*int data[10][10] = { { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },{ 1, 1, 1, 1, 0, 0, 0, 0, 0, 1 },{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },{ 1, 0, 0, 1, 1, 1, 1, 1, 1, 1 },{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },{ 1, 0, 0, 1, 1, 1, 1, 1, 1, 1 },{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },{ 1, 0, 0, 1, 1, 1, 1, 1, 1, 1 } };*/for (int i = 0; i < n; i++){vector<int> temp_v;int temp;for (int j = 0; j < m; j++){cin >> temp;mymap[i][j] = temp;//mymap[i][j] = data[i][j];}}point pstart = point(0, 0);queue<point> que;vector<point> path;vector<point> path_best;que.push(pstart);path.push_back(pstart);mymapflag[0][0] = 1;//int p_sum = 0;int p_sum_max = 999999;int step_x[4] = { 0, -1, 0, 1 };int step_y[4] = { 1, 0, -1, 0 };//右,上,左,下int sum_step[4] = { 1, 3, 1, 0 };//map<point, point> map_pre;记录前驱可以用数组不要用map;//point  pre_point[100][100];//vector<vector<point>> pre_point;vector<vector<point>> pre_point(n, vector<point>(m, point(99, 99)));//记录前驱。while (!que.empty()){point point_cur = que.front(); que.pop();//这是广度优先bfs.适合找第一条,但是要是找所有的?深度优先dfs?if (point_cur.point_x == 0 && point_cur.point_y == m - 1 && point_cur.psum<= P){if (point_cur.psum <= p_sum_max){//path_best = path;//for (int i = 0; i < path_best.size(); i++)//{//cout << path_best[i].point_x << " " << path_best[i].point_y << endl;//}stack<point>ans;while (!(point_cur.point_x == 0 && point_cur.point_y == 0)){//cout << point_cur.point_x << " " << point_cur.point_y << endl;ans.push(point_cur);point_cur = pre_point[point_cur.point_x][point_cur.point_y];}ans.push(point(0, 0));cout << "[0,0]";ans.pop();while (!ans.empty()){point temp = ans.top(); ans.pop();cout << "," << "[" << temp.point_x << "," << temp.point_y << "]";}return 0;}}cout << "current:"<<point_cur.point_x << " " << point_cur.point_y << endl;for (int i = 0; i < 4; i++){int new_x = point_cur.point_x + step_x[i];int new_y = point_cur.point_y + step_y[i];int new_psum = point_cur.psum + sum_step[i];//p_sum += sum_step[i];//if (new_x < 0 || new_x >= n || new_y<0 || new_y >= m || mymap[new_x][new_y] == 0 || mymapflag[new_x][new_y] || new_psum>P){    //cout << mymap[new_x][new_y] << endl;//p_sum -= sum_step[i];continue;}else{cout << new_x << " " << new_y << endl;point new_point = point(new_x, new_y);new_point.psum = new_psum;pre_point[new_x][new_y] = point_cur;que.push(new_point);mymapflag[new_x][new_y] = 1;cout << "先加入的点的psum " << new_psum << endl;path.push_back(new_point);}}}cout << "Can not escape!" << endl;}         return 0;}