CodinGame: Power of Thor 反思

来源:互联网 发布:网络程蝶依视频 编辑:程序博客网 时间:2024/05/17 09:44

Power of Thor 雷神托尔的能量

主界面

The Goal

Your program must allow Thor to reach the light of power.
//你的程序必须能让Thor到达能量之光

Rules

Thor moves on a map which is 40 wide by 18 high. Note that the coordinates (X and Y) start at the top left! This means the most top left cell has the coordinates “X=0,Y=0” and the most bottom right one has the coordinates “X=39,Y=17”.
//Thor在一个40*18的地图上移动。注意左上方建立的坐标系,左上角为(0,0)和右下角(39,17),Thor的位置超出界限(X<0,X>39,Y<0,Y>17)都算输
//coordinate: 坐标
关卡1

Once the program starts you are given:

//在任务的开始,你会得到:
·the variable lightX: the X position of the light of power that Thor must reach.
·the variable lightY: the Y position of the light of power that Thor must reach.
·the variable initialTX: the starting X position of Thor. //Thor开始的位置
·the variable initialTY: the starting Y position of Thor.

Game Input

//游戏输入
The program must first read the initialization(初始化) data from the standard input, then, in an infinite(无限) loop, provides on the standard output the instructions(指令) to move Thor.

Initialization input

//初始化输入
Line 1: 4 integers(整数) lightX lightY initialTX initialTY.
(lightX, lightY) indicates(表明) the position of the light.
(initialTX, initialTY) indicates the initial position of Thor.

Input for a game round

//游戏每回合输入(游戏显示)
Line 1: the number of remaining moves for Thor to reach the light of power: remainingTurns. You can ignore this data but you must read it.

Output for a game round

//游戏每回合输出(游戏需要输出的指令)
A single line providing the move to be made: N NE E SE S SW W or NW
东南西北

原程序

#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. * 提示:当Thor没有按照你预想的方式来移动时,可以使用Debug流来输出initialTX,initialTY(**非常好的提示,可以用来查看你所要查看的数据,以便更好的熟悉程序中各变量的变化,及定位**) **/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();    // 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;        //尝试输出remainingTurns,是Thor的能量值(可以移动的次数)        /* A single line providing the move to be made: N NE E SE S SW W or NW */        cout << "SE" << endl;//SE表示向东南方向移动一格位移        //一开始想了蛮久,它是用EWSN表示"东西南北"的移动    }}

思路1:

简单的使用“上下左右”,但不够灵活,消耗的能量值多,且没有加上Thor位置的变化
可以通过1、2关

        if (lightX > initialTX) cout << "E" << endl;        if (lightX < initialTX) cout << "W" << endl;        if (lightY > initialTY) cout << "S" << endl;        if (lightY < initialTY) cout << "N" << endl;

思路2:

枚举,想把每一个方向的变化所要进行的动作都表示出了
可以通过1、2关

        if (lightX > initialTX && lightY > initialTY)         cout << "SE" << endl;        else if (lightX < initialTX && lightY < initialTY)         cout << "NW" << endl;        else if (lightX < initialTX && lightY > initialTY)         cout << "SW" << endl;        else if (lightX > initialTX && lightY < initialTY)         cout << "NE" << endl;        else if (lightX > initialTX) cout << "E" << endl;        else if (lightX < initialTX) cout << "W" << endl;        else if (lightY > initialTY) cout << "S" << endl;        else if (lightY < initialTY) cout << "N" << endl;

思路3:

优化,有了分类思想,先探讨lightX与initialTX的关系

        if (lightX > initialTX) {            if (lightY > initialTY) cout << "SE" << endl;            else if (lightY < initialTY) cout << "NE" << endl;            else cout << "E" << endl;        }        else if (lightX < initialTX) {            if (lightY > initialTY) cout << "SW" << endl;            else if (lightY < initialTY) cout << "NW" << endl;            else cout << "W" << endl;             }        else if (lightX == initialTX) {            if (lightY > initialTY) cout << "S" << endl;            else cout << "N" << endl;            }

Standard Output Stream: SW
Game information:
Failure: Thor wandered off the path and died (invalid position).
Thor position = (17,18). Light position = (0,17). Energy = 31
//这边initialTY总是会大于lightY,没法及时停下来,向左移动
失败

思路3.5:

与思路3类似,突然想到initialTX和initialTY的值,并纠正

//后期……发现initialTX,initialTY真的就是只是Thor的初始位置坐标//并没有随着Thor的移动而变化,所以就需要自己去主动改变Thor的初始值//使用Debug流        if (lightX > initialTX) {            if (lightY > initialTY) {                cout << "SE" << endl;                initialTX++; initialTY++;}            else if (lightY < initialTY) {               cout << "NE" << endl;                initialTX++; initialTY--;}            else  {                cout << "E" << endl;               initialTX++;}        }        else if (lightX < initialTX) {            if (lightY > initialTY) {                cout << "SW" << endl;                initialTX--; initialTY++;}            else if (lightY < initialTY) {                cout << "NW" << endl;                initialTX--; initialTY--;}            else {                cout << "W" << endl;                initialTX--;}             }        else if (lightX == initialTX) {            if (lightY > initialTY) {                cout << "S" << endl;                initialTY++;}            else {                cout << "N" << endl;                initialTY--;}            }     }

通关程序

#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;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();    // game loop    while (1) {        int remainingTurns;         cin >> remainingTurns; cin.ignore();        if (lightX > initialTX) {            if (lightY > initialTY) {                cout << "SE" << endl;                initialTX++; initialTY++;}            else if (lightY < initialTY) {               cout << "NE" << endl;                initialTX++; initialTY--;}            else  {                cout << "E" << endl;               initialTX++;}        }        else if (lightX < initialTX) {            if (lightY > initialTY) {                cout << "SW" << endl;                initialTX--; initialTY++;}            else if (lightY < initialTY) {                cout << "NW" << endl;                initialTX--; initialTY--;}            else {                cout << "W" << endl;                initialTX--;}             }        else if (lightX == initialTX) {            if (lightY > initialTY) {                cout << "S" << endl;                initialTY++;}            else {                cout << "N" << endl;                initialTY--;}            }     }}

成功
//成功获得能量之光

收获:

1、使用Debug流,cerr << “Debug messages … ” << endl;
2、加深了分类思想,选择结构的认识 if … else …