#include<iostream>
#include<vector>
#include<array>
#define n 21
using namespace std;
array<int, 20> dpx;
array<int, 20> dpy;
int main(void)
{
string c = "LLRRUUDD";
int i,j,k,l, xpos =5,ypos=5,len=0,csize=0,ret=1;
array<array<int, n>, n> arr;
for (i = 0; i < n; i++)
{
if (i == 1 || i == n-1)
arr[i].fill(1);
else
arr[i].fill(0);
}
for (i=1; i < n; i++)
{
arr[i][1] = 1;
arr[i][n-1] = 1;
}
dpx[0] = xpos;
dpy[0] = ypos;
arr[dpy[0]][dpx[0]] = 3;
for (k = 1; k < n; k++)
{
for (j = 1; j < n; j++)
cout << arr[k][j] << " ";
cout << endl;
}
cout << endl;
i = 0;
csize = c.length();
while (csize--)
{
if (arr[ypos][xpos] == 1)
break;
for (l = 1; l <= len; l++)
{
if (xpos == dpx[l] && ypos == dpy[l])
{
ret = 0;
break;
}
}
if (ret == 0)
break;
if (arr[ypos][xpos] == 2)
{
len++;
dpx[len] = dpx[len - 1];
dpy[len] = dpy[len - 1];
}
for (k = len; k > 0; k--)
{
if(k == len)
arr[dpy[len]][dpx[len]] = 0;
dpx[len] = dpx[len - 1];
dpy[len] = dpy[len - 1];
arr[dpy[len]][dpx[len]] = 3;
}
arr[ypos][xpos] = 0;
if (c[i] == 'L')
xpos--;
else if (c[i] == 'R')
xpos++;
else if (c[i] == 'U')
ypos++;
else if (c[i] == 'D')
ypos--;
arr[ypos][xpos] = 3;
for (k = 1; k < n; k++)
{
for (j = 1; j < n; j++)
cout << arr[k][j] << " ";
cout << endl;
}
cout << endl;
i++;
}
}
게임을 올리쇼...
릴리즈 모드로 컴파일 해서!
사실 게임형태는 아니고, 게임을 가장한 알고리즘 문제풀이입니다..
여기서 실행시간 더 최적화시키는 방법이 있을까해서요