#include <iostream>
#include <vector>
#include <string>
#define Me 0
#define Ene 1
using namespace std;
int T;
int pos[2][3];
int map[9][9];
int Distance(){
return abs(pos[0][0]-pos[1][0])+abs(pos[0][1]-pos[1][1]);
}
int GetMap(int i, int j){
if ( i < 0 || j < 0 || i >= 9 || j >= 9)
return -1;
else
return map[i][j];
}
int GetScore(){
int score = 0;
for (int i = 0; i < 9; i++)
for(int j= 0; j < 9; j++)
if(map[i][j] == 1)
score++;
else if(map[i][j] == 2)
score--;
return score;
}
const int dx[4] ={ 0, 1, 0,-1};
const int dy[4] ={-1, 0, 1, 0};
int main(){
cin >> T;
for (int i = 0; i < 2; i++){
cin >> pos[i][0] >> pos[i][1] >> pos[i][2];
}
for(int i = 0; i <9; i++)
for(int j = 0; j < 9; j++)
cin >> map[j][i];
if(Distance() <= 2){
if(GetScore() > 0){
if(pos[1][0] - pos[0][0] > 0){
cout << 2 << endl;
}else if(pos[1][0] - pos[0][0] < 0){
cout << 4 << endl;
}else if(pos[1][1] - pos[0][1] > 0){
cout << 3 << endl;
}else if(pos[1][1] -pos[0][1] < 0){
cout << 1 << endl;
}
return 0;
}
if(pos[1][2] < 3){
cout << 5 << endl;
return 0;
}
}
for(int i = 0; i < 4; i++){
if(GetMap(pos[0][0]+dx[i],pos[0][1]+dy[i]) == 2){
cout << i+1 << endl;
return 0;
}
}
for(int i = 0; i < 4; i++){
if(GetMap(pos[0][0]+dx[i],pos[0][1]+dy[i]) == 0){
cout << i+1 << endl;
return 0;
}
}
for(int i = 0; i < 4; i++){
if(GetMap(pos[0][0]+dx[i],pos[0][1]+dy[i]) != -1){
cout << i+1 << endl;
return 0;
}
}
}
Battle History