#include <iostream>
#include <cstring>
#define WIDTH 4
#define SHARK 100
#define BLANK -1
using namespace std;
typedef struct Fish {
int num;
int row;
int col;
int direction; // ์๋ถํฐ ๋ฐ์๊ณ๋ฐฉํฅ์ผ๋ก 1~8
bool isAlive;
} Fish;
int map[WIDTH][WIDTH];
Fish fish[WIDTH*WIDTH+1];
Fish shark = { 0,0,0,0 };
int getSumOfFishNum();
void eat(int row, int col);
void moveFish();
void changeFish(int index, int dstIndex, int row = 0, int col = 0);
int getNextSharkRow() {
if (shark.direction == 1 || shark.direction == 2 || shark.direction == 8)
return -1;
else if (shark.direction == 4 || shark.direction == 5 || shark.direction == 6)
return 1;
else
return 0;
}
int getNextSharkCol() {
if (shark.direction == 2 || shark.direction == 3 || shark.direction == 4)
return -1;
else if (shark.direction == 6 || shark.direction == 7 || shark.direction == 8)
return 1;
else
return 0;
}
int main() {
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < WIDTH; j++) {
Fish temp = { 0, i, j, 0, true};
cin >> temp.num >> temp.direction;
fish[temp.num] = temp;
map[i][j] = temp.num;
}
}
/*
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < WIDTH; j++)
cout << map[i][j] << " ";
cout << endl;
}
for (int i = 0; i < WIDTH * WIDTH + 1; i++) {
cout << fish[i].num << " " << fish[i].row << " " << fish[i].col << " " << fish[i].direction << " " << fish[i].isAlive << endl;
}
*/
eat(0, 0);
cout << getSumOfFishNum();
}
int getSumOfFishNum() {
moveFish();
//์์ด ์ฎ๊ธฐ๊ธฐ. ๊ฐ๋ฅํ ๊ณณ์ ์ฎ๊ธฐ๊ณ , ์ฎ๊ธธ๋๋ง๋ค ๊ฐ ์ ์ฅ ํ ๋ค์ ์ด๊ธฐํ
int nextRow = getNextSharkRow();
int nextCol = getNextSharkCol();
Fish tempShark = shark;
Fish tempFish[WIDTH*WIDTH+1];
memcpy(tempFish, fish, sizeof(tempFish));
int tempMap[WIDTH][WIDTH];
memcpy(tempMap, map, sizeof(map));
int deep = 1, maxNum = 0;
while (true) {
int r = shark.row + deep*nextRow;
int c = shark.col + deep*nextCol;
if (r < 0 || r >= WIDTH || c < 0 || c >= WIDTH) {
break;
}
else if (map[r][c] == BLANK) {
deep++;
}
else {
eat(r, c);
int a = getSumOfFishNum();
maxNum = a > maxNum ? a : maxNum;
memcpy(map, tempMap, sizeof(map));
memcpy(fish, tempFish, sizeof(tempFish));
shark = tempShark;
deep++;
}
}
if (maxNum == 0)
return shark.num;
return maxNum;
}
void eat(int row, int col) {
int index = map[row][col];
map[shark.row][shark.col] = BLANK;
map[row][col] = SHARK;
shark.row = row;
shark.col = col;
shark.num += fish[index].num;
shark.direction = fish[index].direction;
fish[index].isAlive = false;
}
void moveFish() {
int count = 0;
for (int i = 1; i <= 16; i++) {
if (!fish[i].isAlive)
continue;
if (fish[i].direction == 1 && fish[i].row != 0 && map[fish[i].row - 1][fish[i].col] != SHARK) {
changeFish(i, map[fish[i].row - 1][fish[i].col], fish[i].row-1, fish[i].col);
count = 0;
}
else if (fish[i].direction == 2 && (fish[i].row != 0 && fish[i].col != 0) && map[fish[i].row - 1][fish[i].col - 1] != SHARK) {
changeFish(i, map[fish[i].row - 1][fish[i].col - 1], fish[i].row - 1, fish[i].col-1);
count = 0;
}
else if (fish[i].direction == 3 && fish[i].col != 0 && map[fish[i].row][fish[i].col - 1] != SHARK) {
changeFish(i, map[fish[i].row][fish[i].col - 1], fish[i].row, fish[i].col-1);
count = 0;
}
else if (fish[i].direction == 4 && (fish[i].row != 3 && fish[i].col != 0) && map[fish[i].row + 1][fish[i].col - 1] != SHARK) {
changeFish(i, map[fish[i].row + 1][fish[i].col - 1], fish[i].row + 1, fish[i].col-1);
count = 0;
}
else if (fish[i].direction == 5 && fish[i].row != 3 && map[fish[i].row + 1][fish[i].col] != SHARK) {
changeFish(i, map[fish[i].row + 1][fish[i].col], fish[i].row + 1, fish[i].col);
count = 0;
}
else if (fish[i].direction == 6 && (fish[i].row != 3 && fish[i].col != 3)&& map[fish[i].row + 1][fish[i].col +1] != SHARK) {
changeFish(i, map[fish[i].row + 1][fish[i].col +1], fish[i].row + 1, fish[i].col+1);
count = 0;
}
else if (fish[i].direction == 7 && fish[i].col != 3 && map[fish[i].row][fish[i].col+1] != SHARK) {
changeFish(i, map[fish[i].row][fish[i].col+1], fish[i].row, fish[i].col+1);
count = 0;
}
else if (fish[i].direction == 8 && (fish[i].row != 0 && fish[i].col != 3) && map[fish[i].row - 1][fish[i].col+1] != SHARK) {
changeFish(i, map[fish[i].row - 1][fish[i].col+1], fish[i].row - 1, fish[i].col+1);
count = 0;
}
else {
if (fish[i].direction != 8)
fish[i].direction++;
else
fish[i].direction = 1;
count++;
if (count >= 8)
continue;
i--;
}
}
}
void changeFish(int index, int dstIndex, int row, int col) {
if (dstIndex == BLANK) {
map[fish[index].row][fish[index].col] = BLANK;
map[row][col] = index;
fish[index].row = row;
fish[index].col = col;
}
else {
int tempRow = fish[dstIndex].row;
int tempCol = fish[dstIndex].col;
map[fish[dstIndex].row][fish[dstIndex].col] = index;
map[fish[index].row][fish[index].col] = dstIndex;
fish[dstIndex].row = fish[index].row;
fish[dstIndex].col = fish[index].col;
fish[index].row = tempRow;
fish[index].col = tempCol;
}
}