首页 > 学院 > 开发设计 > 正文

利用缓冲区实现“向量分解” - Power of Thor - Episode 1 [CodingGame技巧总结]

2019-11-11 02:53:56
字体:
来源:转载
供稿:网友

问题描述

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表