Task: | Grid Paths |
Sender: | bits pls |
Submission time: | 2019-05-25 15:55:54 +0300 |
Language: | C++ |
Status: | READY |
Result: | TIME LIMIT EXCEEDED |
test | verdict | time | |
---|---|---|---|
#1 | TIME LIMIT EXCEEDED | -- | details |
Compiler report
input/code.cpp: In function 'int solve(int, int, std::vector<std::vector<int> >&)': input/code.cpp:8:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] if (i >= nums.size() || j >= nums[0].size()) { ~~^~~~~~~~~~~~~~ input/code.cpp:8:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] if (i >= nums.size() || j >= nums[0].size()) { ~~^~~~~~~~~~~~~~~~~ input/code.cpp:9:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] if (i == nums.size() && j == nums[0].size() - 1 || i == nums.size() - 1 && j == nums.size()) { ~~^~~~~~~~~~~~~~ input/code.cpp:9:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] if (i == nums.size() && j == nums[0].size() - 1 || i == nums.size() - 1 && j == nums.size()) { ~~^~~~~~~~~~~~~~~~~~~~~ input/code.cpp:9:6...
Code
#include <iostream> #include <vector> using namespace std; int solve(int i, int j, vector< vector<int> > &nums) { if (i >= nums.size() || j >= nums[0].size()) { if (i == nums.size() && j == nums[0].size() - 1 || i == nums.size() - 1 && j == nums.size()) { return nums[nums.size()-1][nums.size()-1]; } return 0; } int curr = nums[i][j]; //cout << i << " " << j << " curr: "<< curr << endl; int a = solve(i, j+1, nums); //cout << i << " " << j << " a: "<< a << endl; int sum = 0; if (a > 0) { sum += curr + a; } int b = solve(i+1, j, nums); //cout << i << " " << j << " b: "<< b << endl; if (b > 0) { sum += curr + b; } return sum; } int main() { int n; cin >> n; vector< vector<int> > nums; int num; for (int i = 0; i < n; i++) { vector<int> row; for (int j = 0; j < n; j++) { cin >> num; row.push_back(num); } nums.push_back(row); } cout << (solve(0, 1, nums) + solve(1, 0, nums))/(n-1) << endl; }