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: 坐标
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 …
- CodinGame: Power of Thor 反思
- CodinGame: The Descent 反思
- CodinGame: Temperatures 反思
- 利用缓冲区实现“向量分解” - Power of Thor - Episode 1 [CodingGame技巧总结]
- Power of two,power of three,power of four
- [LeetCode] Power of Three | Power of Two
- The power of Reading
- The power of algorithm
- Power of Cryptography POJ2109
- JOJ1728:Power of Cryptography
- poj21029 Power of Cryptography
- POJ2109 Power of Cryptography
- 113 - Power of Cryptography
- UVa Power of Cryptography
- 113 - Power of Cryptography
- F - Power of Cryptography
- uva Power of Cryptography
- POJ2109--Power of Cryptography
- Android 5.0 以上 截屏并保存为PNG方案
- 70. Climbing Stairs -- 动态规划、斐波那契数列
- 关于Ceph的EC实现rollback的两个问题
- bzoj1217: [HNOI2003]消防局的设立
- 解决nodejs不支持async和await关键字的问题
- CodinGame: Power of Thor 反思
- codeforces 620C Pearls in a Row
- Docker网络管理及容器跨主机通信
- linux下程序的标准输入流输出流错误流
- trie树-leetcode-421. Maximum XOR of Two Numbers in an Array
- Linux 串口编程<三> 使用termios与API进行串口程序开发
- activiti eclipse集成
- android广角相机畸变校正算法和实现
- 安卓TextView文本不满一行由于英文,标点符号等原因换行