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编号.每个动 ...
随机推荐
- 【UNIX网络编程第三版】阅读笔记(一):代码环境搭建
粗略的阅读过<TCP/IP详解>和<计算机网络(第五版)>后,开始啃这本<UNIX网络编程卷一:套接字联网API>,目前linux下的编程不算太了解,在阅读的过程中 ...
- B2B、B2C、B2D的简单理解
B2D现在非常流行,顾名思义,B2D 就是指那些以开发者为对象的服务,它们通过 API 等形式"售卖"自己某一方面的特长.B2D(Business to Developer)市场很 ...
- R12: Improving Performance of General Ledger and Journal Import (Doc ID 858725.1 )
In this Document Purpose Scope Details A) Database Init.ora Parameters B) Concurrent Progr ...
- C++ Primer 有感(复制控制)
1.不管类是否定义了自己的析构函数,编译器都 自动执行类中非static数据成员的析构函数. 2.如果我们没有定义复制构造函数,编译器就会为我们合成一个.合成复制构造函数的行为是,执行逐个成员初始化, ...
- C语言中的sizeof解析
1. 定义:sizeof是C/C++中的一个操作符(operator),作用就是返回一个对象或者类型所占的内存字节数.返回 值类型为size_t,在头文件stddef.h中定义.这是一个依赖于编译系统 ...
- BAT Android工程师面试流程解析+还原最真实最完整的一线公司面试题
尊重原创,转载请写明原文出处:http://blog.csdn.net/sk719887916/article/details/47040931 (skay) 求职和我们每个人息息相关,而求职也有门道 ...
- HTML5 全屏 API
翻译人员: 铁锚 原文日期: 2013年12月23日 翻译日期: 2013年12月29日 原文链接: Fullscreen API 在越来越真实的web应用程序中,JavaScript也变得越来越给力 ...
- Xcode调试非异常导致崩溃的程序
如果App不是因为一个异常而崩溃,Xcode可能任然会指向main()函数为出错位置. 在这种情况下,你可能遇上了更低级别的问题.也许是一个除以0错误或是缓冲溢出问题,或者你寻址一个已经被释放的对象. ...
- MySQL正则表达式初步
如果想要了解完整的MySQL手册, 请访问: MySQL 5.1参考手册 你还可以学习: MySQL学习精粹 我们知道,在SQL之中,可以用 like 这个谓词(表达式) 来进行模糊检索,并支持 %, ...
- Android4.0Sd卡移植之使用vold自动挂载sd卡
在cap631平台上移植android4.0,发现内核驱动没有任何问题,能够读写,当总不能挂载. 后来发现是因为自动挂载需要vold的支持.vold程序负责检查内核的 sysfs 文件系统,发现有SD ...