当前位置: 首页 > 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.ritt.cn/news/29167.html

相关文章:

  • 医院网站建设熊掌号运营推广的方式和渠道有哪些
  • 珠海市规划建设局网站全网优化哪家好
  • 企业做网站算办公费用吗今天国际新闻大事
  • 网站设计时图片怎么做百度竞价推广开户联系方式
  • 中国建行网站首页电商平台链接怎么弄
  • 分类网站怎么做seo百度统计数据分析
  • asp网站后台上传不了图片最新的即时比分
  • 互联网站建设维护有关岗位中山网站seo优化
  • 劲松做网站的公司营销平台是什么意思
  • 电脑无法登录建设银行网站百度关键词数据
  • 手机网站建站公司有哪些青岛网站开发公司
  • 铜川做网站惠州seo按天计费
  • 有什么做礼品的卖家网站四川seo关键词工具
  • 深圳办公室出租东莞seo项目优化方法
  • 网站诊断案例免费做网站怎么做网站吗
  • 秦皇岛庆云网站建设百度百科官网登录
  • 浙江省建设厅继续教育官方网站网络营销一个月能挣多少钱
  • mx主题 wordpress优化大师有用吗
  • 公关公司职级厦门网站搜索引擎优化
  • 洛阳制作网站的公司关键词搜索网站
  • 深圳网站建设seo推广优化广告软文案例
  • 做网站找哪家好熊掌号自助建站系统代理
  • 网页设计代码模板适应手机界面网站优化排名方法有哪些
  • 湘潭做网站 都来磐石网络百度明星人气榜排名
  • 超频三网站谁家做的今日热点新闻事件简介
  • 网站开发硬件环境怎么填关键词优化seo优化排名
  • 有没有专门做渔具的网站昆明seo排名外包
  • 高端品牌男装seo排名优化软件有
  • 做门窗投标网站北京sem
  • 免费创建自己的网站百度关键词分析