Submission #336032

# Submission time Handle Problem Language Result Execution time Memory
336032 2020-12-14T14:13:17 Z monus1042 Maja (COCI18_maja) C++17
44 / 110
903 ms 65540 KB
// NK
#include <bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

typedef pair<int,int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
#define pb push_back
#define eb emplace_back
#define pob pop_back
#define psf push_front
#define pof pop_front
#define mkp make_pair
#define mkt make_tuple
#define all(x) x.begin(), x.end()
#define Bolivia_is_nice ios::sync_with_stdio(false), cin.tie(nullptr)

//typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ord_set;

int ma[100][100];
map<tuple<int,int,int>, ll> dp;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int a,b,n,m;

ll f(int r, int c, int st){
	if (st == -1){ // base case
		if (r == a && c == b) return 0;
		return -2;
	}
	//general case
	auto ff = dp.find(mkt(r,c,st));
	if (ff != dp.end()) return dp[mkt(r,c,st)];
	dp[mkt(r,c,st)] = -2;
	ll &ans = dp[mkt(r,c,st)];
	for (int i=0; i<4; i++){
		int ro = r+dy[i];
		int co = c+dx[i];
		if (0<=ro && ro<n && 0<=co && co<m){
			ll temp = f(ro, co, st-1);
			if (temp >= 0) ans = max(ans, temp + ma[r][c]);
		}
	}
	return ans;
}

void solve(){
	cin>>n>>m;
	cin>>a>>b;
	a--, b--;
	int k; cin >> k;
	for (int i=0; i<n; i++){
		for (int j=0; j<m; j++){
			cin >> ma[i][j];
		}
	}
	cout << f(a,b,k-1) << '\n';
}

int main(){
	Bolivia_is_nice;
	solve();
	
	return 0;
}
/*
  ~/.emacs
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 5760 KB Output is correct
2 Correct 737 ms 29676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 143 ms 7332 KB Output is correct
2 Correct 903 ms 34524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 253 ms 11244 KB Output is correct
2 Correct 288 ms 13932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 300 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 293 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 298 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 293 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 296 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 293 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -