利用缓冲区实现“向量分解” - Power of Thor - Episode 1 [CodingGame技巧总结]
来源:互联网 发布:js判断字符串出现次数 编辑:程序博客网 时间:2024/05/29 07:51
问题描述
Your program must allow Thor to reach the light of power.
题目地址:
https://www.codingame.com/ide/puzzle/power-of-thor-episode-1
原始解法
通过嵌套的if条件语句来枚举各类情况:
#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;/** * Auto-generated code below aims at helping you parse * the standard input according to the problem statement. * --- * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders. **/int main(){ int lightX; // the X position of the light of power int lightY; // the Y position of the light of power int initialTX; // Thor's starting X position int initialTY; // Thor's starting Y position cin >> lightX >> lightY >> initialTX >> initialTY; cin.ignore(); int currentX = initialTX; int currentY = initialTY; // game loop while (1) { int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line. cin >> remainingTurns; cin.ignore(); // Write an action using cout. DON'T FORGET THE "<< endl" // To debug: cerr << "Debug messages..." << endl; // if both offset, then move in diaganol line if (currentX - lightX > 0) { if (currentY - lightY > 0){ cout << "NW" << endl; currentX--; currentY--; } else if (currentY - lightY <0){ cout << "SW" << endl; currentX--; currentY++; cerr << currentX << endl; }else { cerr << "now Y same" <<endl; cout << "W" <<endl; currentX--; } } else if (currentX - lightX < 0) { if (currentY - lightY > 0){ cout << "NE" << endl; currentX++; currentY--; } else if (currentY - lightY <0){ cout << "SE" << endl; currentX++; currentY++; }else { cout << "E" <<endl; currentX++; } } else { cerr << "now X same" <<endl; if (currentY - lightY > 0){ cout << "N" << endl; currentY--; } else if (currentY - lightY < 0){ cout << "S" << endl; currentY++; }else { // in position cerr << "now Y same" <<endl; } } // A single line providing the move to be made: N NE E SE S SW W or NW // cout << "SE" << endl; }}
“向量分解”解法
#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;/** * Auto-generated code below aims at helping you parse * the standard input according to the problem statement. * --- * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders. **/int main(){ int lightX; // the X position of the light of power int lightY; // the Y position of the light of power int initialTX; // Thor's starting X position int initialTY; // Thor's starting Y position cin >> lightX >> lightY >> initialTX >> initialTY; cin.ignore(); int dx = lightX - initialTX; int dy = lightY - initialTY; // game loop while (1) { int remainingTurns; cin >> remainingTurns; cin.ignore(); // Write an action using cout. DON'T FORGET THE "<< endl" // To debug: cerr << "Debug messages..." << endl; if (dy > 0) { cout << "S"; dy--; } if (dy < 0) { cout << "N"; dy++; } if (dx > 0) { cout << "E"; dx--; } if (dx < 0) { cout << "W"; dx++; } cout << endl; // A single line providing the move to be made: N NE E SE S SW W or NW }}
技巧分析
在物理学中可以对速度向量进行向量分解,最常用的是分解为X、Y方向两个速度分量。
在C++实现中,虽然我们不能简单引入速度这个概念,但是通过缓冲区的暂存,可以让我们通过单独判定X、Y方向的关系,分别向缓冲区中写入内容,最后用endl一并输出。
潜在问题是如果向缓冲区中写出过多字符,可能因为缓冲区已满而自动输出了。应该可以通过临时变量来解决。
技巧泛化
当输出结果与判定条件都可以进行某种对应形式的分解时,可以适当利用缓冲区(输出缓冲区,或者临时变量)达到分解的目的,从而简化程序逻辑并提高效率。
参考
CodingGame ,ower of Thor - Episode 1,C++最高票答案,2017.02.07
0 0
- 利用缓冲区实现“向量分解” - Power of Thor - Episode 1 [CodingGame技巧总结]
- CodinGame: Power of Thor 反思
- 1 Power of Cryptography
- POJ 2109 Power of Cryptography 语言技巧
- poj_2109 Power of Cryptography(技巧)
- codeforces 704A Thor [技巧吧.]
- [LeetCode] Power of K问题总结
- leecode 解题总结:326. Power of Three
- leecode 解题总结:342. Power of Four
- 数据缓冲区高速缓冲区学习总结1
- 自定义View利用缓冲区实现绘画板功能
- Thor(CF #366 Div. 1)
- 326. Power of Three#1
- 利用GPU实现向量数据的相加
- 利用位向量实现海量数据存储
- HTML5 Episode.1
- POJ 2109 Power of Cryptography double的运算技巧问题
- The Secret of Strand Hill (Episode 113)
- adb通过wifi连接android设备
- JAVA日志
- vue-cli构建vue项目
- CSS3数字时钟
- 答《mysql事务-隔离级别》
- 利用缓冲区实现“向量分解” - Power of Thor - Episode 1 [CodingGame技巧总结]
- 为什么API多用C而不是C++,为什么C++程序大多不使用异常
- $.ajax()方法详解
- MFC中picture控件,将bitmap根据控件大小缩放
- xamarin 中文乱码问题
- 仿射变换详解 warpAffine
- Leetcode: Combination Sum III
- 蓝桥杯 k好数 动态规划
- Web基础架构:负载均衡和LVS