1-28总结

文章介绍了一个关于机器人在网格中按指令搬运物品的最优化问题,涉及路径规划和时间效率计算,提供了C++代码示例。
摘要由CSDN通过智能技术生成

题目链接:机器人搬重物

题目描述

机器人移动学会(RMI)现在正尝试用机器人搬运物品。机器人的形状是一个直径 1.6 米的球。在试验阶段,机器人被用于在一个储藏室中搬运货物。储藏室是一个 N×M 的网格,有些格子为不可移动的障碍。机器人的中心总是在格点上,当然,机器人必须在最短的时间内把物品搬运到指定的地方。机器人接受的指令有:

  • 向前移动 1 步(Creep);
  • 向前移动 2 步(Walk);
  • 向前移动 3 步(Run);
  • 向左转(Left);
  • 向右转(Right)。

每个指令所需要的时间为 1秒。请你计算一下机器人完成任务所需的最少时间。

输入格式

第一行为两个正整数 N,M (1≤N,M≤50),下面 N 行是储藏室的构造,0 表示无障碍,1 表示有障碍,数字之间用一个空格隔开。接着一行有 4 个整数和 1 个大写字母,分别为起始点和目标点左上角网格的行与列,起始时的面对方向(东 E,南 S,西 W,北 N),数与数,数与字母之间均用一个空格隔开。终点的面向方向是任意的。

输出格式

一个整数,表示机器人完成任务所需的最少时间。如果无法到达,输出 −1。

输入输出样例

输入 #1复制

9 10
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
7 2 2 7 S

输出 #1复制

12

题解:这个题的话采用结构体队列去写好一些,首先要值得注意的是机器人的形状是1.6米所以格子的障碍的周围四个点都不能走,把图转换为点图好一些还有就是方向的转变我才用的是用一个三维数组来标记点位以及方向;

AC代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;

using ll = long long;
#define up(h,n) for(int i=h;i<=n;i++)
#define down(h,n) for(int i=h;i>=n;i--)
#define wh(x) while(x--)
#define node struct node
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
constexpr int N = 20005;
constexpr int mod = 1e9 + 7;
typedef int SElemType;


node{
  int x;
  int y;
  int s;
  int temp;
};
const int next1[] = { 0,1,0,-1 };
const int next2[] = { 1,0,-1,0 };
int a[55][55];
int book[55][55][5];
int n, m;
node r, t;
queue<node>q;
int mod1(int x) {
	return (x + 4) % 4;
}
void bfs() {
	int flag = 0;
	r.temp = 0;
	q.push(r);
	book[r.x][r.y][r.s] = 1;
	while (!q.empty()) {
		node p = q.front();
		q.pop();
		if (p.x == t.x && p.y == t.y) {
			cout << p.temp << '\n';
			flag = 1;
			break;
		}
		up(1, 3) { // 遍历走路的四种情况
			int tx = p.x + i * next1[p.s];
			int ty = p.y + i * next2[p.s];
			if (tx <= 0 || tx >= n || ty <= 0 || ty >= m || a[tx][ty] == 1)
				break;  
			if (book[tx][ty][p.s])
				continue;
			book[tx][ty][p.s] = 1;
			q.push({ tx,ty,p.s,p.temp + 1 });
		}
		if (!book[p.x][p.y][mod1(p.s + 1)]) {  // 向左转
			book[p.x][p.y][mod1(p.s + 1)] = 1;
			q.push({ p.x, p.y, mod1(p.s + 1), p.temp + 1 });
		}
		if (!book[p.x][p.y][mod1(p.s - 1)]) { // 向右转
			book[p.x][p.y][mod1(p.s - 1)] = 1;
			q.push({ p.x, p.y, mod1(p.s - 1), p.temp + 1 });
		}		
	}
	if (!flag) {
		cout << -1 << '\n';
	}
}
int main() {
	cin >> n >> m;
	up(1, n) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
			if (a[i][j] == 1) {// 网格图转化为点图
				a[i - 1][j - 1] = a[i - 1][j] = a[i][j - 1] = 1;
			}
		}
	}
	cin >> r.x >> r.y >> t.x >> t.y;
	char ch;
	cin >> ch;
	switch (ch) {
		case 'E':r.s = 0; break;
		case 'S':r.s = 1; break;
		case 'W':r.s = 2; break;
		case 'N':r.s = 3; break;//方向
	}
	bfs();
	return 0;
}

题目链接:A - Frequency​​​​​​

题解:这道题只需要定义一个整型数组去计数即可,用小标,输入字母后减去97或者减去‘a’即可得到下标,进行下标++;再来个循环找最大值最后输出即可

AC代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>

using namespace std;

using ll = long long;
using ull = unsigned long long;
#define up(i, h, n) for (ll i = h; i <= n; i++) 
#define down(i, h, n) for(ll i = h; i >= n; i--)
#define wh(x) while(x--)
#define node struct node
#define it ::iterator
#define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
constexpr int N = 205;
constexpr int mod = 1e9 + 7;

string s;
int a[28];
int flag = 0,max1=0,t=0;
int main() {
	Ios;
	cin >> s;
	up(i, 0, s.size()) {
		a[s[i] - 97]++;
	}
	up(i, 0, 25) {
		if (a[i] > a[max1]) max1 = i;
	}
    cout << (char)(max1 + 97) << '\n';
	return 0;
}

题目链接:C - We Got Everything Covered!

题解:这个题也是很巧妙,只需要输出n个从小到达k长度的字母即可

AC代码:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,k;
        scanf("%d%d",&n,&k);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<k;j++)
            {
                printf("%c",'a'+j);
            }
        }
        printf("\n");
    }
    return 0;
}

  • 23
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值