1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
using namespace std;
int maxu = -1;
void utility(int x, int y, string mat[12][12],int val,int steps){
if (steps == 0){
maxu = max(maxu, val);
return;
}
else if (mat[x][y]=="d" || mat[x][y] == "v"){
return;
} else {
string ori = mat[x][y];
int curr = atoi(ori.c_str());
mat[x][y] = "v";
utility(x-1,y,mat,val+curr,steps-1); //north
utility(x,y+1,mat,val+curr,steps-1); //east
utility(x+1,y,mat,val+curr,steps-1); //south
utility(x,y-1,mat,val+curr,steps-1); //south
mat[x][y] = ori;
}
}
int main(){
int N;
cin >> N;
string mat[12][12] = {"d"};
int row = 0;
int col = 0;
for (int i=1;i<=N;i++){
for (int j=1;i<=N;j++){
string x;
cin >> x;
if (x == "*"){row = i; col = j; mat[i][j]="v";}
mat[i][j] = x;
}
}
utility(row+1,col,mat,0,10);
utility(row-1,col,mat,0,10);
utility(row,col-1,mat,0,10);
utility(row,col+1,mat,0,10);
cout << maxu << endl;
return 0;
}
|