Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <netpbm/pgm.h>
- #include <time.h>
- #include <math.h>
- #define N 100
- #define T 20
- void moveSource(int* x, int* y, float array[N][N], float value){
- int xDirection, yDirection, xSource, ySource;
- xSource = *x;
- ySource = *y;
- xDirection = rand()%2 * 2 - 1;
- yDirection = rand()%2 * 2 - 1;
- xSource += xDirection;
- ySource += yDirection;
- xSource = (xSource==0)? 1: xSource;
- xSource = (xSource==N-1)? N-2 : xSource;
- ySource = (ySource==0)? 1 : ySource;
- ySource = (ySource==N-1)? N-2 : ySource;
- array[xSource][ySource] = value;
- *x = xSource;
- *y = ySource;
- }
- void algo(float array[N][N], int xSource, int ySource, int localSourceX[5], int localSourceY[5]){
- int i;
- int j;
- int k;
- int check;
- for(i = 1 ; i < N-1; i++){
- for(j = 1 ; j < N-1; j++){
- if(i == xSource && j == ySource) continue;
- check = 0;
- for(k = 0; k < 5; k++){
- if(i == localSourceX[k] && j == localSourceY[k]) check = 1;
- }
- if(check) continue;
- array[i][j] = (array[i+1][j] + array[i-1][j] + array[i][j+1] + array[i][j-1])/4.0;
- }
- }
- }
- void climb(int *a, int *b, float array[N][N]){
- int i, j;
- int x;
- x = *a;
- int y;
- y = *b;
- int newX, newY;
- float max;
- max = array[x][y];
- newX = *a;
- newY = *b;
- for(i = x - 1; i <= x + 1; i++){
- for(j = y - 1; j <= y + 1; j++){
- if(i==x && j== y) continue;
- if(i < 0 || i >= N || j < 0 || j >= N) continue;
- if(max < array[i][j]){
- max = array[i][j];
- newX = i;
- newY = j;
- }
- }
- }
- //printf("max = %lf\n", max);
- //printf("Check max: %lf\n", array[newX][newY]);
- *a = newX;
- *b = newY;
- }
- void climbHipek(int *x, int *y, float array[N][N]){
- int tmpX;
- tmpX = *x;
- int tmpY;
- tmpY = *y;
- climb(&tmpX, &tmpY, array);
- if(tmpX == *x && tmpY == *y){
- *x = rand()%N;
- *y = rand()%N;
- }else{
- *x = tmpX;
- *y = tmpY;
- }
- }
- int main()
- {
- gray** image;
- int i, j;
- float array [N][N] = { 0 };
- srand(time(NULL));
- int xSource = rand()%(N-1)+1;
- int ySource = rand()%(N-1)+1;
- array[xSource][ySource] = 100;
- int localSourceX[5] = {0};
- int localSourceY[5] = {0};
- for(i = 0; i < 5; i++){
- localSourceX[i] = rand()%(N-1)+1;
- localSourceY[i] = rand()%(N-1)+1;
- array[localSourceX[i]][localSourceY[i]] = 75.0;
- }
- int iteration = 0;
- int counter = 0;
- char filename[100] = {0};
- int fileNumber = 0;
- FILE* file;
- int hipekWin = 0;
- int xHipek = rand()%N;
- int yHipek = rand()%N;
- int xDirection;
- int yDirection;
- int xDestination = 1;
- int yDestination = 1;
- int xHipDestination = 1;
- int yHipDestination = 1;
- float maxHip = 0;
- int temp = 0;
- float vec[T] = {0};
- int test = 0;
- while(!hipekWin && ++iteration < 250)
- {
- maxHip = 0;
- xHipDestination = xHipek;
- yHipDestination = yHipek;
- for(int z = 0; z < T; z++)
- {
- vec[z] = array[xSource - z][ySource];
- }
- /*for(int z = 1; z < 21; z++)
- {
- printf("%lf ", vec[z]);
- }
- printf("\n");*/
- for(i = xHipek-1; i<=xHipek+1;i++)
- {
- for(j=yHipek-1; j<=yHipek+1; j++)
- {
- maxHip = array[i][j];
- for(int k = 0; k < T; k++)
- {
- if(vec[k]=maxHip)
- {
- test = 1;
- }
- }
- if(i==xHipek && j==yHipek) continue;
- if(test >= 0)
- {
- maxHip = array[i][j];
- xHipDestination = i;
- yHipDestination = j;
- //xHipek = xHipDestination;
- //yHipek = yHipDestination;
- }
- }
- }
- if(test < 1)
- {
- if(xHipek >= N-2) xDestination = -1;
- if(yHipek >= N-2) yDestination = -1;
- if(xHipek <= 1) xDestination = 1;
- if(yHipek <= 1) yDestination = 1;
- xHipek += xDestination;
- yHipek += yDestination;
- }else
- {
- xHipek = xHipDestination;
- yHipek = yHipDestination;
- }
- //climbHipek(&xHipek, &yHipek, array);
- if(xHipek == xSource && yHipek == ySource) hipekWin = 1;
- if((counter+1)%10 == 0)
- {
- moveSource(&xSource, &ySource, array, 100.0);
- for(i = 0 ; i < 5; i++){
- moveSource(&(localSourceX[i]), &(localSourceY[i]), array, 75.0);
- }
- }
- image = pgm_allocarray(N, N);
- for(i=0;i<N;i++)
- {
- for(j=0;j<N;j++)
- {
- image[i][j] = array[i][j]/100.0*255.0;
- if(i==xHipek && j==yHipek)
- image[i][j] = 255.0;
- }
- }
- fileNumber++;
- sprintf(filename, "image%d.pgm", fileNumber);
- file = fopen(filename, "wb");
- pgm_writepgm(file, image, N, N, 255, 1);
- fclose(file);
- algo(array, xSource, ySource, localSourceX, localSourceY);
- counter++;
- }
- printf("Hipek win after %d iteration!\n", counter);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement