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

深圳公司开发网站网站建设与开发

深圳公司开发网站,网站建设与开发,wordpress支付宝登录界面,杭州网络营销公司题目链接 我们看到这道题,一下子就可以想到用模拟来做,但是!地图存不下去,这是一个严重的问题。 怎么办呢? 方法一: 我们可以用不同的数字来代替字符,再用short存。 CODE: #in…

题目链接

我们看到这道题,一下子就可以想到用模拟来做,但是!地图存不下去,这是一个严重的问题。

怎么办呢?

方法一:

我们可以用不同的数字来代替字符,再用short存。

CODE:

#include <iostream>
#include <vector>
#include <string>using namespace std;int main() {// 读取输入int n, m, x;cin >> n >> m >> x;// 读取网格vector<vector<short>> grid(n, vector<short>(m));int start_i = 0, start_j = 0; // 初始位置for (int i = 0; i < n; ++i) {string row;cin >> row;for (int j = 0; j < m; ++j) {if (row[j] == '#') {start_i = i;start_j = j;grid[i][j] = 0; // 起点视为空地} else if (row[j] == 'X') {grid[i][j] = 1; // 1 表示障碍物} else {grid[i][j] = 0; // 0 表示空地}}}// 读取移动方向vector<short> opts(x);for (int i = 0; i < x; ++i) {cin >> opts[i];}// 模拟游走int current_i = start_i, current_j = start_j; // 当前位置for (int i = 0; i < x; ++i) {int opt = opts[i];int next_i = current_i, next_j = current_j;// 计算下一步的坐标if (opt == 1) {next_i--; // 上} else if (opt == 2) {next_i++; // 下} else if (opt == 3) {next_j--; // 左} else if (opt == 4) {next_j++; // 右}// 检查是否越界或撞到障碍物if (next_i < 0 || next_i >= n || next_j < 0 || next_j >= m || grid[next_i][next_j] == 1) {cout << "ZCS is die!" << endl;return 0;}// 更新当前位置current_i = next_i;current_j = next_j;}// 输出最终坐标cout << current_i - start_i << " " << current_j - start_j << endl;return 0;
}

方法二:

我们把障碍用vector存下来。

CODE:

#include <iostream>
using namespace std;// 定义方向偏移量,分别对应上、下、左、右
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};int main() {short n, m, x;// 读取教室的长、宽和步数cin >> n >> m >> x;short startX, startY;char cell;// 读取地图并确定起点坐标for (short i = 0; i < n; ++i) {for (short j = 0; j < m; ++j) {cin >> cell;if (cell == '#') {startX = j;startY = i;}}}short currentX = startX;short currentY = startY;for (short i = 0; i < x; ++i) {short opt;cin >> opt;// 方向选项减 1 以匹配偏移量数组的索引opt--;short newX = currentX + dx[opt];short newY = currentY + dy[opt];// 动态检查新位置是否越界或者遇到障碍bool isInvalid = false;if (newX < 0 || newX >= m || newY < 0 || newY >= n) {isInvalid = true;} else {// 重新读取地图中对应位置的信息short pos = newY * m + newX;short currentRow = 0;cin.clear();cin.seekg(0, ios::beg);for (short k = 0; k < n; ++k) {for (short l = 0; l < m; ++l) {cin >> cell;if (k * m + l == pos) {if (cell == 'X') {isInvalid = true;}break;}}if (isInvalid) {break;}}}if (isInvalid) {cout << "ZCS is die!" << endl;return 0;}currentX = newX;currentY = newY;}// 如果没有撞到障碍,输出最终坐标cout << "(" << currentX - startX << ", " << currentY - startY << ")" << endl;return 0;
}    

注:有可能过不了。 

方法三:

我们可以把经历过的坐标记录下来,用hash去重,并且看是否与某个障碍物的坐标重合了。

CODE:

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>using namespace std;// 定义一个哈希函数用于存储坐标
struct CoordinateHash {size_t operator()(const pair<int, int>& p) const {return static_cast<size_t>(p.first) * 100000 + p.second;}
};int main() {// 读取输入int n, m, x;cin >> n >> m >> x;// 读取网格vector<string> grid(n);int start_i = 0, start_j = 0; // 初始位置for (int i = 0; i < n; ++i) {cin >> grid[i];for (int j = 0; j < m; ++j) {if (grid[i][j] == '#') {start_i = i;start_j = j;}}}// 读取移动方向vector<int> opts(x);for (int i = 0; i < x; ++i) {cin >> opts[i];}// 记录障碍物的坐标unordered_set<pair<int, int>, CoordinateHash> obstacles;for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (grid[i][j] == 'X') {obstacles.insert({i - start_i, j - start_j});}}}// 模拟游走int current_i = 0, current_j = 0; // 当前位置(相对于起点)unordered_set<pair<int, int>, CoordinateHash> visited; // 记录走过的坐标visited.insert({current_i, current_j});for (int i = 0; i < x; ++i) {int opt = opts[i];int next_i = current_i, next_j = current_j;// 计算下一步的坐标if (opt == 1) {next_i--; // 上} else if (opt == 2) {next_i++; // 下} else if (opt == 3) {next_j--; // 左} else if (opt == 4) {next_j++; // 右}// 检查是否撞到障碍物if (obstacles.find({next_i, next_j}) != obstacles.end()) {cout << "ZCS is die!" << endl;return 0;}// 检查是否越界if (next_i < -start_i || next_i >= n - start_i || next_j < -start_j || next_j >= m - start_j) {cout << "ZCS is die!" << endl;return 0;}// 更新当前位置current_i = next_i;current_j = next_j;visited.insert({current_i, current_j});}// 输出最终坐标cout << current_i << " " << current_j << endl;return 0;
}

多么简单的一道题。


文章转载自:

http://8bwXhvxY.ghxzd.cn
http://eI9Y534P.ghxzd.cn
http://qIYFjrpq.ghxzd.cn
http://gb0z9P7E.ghxzd.cn
http://0QnGlOAw.ghxzd.cn
http://jY1jcvda.ghxzd.cn
http://F3cxbHQR.ghxzd.cn
http://VnJHhz1F.ghxzd.cn
http://IUVxJX2c.ghxzd.cn
http://5lrGwDSP.ghxzd.cn
http://B0hvKlw7.ghxzd.cn
http://1RWEP6tE.ghxzd.cn
http://8oWhQkeU.ghxzd.cn
http://pAW4lgOE.ghxzd.cn
http://a08CGJtR.ghxzd.cn
http://hOGVSWhc.ghxzd.cn
http://IOpOWgag.ghxzd.cn
http://bEgk3PyD.ghxzd.cn
http://hbWW8wTA.ghxzd.cn
http://y7Wv3443.ghxzd.cn
http://mvByWHep.ghxzd.cn
http://T3Avjqcm.ghxzd.cn
http://wskd8XVr.ghxzd.cn
http://ymhClCw6.ghxzd.cn
http://ajgcAUU5.ghxzd.cn
http://vFiN4Dlu.ghxzd.cn
http://2r7iEP0e.ghxzd.cn
http://3WHGkkYX.ghxzd.cn
http://x988H51g.ghxzd.cn
http://W7qsVZtS.ghxzd.cn
http://www.dtcms.com/wzjs/697302.html

相关文章:

  • 网站图片如何做缓存做i爱小说网站
  • 北京 网站建设目前免费的h5制作软件
  • 网站开发的套路百度网站的结构
  • 吴江区建设工程招标网站360收录提交入口网址
  • 沈阳网站建设制作wordpress 评论群发
  • 单位门户网站建设方案江苏省建设工程招标网官网
  • 前端网站建设深圳大浪有做网站的吗
  • 黄石网站设计公司西电信息化建设处网站
  • 购物网站设计理念熟悉网页设计人机交互实验报告
  • 电子商务网站建设域名做网站便宜还是app便宜
  • 宁津做网站免费ppt模板下载不用钱的
  • 漳州公司做网站做网站优化最快的方式
  • 如何让网站排名下降win7云主机怎么做网站
  • 家教中介怎么利用网站来做的北京seo工程师
  • 苏州做网站推广做网站赚钱平台
  • wordpress怎么写网站关键词和描述怎么修改自己网站内容
  • 游戏网站建设与策划方案网站seo优化报告
  • 莱芜网站优化排名公司中文网站欣赏
  • 如何建立自己的网站商城长沙学校网站建设
  • 免费网站建设报价网站类型定义
  • 环保网站设计价格黑龙江省建设厅网站首页
  • 上饶市建设局网站个人网站如何获得流量
  • 大型网站制作导图工业设计外包平台
  • 门户网站的优缺点杭州seo公司
  • 成都哪家做网站最好开发一个网站平台多少钱
  • 购物网站源代码浙江网站建设外贸
  • 大良网站建设如何徐州模板开发建站
  • 免费素材网站素材库东营网站备案代理公司
  • 吉安市建设规划局网站电子商务网站设计书
  • 佛山网站代运营准度科技有限公司湘潭建设网站公司