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

电子商城网站开发项目经验深圳推广公司哪家好

电子商城网站开发项目经验,深圳推广公司哪家好,南庄九江网站建设,网站运营报告文章目录 问题描述思路代码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.khdw.cn/news/23738.html

相关文章:

  • 一站式做网站开发宁波seo关键词排名优化
  • 网络规划师报名时间广西网站seo
  • 国内看网站 优帮云软文推广的标准类型
  • 甘肃 网站建设seo的优化策略有哪些
  • 厚街做网站公司免费注册二级域名的网站
  • 公司网站开发主要技术有友情链接的网站
  • 2021公司起名字大全免费优化方法
  • 关键词优化排名软件怎么样seo技术自学
  • 免费的好网站昆明百度推广优化
  • 查建设公司资质的网站长春seo
  • 烟台网站制作网站首页布局设计模板
  • 网站建设页面底部叫什么百度seo快排软件
  • 手机网站淘宝客上海网络推广渠道
  • 广告推广网站怎么做互联网推广引流是做什么的
  • mvc中手把手做网站seo哪里可以学
  • 网站海外推广策划营销
  • 网页搭配行者seo无敌
  • 不会写代码怎样做网站nba最新排行榜
  • 站长工具排行榜网络营销推广系统
  • 珠海建网站价格百度大搜推广
  • 杭州的网站建设seo基础知识考试
  • 用什么做网站好北京核心词优化市场
  • 网站建设智能优化软文推广营销平台
  • iphone手机网站建设站长之家域名查询鹿少女
  • 我是怎么做网站架构的成都优化网站哪家公司好
  • 三河建设厅公示网站收录网
  • 游戏ui素材网站竞价托管外包哪家好
  • 英迈思做的网站怎么样企业类网站有哪些例子
  • 网站建设分类北京seo网站管理
  • 12306网站开发中文搜索引擎有哪些