Created
January 24, 2012 14:49
-
-
Save alexnask/1670531 to your computer and use it in GitHub Desktop.
Solving a simple problem sub-optimally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <cstdlib> | |
| #include <sstream> | |
| class Table { | |
| private: | |
| int cells[10][10]; | |
| int currX, currY; | |
| static int times; | |
| int cell(int x, int y) { | |
| if( x < 0 || y < 0 || x > 9 || y > 9) { | |
| return -1; | |
| } | |
| return cells[y][x]; | |
| } | |
| void set(int x, int y, int value) { | |
| cells[y][x] = value; | |
| } | |
| std::string toString() { | |
| std::string str = "--------------------------------------------------\n"; | |
| for(int y = 0; y < 10; y++) { | |
| str += "|"; | |
| for(int x = 0; x < 10; x++) { | |
| std::stringstream ss; | |
| ss << cell(x,y); | |
| if(cell(x,y) < 10) { | |
| str += " " + ss.str() + " | "; | |
| } else if(cell(x,y) < 100){ | |
| str += ss.str() + " |"; | |
| } else { | |
| str += ss.str() + "|"; | |
| } | |
| } | |
| str += "\n"; | |
| } | |
| str += "--------------------------------------------------"; | |
| return str; | |
| } | |
| public: | |
| Table() { | |
| Table(0,0); | |
| } | |
| Table(int _currX, int _currY) : currX(_currX), currY(_currY) { | |
| for(int i = 0; i < 10; i++) { | |
| for(int j = 0; j < 10; j++) { | |
| set(j,i,0); | |
| } | |
| } | |
| set(currX,currY,1); | |
| } | |
| void next() { | |
| bool processed = false; | |
| // Case 1: Up | |
| if(cell(currX, currY - 3) == 0) { | |
| processed = true; | |
| Table *table = new Table(*this); | |
| table->set(currX,currY - 3, cell(currX,currY) + 1); | |
| currY -= 3; | |
| table->next(); | |
| delete table; | |
| } | |
| // Case 2: Down | |
| if(cell(currX, currY + 3) == 0) { | |
| processed = true; | |
| Table *table = new Table(*this); | |
| table->set(currX,currY + 3, cell(currX,currY) + 1); | |
| currY += 3; | |
| table->next(); | |
| delete table; | |
| } | |
| // Case 3: Right | |
| if(cell(currX + 3, currY) == 0) { | |
| processed = true; | |
| Table *table = new Table(*this); | |
| table->set(currX + 3,currY, cell(currX,currY) + 1); | |
| currX += 3; | |
| table->next(); | |
| delete table; | |
| } | |
| // Case 4: Left | |
| if(cell(currX - 3, currY) == 0) { | |
| processed = true; | |
| Table *table = new Table(*this); | |
| table->set(currX - 3,currY, cell(currX,currY) + 1); | |
| currX -= 3; | |
| table->next(); | |
| delete table; | |
| } | |
| // Case 5: Top-Right | |
| if(cell(currX + 2, currY - 2) == 0) { | |
| processed = true; | |
| Table *table = new Table(*this); | |
| table->set(currX + 2,currY - 2, cell(currX,currY) + 1); | |
| currX += 2; | |
| currY -= 2; | |
| table->next(); | |
| delete table; | |
| } | |
| // Case 6: Top-Left | |
| if(cell(currX - 2, currY - 2) == 0) { | |
| processed = true; | |
| Table *table = new Table(*this); | |
| table->set(currX - 2,currY - 2, cell(currX,currY) + 1); | |
| currX -= 2; | |
| currY -= 2; | |
| table->next(); | |
| delete table; | |
| } | |
| // Case 7: Bottom-Left | |
| if(cell(currX - 2, currY + 2) == 0) { | |
| processed = true; | |
| Table *table = new Table(*this); | |
| table->set(currX - 2,currY + 2, cell(currX,currY) + 1); | |
| currX -= 2; | |
| currY += 2; | |
| table->next(); | |
| delete table; | |
| } | |
| // Case 8: Bottom-Right | |
| if(cell(currX + 2, currY + 2) == 0) { | |
| processed = true; | |
| Table *table = new Table(*this); | |
| table->set(currX + 2,currY + 2, cell(currX,currY) + 1); | |
| currX += 2; | |
| currY += 2; | |
| table->next(); | |
| delete table; | |
| } | |
| if(!processed) { | |
| bool check = true; | |
| for(int y = 0; y < 10; y++) { | |
| for(int x = 0; x < 10; x++) { | |
| if(cell(x,y) < 1) { | |
| check = false; | |
| } | |
| } | |
| } | |
| if(check) { | |
| std::cout << "Here is your solution:" << std::endl << toString() << std::endl; | |
| } else if(cell(currX,currY) >= 85) { | |
| std::cout << "Nearly got there, " << cell(currX,currY) << " out of 100 D:" << std::endl << "Here is the table anyway:" << std::endl << toString() << std::endl; | |
| } else { | |
| Table::times += 1; | |
| if(Table::times % 1000000 == 0) { | |
| std::cout << Table::times << " impasses reached." << std::endl; | |
| } | |
| } | |
| } | |
| } | |
| }; | |
| int Table::times = 0; | |
| int main(int argc, char* argv[]) { | |
| if(argc != 3) { | |
| for(int y = 0; y < 10; y++) { | |
| for(int x = 0; x < 10; x++) { | |
| Table *table = new Table(x,y); | |
| table->next(); | |
| delete table; | |
| std::cout << "Processing finished for 1 at (x , y) = (" << x << " , " << y <<")!" << std::endl; | |
| } | |
| } | |
| } else { | |
| for(int y = atoi(argv[2]); y < 10; y++) { | |
| for(int x = atoi(argv[1]); x < 10; x++) { | |
| Table *table = new Table(x,y); | |
| table->next(); | |
| delete table; | |
| std::cout << "Processing finished for 1 at (x , y) = (" << x << " , " << y <<")!" << std::endl; | |
| } | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment