#include <stdio.h>
#define UP 0
#define RIGHT 1
#define DOWN 2
#define LEFT 3
#define MIRROR_1 1
#define MIRROR_2 2
#define SIZE 5
int mirror[SIZE][SIZE] = {
{ 0, 0, 0, 0, 2},
{ 0, 1, 0, 2, 0},
{ 0, 2, 0, 0, 1},
{ 0, 0, 0, 1, 0},
{ 0, 0, 0, 0, 1},
};
int process(int x, int y, int direction);
int main (int argc, char **argv) {
int count = process(0, 0, RIGHT);
printf ("%d\n", count);
return 0;
}
int process(int x, int y, int direction) {
int count = 0;
int isEnd = 0;
while ( isEnd == 0 && x >= 0 && x < SIZE && y >= 0 && y < SIZE) {
switch(direction) {
case UP:
while (y >= 0 && mirror[y][x] == 0) {
y--;
}
// printf ("Up %d %d\n", x, y);
break;
case RIGHT:
while (x < SIZE && mirror[y][x] == 0) {
x++;
}
// printf ("Right %d %d\n", x, y);
break;
case DOWN:
while (y < SIZE && mirror[y][x] == 0) {
y++;
}
// printf ("Down %d %d\n", x, y);
break;
case LEFT:
while (x >= 0 && mirror[y][x] == 0) {
x--;
}
// printf ("LEFT %d %d\n", x, y);
break;
default:
break;
}
if ( x >= 0 && x < SIZE && y >= 0 && y < SIZE ){
if ( mirror[y][x] == MIRROR_1 ) {
direction ^= 1;
count++;
} else if ( mirror[y][x] == MIRROR_2 ) {
direction ^= 3;
count++;
}
switch (direction) {
case UP : y--; break;
case RIGHT : x++; break;
case LEFT : x--; break;
case DOWN : y++; break;
}
//printf ("%d %d %d\n",direction, x, y);
} else {
isEnd = 1;
}
};
return count;
}
'Coding > CPP 삽질기' 카테고리의 다른 글
사각형 개수 구하기 (0) | 2014.11.12 |
---|---|
대칭행렬 (0) | 2014.10.24 |
Programming 실습 예제 (0) | 2014.03.28 |