当前位置: 首页 > news >正文

陈铭生小说热门seo推广排名稳定

陈铭生小说,热门seo推广排名稳定,沧州网站建设联系电话,网站地图是什么样子的文章目录 问题描述思路代码C 问题描述 描述 定义一个二维数组 N*M ,如 5 5 数组下所示: int maze[5][5] { 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁&#xff0…

文章目录

    • 问题描述
    • 思路
    • 代码C++

问题描述

描述

定义一个二维数组 N*M ,如 5 × 5 数组下所示:

int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的路线。入口点为[0,0],既第一格是可以走的路。

数据范围: 2≤n,m≤10 2≤n,m≤10 , 输入的内容只包含 0≤val≤1 0≤val≤1
输入描述:

输入两个整数,分别表示二维数组的行数,列数。再输入相应的数组,其中的1表示墙壁,0表示可以走的路。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。
输出描述:

左上角到右下角的最短路径,格式如样例所示。
示例1
输入:

5 5
0 1 0 0 0
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

输出:

(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)
(4,4)

思路

  • 前进策略: 一直靠着左侧的墙壁走
  • 假设初始方向为col的方向
  • 如果能向左走,直接向左转弯,并向前走一步;
  • 否则依次尝试:向前走、向右走。注意:
    • 向前走时不需要修改当前的前进方向
    • 尝试向右走时,无论是否能够向右走出一步,都直接向右转方向,这样连续转几次,就能转出死角
  • 当 当前位置 等于 出口位置时结束
  • 通过删除重复不必要的足迹使路径最短——已知了只有一条通路

代码C++

在这里插入图片描述

#include <iostream>
#include <string>
#include <vector>using namespace std;struct Pos {int col;int row;bool operator==(const Pos& another) const {return another.col == col && another.row == row;}
};class Agent {
public:int N;  // 总行数int M;  // 总列数vector<vector<int>> data;  // 迷宫矩阵int direction;vector<Pos> path;Pos cur_pos;Pos final_pos;public:void get_input() {cin >> N >> M;data = vector<vector<int>>(N, vector<int>(M));for (int i=0; i<N; ++i) {for (int j=0; j<M; ++j) {cin >> data[i][j];}}final_pos.row = N - 1;final_pos.col = M - 1;cur_pos.row = 0;cur_pos.col = 0;path.push_back(cur_pos);direction = 1;}void print_path() {for (auto& pos : path) {printf("(%d,%d)\n", pos.row, pos.col);}}bool valid_pos(const Pos& pos) {if (pos.col >= 0 && pos.col < M &&pos.row >= 0 && pos.row < N &&data[pos.row][pos.col] == 0) {return true;} else {return false;}}bool move_forward() {Pos next_pos = cur_pos;if (direction == 1) {next_pos.col += 1;} else if (direction == 2) {next_pos.row += 1;} else if (direction == 3) {next_pos.col -= 1;} else {next_pos.row -= 1;}// printf("forwd: %d,%d\n", next_pos.row, next_pos.col);if (valid_pos(next_pos)) {cur_pos = next_pos;path.push_back(next_pos);return true;} else {return false;}}bool move_right() {Pos next_pos = cur_pos;if (direction == 1) {next_pos.row += 1;} else if (direction == 2) {next_pos.col -= 1;} else if (direction == 3) {next_pos.row -= 1;} else {next_pos.col += 1;}// printf("right: %d,%d\n", next_pos.row, next_pos.col);direction = (direction + 1) <= 4 ? (direction + 1) : 1;  // 更新方向if (valid_pos(next_pos)) {cur_pos = next_pos;path.push_back(next_pos);return true;} else {return false;}}bool move_left() {Pos next_pos = cur_pos;if (direction == 1) {next_pos.row -= 1;} else if (direction == 2) {next_pos.col += 1;} else if (direction == 3) {next_pos.row += 1;} else {next_pos.col -= 1;}// printf("left : %d,%d\n", next_pos.row, next_pos.col);if (valid_pos(next_pos)) {cur_pos = next_pos;direction = (direction - 1) >= 1 ? (direction - 1) : 4;  // 更新方向path.push_back(next_pos);return true;} else {return false;}}// 前进策略: 一直靠着左侧的墙壁走void find_way() {while (true) {if (cur_pos == final_pos) {break;}// 根据 当前 direction 判断前后左右if (move_left()) {       /*左侧是墙壁*/trim_path();// printf("向左走\n");} else {if (move_forward()) {   /*前面能走*/trim_path();// printf("向前走\n");} else {/*向右能走,注意无论是否能向右走,都直接向右转向了,所以一定能转出去*/if (move_right()) {trim_path();// printf("向右走\n");}}}}}// 移除不必要的足迹: A-B-D-E-B-C-X-Y-Z,   B-D-E就是不必要的足迹,  不能有重复的坐标点void trim_path() {Pos end = path.back();for (auto iter = path.begin(); iter != path.end()-1; iter++) {if (*iter == end) {for (auto iter2 = iter; iter2 != path.end()-1; iter2) {path.erase(iter2);}break;}}}
};int main()
{Agent agent;agent.get_input();agent.find_way();agent.print_path();return 0;
}
http://www.ds6.com.cn/news/83136.html

相关文章:

  • 推广联盟网站怎么做百度热门关键词排名
  • 做网站大概多钱搜索引擎优化的名词解释
  • 福州什么推广网站好外贸网络营销推广
  • 上海模板建站平台sem和seo是什么职业岗位
  • 东莞网站设计师武汉seo服务外包
  • 企业网站优化的弊端百度快照怎么优化排名
  • 有没有帮忙做推广的网站手机系统优化软件哪个好
  • 做澳洲外贸的网站微信营销的特点
  • 上市设计公司网站竞价托管收费标准
  • 最好的网站开发会计培训
  • 北京一个公司做网站认证宁波seo网站排名
  • 做微信扫码网站百中搜
  • 国内电商网站有哪些竞价恶意点击犯法吗
  • Wordpress必备chajseo优化包括什么
  • 衡水提供网站制作公司哪家好湖南seo技术培训
  • 单产品网站模板苏州百度推广服务中心
  • 如何拿qq空间做网站能够免费换友链的平台
  • 网站是谁做的seo难不难学
  • php大型网站设计黑帽seo联系方式
  • 宿迁做网站 宿迁网站建设河北seo诊断培训
  • 美女做暖暖视频免费网站公司网页制作
  • 做网站项目时 需求分析的内容网站结构
  • 天津手机网站建设短视频营销推广方案
  • 访问数据库的网站开发语言抖音seo代理
  • 网站例子大全如何制作网站教程
  • 正定网站制作网站推广一般多少钱
  • 制作网站详细步骤怎么优化网站排名
  • 浙江做网站公司有哪些电商广告
  • 网站 app开发 财务做帐域名停靠浏览器
  • 遵义市政府网站建设概况百度app广告