Labirin,,,,???!!!

lrk-maze.gif

 

Somehow, we want to solve one pathway labirin using “Right Hand Rules”. Always put right hand on the wall, then you will reach the exit if the exit is reachable.
Example simulation is attached. The simulation use C programming language. Just compile it, and run.

Enjoy.

#include<stdio.h>
#include<windows.h>
int director(char labirin[12][12],int sb[2],int pointer);
void move(char labirin[12][12],int sb[2]);
void print(char labirin[12][12]);

int main()
{
	system("PAUSE");
	char labirin [12][12] =
		{{"##.#########"},
		{"#...#......#"},
		{"..#.#.####.#"},
		{"###.#....#.#"},
		{"#....###.#.#"},
		{"##...#.#.#.#"},
		{"#....#...#.#"},
		{"##.#####.#.#"},
		{"#........#.#"},
		{"######.###.#"},
		{"#......#...#"},
		{"##########.#"}};
	int sb[2]={0,2};
	int j,i;

	if(labirin[1][2]=='#' && labirin[0][3]=='#')
	{
		labirin[0][2]='O';
		Sleep(500);
		print(labirin);
		Sleep(500);
		printf("\nNo exit");
		return 0;
	}
	move(labirin,sb);

}

void print(char labirin[12][12])
{
	int i,j;
	system("cmd /c cls");
	for(i=0;i<12;i++)
	{
		for(j=0;j<12;j++)
		{
			printf("%c ",labirin[j][i]);
		}
	printf("\n");
	}
}

int director(char labirin[12][12],int sb[2] ,int pointer)
{
	int i,temp;
	int a[]={0,1};
	int b[]={1,0};
	int c[]={0,-1};

	if(pointer==1)
	{
		temp=a[0]; a[0]=a[1]; a[1]=-temp;
		temp=b[0]; b[0]=b[1]; b[1]=-temp;
		temp=c[0]; c[0]=c[1]; c[1]=-temp;
	}
	else if(pointer==2)
	{
		a[0]=-a[0]; a[1]=-a[1];
		b[0]=-b[0]; b[1]=-b[1];
		c[0]=-c[0]; c[1]=-c[1];
	}
	else if(pointer==3)
	{
		temp=a[0]; a[0]=-a[1]; a[1]=temp;
		temp=b[0]; b[0]=-b[1]; b[1]=temp;
		temp=c[0]; c[0]=-c[1]; c[1]=temp;
	}
	if (labirin[sb[0]+a[0]][sb[1]+a[1]]=='#')
	{
		if (labirin[sb[0]+b[0]][sb[1]+b[1]]=='.'||
  		    labirin[sb[0]+b[0]][sb[1]+b[1]]=='X')
		{
			return 0;

		}
		else if(labirin[sb[0]+b[0]][sb[1]+b[1]]=='#')
		{
			if (labirin[sb[0]+c[0]][sb[1]+c[1]]=='.' || 
			    labirin[sb[0]+c[0]][sb[1]+c[1]]=='X')
			{
				return 1;

			}
			else
			{
				return 2;
			}
		}
	}
	else
	{
		return 3;
	}
}

void move(char labirin[12][12],int sb[2])
{
	static int pointer=0;
	if (!(sb[0]==0&&sb[1]==2&&pointer==0)&&
		sb[0]==0||sb[0]==11||sb[1]==11||sb[1]==0)
	{
		if(sb[0]==0 && sb[1]==2)
		{
			printf("\nNo Exit");
		}
		else
		{
			labirin[sb[0]][sb[1]]='O';
			print(labirin);
			labirin[sb[0]][sb[1]]='X';
			Sleep(500);
			print(labirin);
			printf("\nSuccess out from labirin!!");
			Sleep(5000);
		}

	}
	else
	{
		labirin[sb[0]][sb[1]]='O';
		print(labirin);
		Sleep(500);
		if(labirin[sb[0]][sb[1]]=='O' || 
		   labirin[sb[0]][sb[1]]=='X')
		{
			labirin[sb[0]][sb[1]]='X';
		}
		print(labirin);
		pointer=(pointer+director(labirin,sb,pointer))%4;
		if(pointer==0)
			sb[0]=sb[0]+1;
		else if(pointer==1)
			sb[1]=sb[1]-1;
		else if(pointer==2)
			sb[0]=sb[0]-1;
		else if(pointer==3)
			sb[1]=sb[1]+1;
		move(labirin,sb);
	}
}

3 thoughts on “Labirin,,,,???!!!

  1. iya mas,ini salah satu program awal-awal pada saat saya masuk kuliah, belum banyak tahu tentang algoritma, jadi dirty code gini deh,^_^
    makasih ya dah mau mampir,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s