top of page

CCC '24 J5 - Harvest Waterloo

#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;
}

Recent Posts

See All

CCC 2013 J5/S3

#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> #include <cmath> using namespace std; char...

Comments


bottom of page