Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <chrono>
- #include <vector>
- #include <iostream>
- #include <sys/time.h>
- #define RAND_MAX 5
- /*
- 1
- 2 3
- 4
- */
- int grid[5][5] = {
- {0, 0, 0, 0, 2},
- {0, 0, 0, 0, 2},
- {0, 0, 0, 0, 2},
- {0, 0, 0, 0, 2},
- {0, 0, 0, 0, 2}
- };
- int InitPos[2] = {2, 2};
- int MaxExp = 5000000;
- bool Success = false;
- int StepCount = 0;
- int ExpNumber = 1;
- int AntsBag = 0;
- void Init();
- void CarryFood(int * pos);
- void LeftFood(int * pos);
- bool checkMovability(int * pos, int direction);
- bool moveToDirection(int pos[2], int direction);
- bool checkSuccess();
- void ShowResult(std::vector <int> &l_path);
- FILE * file = fopen("FinalDataUnsig.txt", "a+");
- int main(int argc, char const *argv[])
- {
- time_t t;
- srand((unsigned)time(&t));
- fprintf(file, "%%Total Experiment Number : %d. Exp Number-Step Count-Pathn", MaxExp);
- while(ExpNumber <= MaxExp)
- {
- Init();
- std::vector <int> path;
- int pos[2];
- pos[0] = InitPos[0];
- pos[1] = InitPos[1];
- do{
- int direction = (rand() % 4) + 1;
- if (moveToDirection(pos, direction))
- {
- StepCount++;
- path.push_back(direction);
- }
- if (pos[1] == 4&&grid[pos[0]][4]==2&&AntsBag==0)
- {
- CarryFood(pos);
- }
- if (pos[1] == 0&&grid[pos[0]][0]==0&&AntsBag==2)
- {
- LeftFood(pos);
- }
- checkSuccess();
- }
- while(!Success);
- ShowResult(path);
- ExpNumber++;
- }
- fclose(file);
- return 0;
- }
- void Init()
- {
- Success = false;
- StepCount = 0;
- AntsBag = 0;
- int gridInit[5][5] = {
- {0, 0, 0, 0, 2},
- {0, 0, 0, 0, 2},
- {0, 0, 0, 0, 2},
- {0, 0, 0, 0, 2},
- {0, 0, 0, 0, 2}
- };
- for (int i = 0; i < 5; ++i)
- {
- for (int j = 0; j < 5; ++j)
- {
- grid[i][j] = gridInit[i][j];
- }
- }
- }
- void ShowResult(std::vector <int> &l_path)
- {
- fprintf(file, "%d %d ", ExpNumber, StepCount);
- if (file != NULL)
- {
- for (std::vector<int>::iterator it = l_path.begin(); it != l_path.end(); it++)
- {
- fprintf(file, "%d", *it);
- }
- }
- fprintf(file, "n");
- }
- void CarryFood(int * pos)
- {
- if (pos[1] == 4&&grid[pos[0]][4]==2&&AntsBag==0)
- {
- AntsBag = 2;
- grid[pos[0]][4] = 0;
- }
- }
- void LeftFood(int * pos)
- {
- if (pos[1] == 0&&grid[pos[0]][0]==0&&AntsBag==2)
- {
- AntsBag = 0;
- grid[pos[0]][0] = 2;
- }
- }
- bool checkMovability(int * pos, int direction)
- {
- switch(direction)
- {
- case 1:
- {
- if(pos[1]==0){
- return false;
- }
- break;
- }
- case 2:
- {
- if (pos[0]==0)
- {
- return false;
- }
- break;
- }
- case 3:
- {
- if (pos[0]==4)
- {
- return false;
- }
- break;
- }
- case 4:
- {
- if (pos[1]==4)
- {
- return false;
- }
- break;
- }
- default:
- {
- printf("Wrong direction input is given!!n");
- return false;
- break;
- }
- }
- return true;
- }
- bool moveToDirection(int * pos, int direction)
- {
- if ( !checkMovability(pos, direction) )
- {
- return false;
- }
- switch(direction){
- case 1:
- {
- pos[1] -= 1;
- break;
- }
- case 2:
- {
- pos[0] -= 1;
- break;
- }
- case 3:
- {
- pos[0] += 1;
- break;
- }
- case 4:
- {
- pos[1] += 1;
- break;
- }
- default:
- {
- printf("I'm stunned!n");
- return false;
- break;
- }
- }
- return true;
- }
- bool checkSuccess()
- {
- for (int i = 0; i < 5; ++i)
- {
- if (grid[i][0] != 2)
- {
- return false;
- }
- }
- Success = true;
- return true;
- }
Add Comment
Please, Sign In to add comment