hdoj1078_FatMouse and Cheese(dp)
来源:互联网 发布:淘宝号查询信誉网址 编辑:程序博客网 时间:2024/06/05 03:23
思路:记忆dp
#include<iostream>#include<string.h>#include<algorithm>using namespace std;bool used[500][500];int chess[500][500];long long table[500][500];long long dp(int x, int y, int n, int k){ used[x][y] = true; long long tmpmax = -1; for (int i = 1; i <= k; i++) { if (x + i < n) { if (chess[x][y] > chess[x+i][y])//只更新chess比当前值小的可达值 { if (!used[x + i][y]) table[x + i][y] = dp(x + i, y, n, k); tmpmax = max(tmpmax, table[x + i][y]); } } if (x - i >= 0) { if (chess[x][y] > chess[x-i][y]) { if (!used[x - i][y]) table[x - i][y] = dp(x - i, y, n, k); tmpmax = max(tmpmax, table[x - i][y]); } } if (y + i < n) { if (chess[x][y] > chess[x][y + i]) { if (!used[x][y + i]) table[x][y + i] = dp(x, y + i, n, k); tmpmax = max(tmpmax, table[x][y + i]); } } if (y - i >= 0) { if (chess[x][y] > chess[x][y - i]) { if (!used[x][y - i]) table[x][y - i] = dp(x, y - i, n, k); tmpmax = max(tmpmax, table[x][y - i]); } } } if (tmpmax == -1)//如果当前点相对于起始点(0,0)不可达,则值为-2 table[x][y] = -2; else table[x][y] = tmpmax + chess[x][y]; return table[x][y];}int main(){ int n, k; while (cin >> n >> k&&n != -1) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { cin >> chess[i][j]; } memset(table, 0, sizeof(table)); memset(used, false, sizeof(used)); used[0][0] = true; table[0][0] = chess[0][0]; long long ans = table[0][0]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (!used[i][j]) { table[i][j] = dp(i, j, n, k); } ans = max(ans, table[i][j]); } cout << ans << endl; } return 0;}
0 0
- hdoj1078_FatMouse and Cheese(dp)
- hdu FatMouse and Cheese(dp)
- HDU1078 FatMouse and Cheese(DFS+DP)
- HDU1078 FatMouse and Cheese(DP&非DP)
- hdu FatMouse and Cheese(dp)
- 回溯 dp FatMouse and Cheese
- hdu(1078) FatMouse and Cheese (记忆化搜索+dp)
- HDU 1078 FatMouse and Cheese(简单DP)
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
- HDU FatMouse and Cheese (记忆化搜索+dp思想)
- FatMouse and Cheese (DFS + DP 记忆化搜索)
- HDOJ 1078 FatMouse and Cheese (DFS+DP)
- HDU 1078 FatMouse and Cheese (记忆化dp)
- HDU1078:FatMouse and Cheese(记忆化 dp+搜索) (P)
- HDU1078 FatMouse and Cheese DP(记忆化搜索)
- hdu 1078 FatMouse and Cheese (深度优先搜索+dp)
- hdu 1078 FatMouse and Cheese (搜索+dp)
- hdu 1078(dfs+dp)FatMouse and Cheese
- 关于版本管理的一些初步理解
- 进程与PCB的关系如何看???
- POJ 1503 && HDU 1047 Integer Inquiry(高精度)
- AngularJS从构建项目开始
- tar命令的详细解释
- hdoj1078_FatMouse and Cheese(dp)
- android用pull解析xml文件
- POJ 1504 Adding Reversed Numbers(水~)
- STM32 SPI总线移植fatfs产生硬件异常问题
- Mac下Charles手机抓包与截包篡改报文
- 安卓学习笔记之通知栏的使用
- processing ArrayList demo
- A股端午节节后或报复性大涨
- java面试中注意的问题01