社区讨论
警钟短鸣:奇异搞笑DFS无法通过!
P1004[NOIP 2000 提高组] 方格取数参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @mhj94hdb
- 此快照首次捕获于
- 2025/11/03 22:44 4 个月前
- 此快照最后确认于
- 2025/11/03 22:44 4 个月前
搞半天原来是道DP
44分DFS代码:
CPP#include<bits/stdc++.h>
using namespace std;
int List[20][20] , cnt = 0;
int n;
queue<pair<int , int> > q;
void dfs(int x , int y , int cntt , queue<pair<int , int> > Path){
if(x == n and y == n){
if(cnt < cntt){
cnt = cntt;
while(!Path.empty()){
q.push(Path.front());
Path.pop();
}
}
return;
}
if(x + 1 <= n){
Path.push(make_pair(x + 1 , y));
dfs(x + 1 , y , cntt + List[x][y] , Path);
}
if(y + 1 <= n){
Path.push(make_pair(x , y + 1));
dfs(x , y + 1 , cntt + List[x][y] , Path);
}
}
int main(){
cin >> n;
while(true){
int a , b , c;
cin >> a >> b >> c;
if(a == 0 and b == 0 and c == 0){
break;
}
List[a][b] = c;
}
int res = 0;
dfs(1 , 1 , 0 , q);
res += cnt;
while(!q.empty()){
int a = q.front().first , b = q.front().second;
List[a][b] = 0;
q.pop();
}
cnt = 0;
dfs(1 , 1 , 0 , q);
cout << res + cnt << endl;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...