Skip to content

Instantly share code, notes, and snippets.

@hanjae-jea
Last active June 7, 2020 06:21
Show Gist options
  • Save hanjae-jea/5bac523bb4cb668921c2ddc3ead92446 to your computer and use it in GitHub Desktop.
Save hanjae-jea/5bac523bb4cb668921c2ddc3ead92446 to your computer and use it in GitHub Desktop.
#include<stdio.h>
int mapSizeX, mapSizeY, startX, startY, direction, map[51][51] = { 0, };
int dx[4] = { 0,0, -1,1 };
int dy[4] = { -1,1, 0,0 };
void move(int curX, int curY, int curDir)
{
if (curY < 1 || curY > mapSizeY || curX < 1 || curX > mapSizeX)
{
return;
}
else
{
map[curX][curY]=1;
if(curDir==1)
{
if(map[curX-1][curY]==1)
{
return;
}
else if(map[curX-1][curY]==2)
{
curDir=3;
move(curX,curY,curDir);
}
else
{
move(curX-1,curY,curDir);
}
}
else if(curDir==2)
{
if(map[curX+1][curY]==1)
{
return;
}
else if(map[curX+1][curY]==2)
{
curDir=4;
move(curX,curY,curDir);
}
else
{
move(curX+1,curY,curDir);
}
}
else if(curDir==3)
{
if(map[curX][curY+1]==1)
{
return;
}
else if(map[curX][curY+1]==2)
{
curDir=2;
move(curX,curY,curDir);
}
else
{
move(curX,curY+1,curDir);
}
}
else
{
if(map[curX][curY-1]==1)
{
return;
}
else if(map[curX][curY-1]==2)
{
curDir=1;
move(curX,curY,curDir);
}
else
{
move(curX,curY-1,curDir);
}
}
}
}
int main()
{
scanf("%d %d", &mapSizeY, &mapSizeX);
for (int i = 1; i <= mapSizeX; i++)
{
for (int j = 1; j <= mapSizeY; j++)
scanf("%d", &map[i][j]);
}
scanf("%d %d", &startY, &startX);
direction=3;
move(startX, startY,direction);
for (int i = 1; i <= mapSizeX; i++)
{
for (int j = 1; j <= mapSizeY; j++)
printf("%d ", map[i][j]);
printf("\n");
}
}
#include <iostream>
using namespace std;
int map[1000][1000];
int n;
int d=3;
int dx[4]={0,-1,0,1};
int dy[4]={1,0,-1,0};
int count=1;
void move(int iy,int ix,int d)
{
map[iy][ix]=count;
if(count>=n*n) return;
count++;
if(map[iy+dy[d]][ix+dx[d]]!=0 || iy+dy[d]<0 || ix+dx[d]<0 || iy+dy[d]>=n || ix+dx[d]>=n)
{
d++;
d%=4;
count = count - 1;
}
else
{
iy+=dy[d];
ix+=dx[d];
}
move(iy,ix,d);
}
int main()
{
cin >> n;
move(0,0,3);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout << map[i][j] << " ";
}
cout << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment