关注
#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;
}
查看原帖
点赞 评论
相关推荐
01-10 01:08
门头沟学院 Java 点赞 评论 收藏
分享
01-10 10:26
南京邮电大学 Java 点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 在大厂上班是一种什么样的体验 #
14030次浏览 184人参与
# 机械人避雷的岗位/公司 #
44635次浏览 315人参与
# 论秋招对个人心气的改变 #
15030次浏览 205人参与
# 我的省钱小妙招 #
37526次浏览 446人参与
# 程序员找工作至少要刷多少题? #
22757次浏览 285人参与
# 工作前VS工作后,你的心态变化 #
38358次浏览 266人参与
# 为了减少AI幻觉,你注入过哪些设定? #
7003次浏览 197人参与
# 通信/硬件求职避坑tips #
139858次浏览 1086人参与
# 牛客AI体验站 #
8833次浏览 221人参与
# 毕业论文进行时 #
27601次浏览 153人参与
# 运营来爆料 #
77340次浏览 467人参与
# 你的mentor是什么样的人? #
51414次浏览 727人参与
# 你现在会用到哪些AI技能? #
24756次浏览 137人参与
# 找实习多的是你不知道的事 #
1789036次浏览 20670人参与
# 运营人的第一份offer应该如何选 #
218845次浏览 1260人参与
# 我现在比当时_,你想录用我吗 #
9899次浏览 136人参与
# 哪一瞬间让你觉得工作好累 #
75697次浏览 431人参与
# 12306一秒售罄,你抢到回家的票了吗? #
2509次浏览 52人参与
# 应届生进小公司有什么影响吗 #
118678次浏览 1162人参与
# 一张图晒一下你的AI员工 #
6183次浏览 136人参与