关注
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
vector<vector<int> > map;
int maxf = INT_MIN;
vector<pair<int,int>> shortesresult;
/*
To find the path with least cost;
Move left or right costs 1
Move Up costs 3
Move down costs 0
input:
4 4 10 // n m p p is total allowed cost
1 1 1 1
1 1 1 1
0 0 0 1
0 0 1 1
*/
bool dfs(int x,int y,int n,int m,int
p,vector<pair<int,int>> &result){
if(p <= 0){
return false; //
}
if(x == 0 and y == m){
result.push_back({x,y});
for(auto &p : result){
//cout << '[' << p.first <<
"," << p.second << ']';
}
//cout << p << endl;
if(p > maxf){
shortesresult = result;
maxf = p;
}
result.pop_back(); // bug!!!!!!
return true;
}
if(x < 0 || y < 0 || x > n || y > m ){
return false;
}
if(map[x][y] <= 0){
return false;
}
//cout << x << " " << y
<< " " << p << endl;
result.push_back({x,y});
map[x][y] = -1;
bool r1 = dfs(x + 1,y,n,m,p,result);
bool r2 = dfs(x,y + 1,n,m,p - 1,result);
bool r3 = dfs(x,y - 1,n,m,p - 1,result);
bool r4 = dfs(x - 1,y,n,m,p - 3,result);
result.pop_back();
map[x][y] = 1;
return false;
}
int main(){
int n,m,p,tmp;
cin >> n >> m >>p;
map = vector<vector<int>
>(n,vector<int>(m,0));
for(int i = 0; i < n; i ++){
for(int j = 0;j < m;j ++){
cin >> tmp;
map[i][j] = tmp;
}
}
vector<pair<int,int>> result;
bool isFirst = true;
dfs(0,0,n-1,m-1,p,result);
if(shortesresult.size() != 0){
for(auto &p : shortesresult){
if(!isFirst){
cout << ',';
}
cout << '[' << p.first << ","
<< p.second << ']';
isFirst = false;
}
}else{
cout << "Can not escape!" << endl;
}
cout << endl;
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 长得好看会提高面试通过率吗? #
8277次浏览 85人参与
# 面试被问第一学历差时该怎么回答 #
273905次浏览 2223人参与
# MiniMax求职进展汇总 #
26606次浏览 327人参与
# 沪漂/北漂你觉得哪个更苦? #
3067次浏览 65人参与
# 百度工作体验 #
316651次浏览 2233人参与
# 你的实习产出是真实的还是包装的? #
5259次浏览 91人参与
# 巨人网络春招 #
11765次浏览 235人参与
# 离家近房租贵VS离家远但房租低,怎么选 #
17109次浏览 138人参与
# 学历or实习经历,哪个更重要 #
242839次浏览 1259人参与
# AI面会问哪些问题? #
1787次浏览 54人参与
# 从事AI岗需要掌握哪些技术栈? #
1151次浏览 39人参与
# 你做过最难的笔试是哪家公司 #
2475次浏览 33人参与
# HR最不可信的一句话是__ #
1519次浏览 42人参与
# 春招至今,你的战绩如何? #
20408次浏览 198人参与
# 找AI工作可以去哪些公司? #
1154次浏览 21人参与
# 校招生月薪1W算什么水平 #
134628次浏览 456人参与
# AI时代,哪个岗位还有“活路” #
4011次浏览 92人参与
# XX请雇我工作 #
51219次浏览 172人参与
# 简历第一个项目做什么 #
32939次浏览 424人参与
# 你最满意的offer薪资是哪家公司? #
77193次浏览 377人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
153221次浏览 893人参与
# 秋招白月光 #
734084次浏览 5454人参与
查看5道真题和解析