Codewars : Take a Ten Minute Walk(步行十分钟)

纪念第一次刷Codewars

1.问题描述

原题

∙ \bullet You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones – everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. [‘n’, ‘s’, ‘w’, ‘e’]). You always walk only a single block for each letter (direction) and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don’t want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.

∙ \bullet Note: you will always receive a valid array containing a random assortment of direction letters (‘n’, ‘s’, ‘e’, or ‘w’ only). It will never give you an empty array (that’s not a walk, that’s standing still!).


翻译

∙ \bullet 您住在笛卡尔市,那里的所有道路都以完美的网格布局。您提前十分钟到达预约地点,所以您决定趁机散步。该城市在其手机上为市民提供了一个“步行生成”应用程序-每次您按下按钮时,都会向您发送一个由一个字母组成的字符串数组,这些字符串代表行走的方向(例如[‘n’,‘s’,‘w’, ‘e’])。您总是每个字母(方向)只走一个街区,并且知道走过一个城市街区要花费一分钟,因此创建一个函数,如果应用程序给您走,它将返回true,而该函数将花费您正好十分钟(不想早或晚!),当然,它将使您回到起点。否则返回false。

∙ \bullet 注意:您将始终收到一个有效的数组,其中包含方向字母的随机分类(仅’n’,‘s’,‘e’或’w’)。它永远不会给您一个空数组(这不是步行,而是停滞不前!)。

难 度 : 6 k y u {\color{Purple} 难度:6kyu} 6kyu


2.问题分析

∙ \bullet 题目重述:给定一个数组,其中有四个方向的走向,当你走十次并刚好走回原点时,返回true,否则返回false。

∙ \bullet 思路:首先判断是否走十分钟。然后分别考虑每个方向,用x来控制左右方向,向左走加1,向右走减1;用y来控制上下方向,向上走加1,向下走减1。初始化 x = 0 , y = 0 , x=0,y=0, x=0,y=0,最后看x和y是否仍然等于0。

∙ \bullet 优化:使用一些特定的函数可以使算法更加的简便。


3.代码

∙ \bullet 普通做法:

#include<vector>
bool isValidWalk(std::vector<char> walk) {
  	if(walk.size() != 10) return false;
  	int x=0, y=0;
  	for(char c : walk){
    	switch(c) {
      		case 'n': y++; break;
      		case 's': y--; break;
      		case 'e': x++; break;
      		case 'w': x--; break;
    	}
  	}
  	if(x == 0 && y == 0) return true;
  	return false;
}

∙ \bullet 使用函数:

#include<vector>
#include <algorithm> 

//判断条件为是否刚好等于十分钟,并且向左走的步数等于向右走的步数,向上走的步数等于向下走的步数。
bool isValidWalk(std::vector<char> walk) {
  return walk.size() == 10 && 
         std::count(walk.begin(), walk.end(), 'e') == std::count(walk.begin(), walk.end(), 'w') &&
         std::count(walk.begin(), walk.end(), 'n') == std::count(walk.begin(), walk.end(), 's');
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比奇堡咻飞兜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值