GlitchBot -HZNU寒假集训
One of our delivery robots is malfunctioning! The job of the robot is simple; it should follow a list of instructions in order to reach a target destination. The list of instructions is originally correct to get the robot to the target. However, something is going wrong as we upload the instructions into the robot’s memory. During the upload, one random instruction from the list takes on a different value than intended. Yes, there is always a single bad instruction in the robot’s memory and it always results in the robot arriving at an incorrect destination as it finishes executing the list of instructions.
The robot can execute the instructions “Left”, “Right”, and “Forward”. The “Left” and “Right” instructions do not result in spatial movement but result in a 9090-degree turn in the corresponding direction. “Forward” is the only instruction that results in spatial movement, causing the robot to move one unit in the direction it is facing. The robot always starts at the origin (0,0)(0,0) of a grid and faces north along the positive y-axis.
Given the coordinates of the target destination and the list of instructions that the robot has in its memory, you are to identify a correction to the instructions to help the robot reach the proper destination.
Input
The first line of the input contains the xx and yy integer coordinates of the target destination, where −50≤x≤50−50≤x≤50 and −50≤y≤50−50≤y≤50. The following line contains an integer nn representing the number of instructions in the list, where 1≤n≤501≤n≤50. The remaining nn lines each contain a single instruction. These instructions may be: “Left”, “Forward”, or “Right”.
Output
Identify how to correct the robot’s instructions by printing the line number (starting at 11) of an incorrect input instruction, followed by an instruction substitution that would make the robot reach the target destination. If there are multiple ways to fix the instructions, report the fix that occurs for the earliest line number in the sequence of instructions. There is always exactly one unique earliest fix.
Sample Input 1 | Sample Output 1 |
---|---|
3 2 |
8 Right |
Sample Input 2 | Sample Output 2 |
---|---|
-1 1 |
1 Forward |
题解:给一个指定的点,给机器人下指令,前进、向右或者向左转,然后机器人出了故障,中间有一个命令出错了(可能有多种解决方案,输出最早的),找到第几个出错,并输出正确指令。
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
int x, y;
int n;
char ins[][];
int map[][] = {};
int go[][] = { {,},{,},{-,},{,-} };
int main()
{
int turn = ;
scanf("%d %d", &x, &y);
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%s", ins[i]);
}
for (int i = ; i <= n; i++)
{
if (strcmp(ins[i], "Forward") == )
{
strcpy(ins[i],"Right");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
map[][] = ;
map[][] = ;
strcpy(ins[i], "Left");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
strcpy(ins[i], "Forward");
map[][] = ;
map[][] = ;
}
else if (strcmp(ins[i], "Right") == )
{ strcpy(ins[i], "Forward");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
map[][] = ;
map[][] = ;
strcpy(ins[i], "Left");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
strcpy(ins[i], "Right");
map[][] = ;
map[][] = ;
}
else
{ strcpy(ins[i], "Right");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
map[][] = ;
map[][] = ;
strcpy(ins[i], "Forward");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
strcpy(ins[i], "Left");
map[][] = ;
map[][] = ;
} }
return ;
}
GlitchBot -HZNU寒假集训的更多相关文章
- Wooden Sticks -HZNU寒假集训
Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in a ...
- 今年暑假不AC - HZNU寒假集训
今年暑假不AC "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" "@#$%^&a ...
- FatMouse' Trade -HZNU寒假集训
FatMouse' Trade FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the wa ...
- 畅通工程-HZNU寒假集训
畅通工程 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只 ...
- 并查集模板题(The Suspects )HZNU寒假集训
The Suspects Time Limit: 1000MS Memory Limit: 20000KTotal Submissions: 36817 Accepted: 17860 Descrip ...
- CSU-ACM寒假集训选拔-入门题
CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...
- 中南大学2019年ACM寒假集训前期训练题集(基础题)
先写一部分,持续到更新完. A: 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...
- 2022寒假集训day2
day1:学习seach和回溯,初步了解. day2:深度优化搜索 T1 洛谷P157:https://www.luogu.com.cn/problem/P1157 题目描述 排列与组合是常用的数学方 ...
- 食物链-HZUN寒假集训
食物链 总时间限制: 1000ms 内存限制: 65536kB 描述 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动 ...
随机推荐
- java同步synchronized
java同步synchronized volatile仅仅用来保证该变量对所有线程的可见性,但不保证原子性. 看下面的这段代码: /** * * @author InJavaWeTrust * */ ...
- Ubuntu启动eclipse问题
Ubuntu启动eclipse问题 在Ubuntu安装eclipse后 可以在终端顺利启动eclipse:进入eclipse目录,在终端输入命令 ./eclipse > /dev/null 2& ...
- 【Android 应用开发】Android 图表绘制 achartengine 示例解析
作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/38420197 一. AChartEngine 简介 1. 项 ...
- GDAL库中WFS服务中含有中文不能获取数据的问题
GDAL库中目前提供了对WFS服务发布的数据进行获取,目前发现对于中文的服务名称或者图层名为中文,GDAL不能正确识别.通过调试发现,其原因有下面两点: 1.输入的URL路径没有使用UTF8编码而从网 ...
- tomcat的realm域
Realm域,其实可以看成是一个包含了用户及密码的数据库,而且每个用户还会包含了若干角色.也就是包含了用户名.密码.角色三个列的数据记录集合,如下图,最下面椭圆内的包含的整块即可以看成realm域.它 ...
- 环境连接报错(最大连接数超过) APP-FND-01516
数据库用户登录服务器,sqlplu 解决办法: 先把界面上要保存的操作保存好 应用用户登录,切换到ora用户 杀掉进程 ps -fu ora | grep LOCAL=NO|grep -v grep| ...
- Android 5.0 SEAndroid下如何获得对一个内核节点的访问权限
-9]* u:object_r:tty_device:s0 # We add here /dev/wf_bt u:object_r:wf_bt_device:s0 wf_bt ...
- Python的time(时间戳与时间字符串互相转化)
strptime("string format")字符串如"20130512000000"格式的 输入处理函数 localtime(float a)时间戳的输入 ...
- 【算法导论】八皇后问题的算法实现(C、MATLAB、Python版)
八皇后问题是一道经典的回溯问题.问题描述如下:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8*8个方格),使它们谁也不能被吃掉? 看到这个问题,最容易想 ...
- 【Qt编程】Qt学习笔记<三>
1. 如果程序中使用了png以外格式的图片,在发布程序时就要将Qt安装目录下plugins中的imagineformats文件复制到发布文件中. 2. 在函数声明处快速添加函数定义 ...