#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
int main()
{
int r, c, sr, sc;
cin >> r;
cin >> c;
int p[r][c];
bool visited[r][c];
for(int i = 0; i < r; i++){
string one;
cin>>one;
for(int j = 0; j < c; j++){
visited[i][j] = false;
if(one[j] == '*'){
p[i][j] = -1;
}
if(one[j] == 'L'){
p[i][j] = 10;
}
if(one[j] == 'M'){
p[i][j] = 5;
}
if(one[j] == 'S'){
p[i][j] = 1;
}
}
}
cin >> sr;
cin >> sc;
stack<pair<int, int>> dfs;
pair<int, int> start = {sr, sc};
dfs.push(start);
visited[sr][sc] = true;
int result = p[sr][sc];
while(!dfs.empty()){
pair<int, int> cur = dfs.top();
dfs.pop();
int cur_r = cur.first;
int cur_c = cur.second;
//down
if(cur_r+1 < r && p[cur_r+1 ][cur_c] > 0){
if(!visited[cur_r+1][cur_c] ){
visited[cur_r+1][cur_c] = true;
result += p[cur_r+1][cur_c];
dfs.push({cur_r+1, cur_c});
}
}
//up
if(cur_r-1 >= 0 && p[cur_r -1 ][cur_c] > 0){
if(!visited[cur_r-1][cur_c] ){
visited[cur_r-1][cur_c] = true;
result += p[cur_r-1][cur_c];
dfs.push({cur_r-1, cur_c});
}
}
//left
if(cur_c-1 >= 0 && p[cur_r][cur_c-1] > 0){
if( !visited[cur_r][cur_c - 1]){
visited[cur_r][cur_c - 1] = true;
result += p[cur_r][cur_c-1];
dfs.push({cur_r, cur_c-1});
}
}
//right
if(cur_c+1 < c && p[cur_r][cur_c+1] > 0){
if( !visited[cur_r][cur_c + 1]){
visited[cur_r][cur_c+1] = true;
result += p[cur_r][cur_c+1];
dfs.push({cur_r, cur_c+1});
}
}
}
std::cout<<result<<endl;
}
top of page
Recent Posts
See Alls1 = input() s2 = input() silly = '' silly_o = '' quiete = '-' i = 0 j = 0 while i < len(s1) and j < len(s2): if s1[i] != s2[j]: if...
#include<iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; bool rowcom(pair<int, int> a, pair<int,...
#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> #include <cmath> using namespace std; char...
bottom of page
Comments