Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- int calcDistance(char, int, int);
- int calcCursor(char, int, int, bool[]);
- void changePositions(char, int*, int*);
- int main(){
- string code = "i7345678912345e234567891234e";
- int codeSize = code.length();
- //Array that holds whether the letter has been used
- bool codeMap[codeSize];
- for(int i = 0; i < codeSize; i++){
- codeMap[i] = false;
- }
- //Default position on the keyboard
- int yPosition = 0;
- int xPosition = 0;
- //Default cursor position
- int cursor = 0;
- int maxTable[codeSize];
- for(int i = 0; i < codeSize; i++){
- maxTable[i] = 0;
- }
- int distance = 0;
- for(int j = 0; j < codeSize; j++){
- for(int i = 0; i < codeSize; i++){
- maxTable[i] = 0;
- }
- //Goes through filling the table of the distance required for each key press
- for(int i = 0; i < codeSize; i++){
- if(codeMap[i] != true){ //Letter hasn't been used
- if(calcDistance(code[i], xPosition, yPosition) > calcCursor(code[i], i, cursor, codeMap)){ //Takes longer to move position
- maxTable[i] = calcDistance(code[i], xPosition, yPosition);
- cout << calcDistance(code[i], xPosition, yPosition) << " ";
- }
- else if(calcDistance(code[i], xPosition, yPosition) < calcCursor(code[i], i, cursor, codeMap)){ //Takes longer to move cursor
- maxTable[i] = calcCursor(code[i], i, cursor, codeMap);
- cout << calcCursor(code[i], i, cursor, codeMap) << " ";
- }
- else{ //The distance and cursor distance are the same
- maxTable[i] = calcDistance(code[i], xPosition, yPosition);
- cout << calcDistance(code[i], xPosition, yPosition) << " ";
- }
- }
- else{ //The letter has already been used
- maxTable[i] = 1000;
- }
- }
- int minValue = 1000;
- int minLetter = 100;
- for(int i = 0; i < codeSize; i++){
- if(maxTable[i] <= minValue){
- minValue = maxTable[i];
- minLetter = i;
- }
- }
- cout << code[minLetter] << "(" << minLetter << ")" << endl;
- distance += minValue;
- //Remove and shift the code, set new positions
- codeMap[minLetter] = true;
- cursor = minLetter+1;
- changePositions(code[minLetter], &xPosition, &yPosition);
- }
- cout << endl;
- cout << distance << endl;
- system("pause");
- return 0;
- }
- int calcDistance(char letter, int xPosition, int yPosition){
- int distance = 0;
- int changeX;
- int changeY;
- vector<vector <char>> alphabet{
- { '1', '2', '3', '4', '5', '6', '7', '8', '9' },
- { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' },
- { 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' },
- { 'z', 'x', 'c', 'v', 'b', 'n', 'm' }
- };
- for (vector<vector<char>>::size_type j = 0; j < alphabet.size(); j++){
- for (vector<char>::size_type k = 0; k < alphabet[j].size(); k++){
- if (alphabet[j][k] == letter){
- changeX = k - xPosition;
- changeY = j - yPosition;
- distance += abs(changeX) +abs(changeY);
- yPosition = j;
- xPosition = k;
- }
- }
- }
- return distance;
- }
- int calcCursor(char letter, int letterPosition, int cursor, bool codeMap[]){
- int distance = 0;
- if(cursor < letterPosition){
- for(int i = cursor; i < letterPosition; i++){
- if(codeMap[i] == true){
- distance++;
- }
- }
- }
- else if(cursor > letterPosition){
- for(int i = cursor; i > letterPosition+1; i--){
- if(codeMap[i] == true){
- distance++;
- }
- }
- }
- else{
- return 0; //The cursor is on the letter already
- }
- if(distance > 14){
- distance -= 13;
- }
- return distance;
- }
- void changePositions(char letter, int *xPosition, int *yPosition){
- vector<vector <char>> alphabet{
- { '1', '2', '3', '4', '5', '6', '7', '8', '9' },
- { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' },
- { 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' },
- { 'z', 'x', 'c', 'v', 'b', 'n', 'm' }
- };
- for (vector<vector<char>>::size_type j = 0; j < alphabet.size(); j++){
- for (vector<char>::size_type k = 0; k < alphabet[j].size(); k++){
- if (alphabet[j][k] == letter){
- *yPosition = j;
- *xPosition = k;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement