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

本文介绍了一个基于C++的编程挑战,旨在帮助角色Thor找到光明之源。通过两种不同的算法实现——原始解法与向量分解法,文章详细展示了如何通过条件判断和坐标调整来引导角色到达目标位置。

问题描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值