关注
#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;
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 今年春招是金一银二嘛? #
27208次浏览 251人参与
# 机械制造2024笔面经 #
1514979次浏览 12994人参与
# 没关系,至少我的__很曼妙 #
11535次浏览 178人参与
# 软开人,秋招你打算投哪些公司呢 #
176282次浏览 1312人参与
# 牛客吐槽大会 #
10089次浏览 179人参与
# 帆软软件工作体验 #
10162次浏览 46人参与
# AI求职实录 #
16661次浏览 391人参与
# 快手年终开大包 #
3840次浏览 50人参与
# 抛开难度不谈,你最想去哪家公司? #
14962次浏览 217人参与
# 赚钱的意义在这一刻具象化 #
11292次浏览 211人参与
# 为什么有人零实习也能进大厂? #
14027次浏览 239人参与
# 你的第一家实习公司是什么档次? #
12544次浏览 133人参与
# 考研人,我有话说 #
163944次浏览 1243人参与
# 总结:哪家公司面试体验感最好 #
79642次浏览 445人参与
# 1月小结:你过的开心吗? #
4929次浏览 84人参与
# Prompt分享 #
17546次浏览 406人参与
# AI时代的工作 VS 传统时代的工作,有哪些不同? #
16071次浏览 366人参与
# 当你问AI“你会取代我的工作吗”,它说_? #
8746次浏览 232人参与
# 实习生活中那些难忘的瞬间 #
293250次浏览 3222人参与
# 实习最想跑路的瞬间 #
113074次浏览 694人参与