SRM 576 D2 L2:ArcadeManao,DFS,善于根据实际问题使用最简便的方法
2013-08-11 22:51
387 查看
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12504
这是个典型的图论的题目,很容易就能把题目抽象的图论的描述:求连接两点的所有 路径中权值最大的边 的最小值。但求解起来非常麻烦。如果换一个角度来思考,根据实际题目的要求,只需要计算当梯子长度从 0 到 N-1 时起点和终点是否能够连通,那么最先符合条件的那个长度就是最短的长度。所以使用DFS求解是非常方便的。这道题目给我的启发就是要根据实际的题目描述来找到最适合题目的解,而不是盲目的抽象,泛化。就像刚看的编程珠玑的第一节中的那个排序问题,要善于根据实现的题目描述找到最优解。
代码如下:
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
using namespace std;
/*************** Program Begin **********************/
class ArcadeManao {
private:
bool v[50][50];
int M, N, L;
vector <string> lev;
void DFS(int x, int y)
{
v[x][y] = true;
// 左
if (y-1 >= 0 && 'X' == lev[x][y-1] && !v[x][y-1]) {
DFS(x, y-1);
}
// 右
if (y+1 <= M-1 && 'X' == lev[x][y+1] && !v[x][y+1]) {
DFS(x, y+1);
}
// 竖直方向
for (int i = 0; i < N; i++) {
if (abs(x-i) <= L && 'X' == lev[i][y] && !v[i][y]) {
DFS(i, y);
}
}
}
public:
int shortestLadder(vector <string> level, int coinRow, int coinColumn) {
int res = 0;
N = level.size();
M = level[0].size();
lev = level;
for (int i = 0; i < N; i++) {
memset(v, 0, sizeof(v));
L = i;
DFS(N-1, 0);
if (v[coinRow-1][coinColumn-1]) {
res = i;
break;
}
}
return res;
}
};
/************** Program End ************************/
这是个典型的图论的题目,很容易就能把题目抽象的图论的描述:求连接两点的所有 路径中权值最大的边 的最小值。但求解起来非常麻烦。如果换一个角度来思考,根据实际题目的要求,只需要计算当梯子长度从 0 到 N-1 时起点和终点是否能够连通,那么最先符合条件的那个长度就是最短的长度。所以使用DFS求解是非常方便的。这道题目给我的启发就是要根据实际的题目描述来找到最适合题目的解,而不是盲目的抽象,泛化。就像刚看的编程珠玑的第一节中的那个排序问题,要善于根据实现的题目描述找到最优解。
代码如下:
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
using namespace std;
/*************** Program Begin **********************/
class ArcadeManao {
private:
bool v[50][50];
int M, N, L;
vector <string> lev;
void DFS(int x, int y)
{
v[x][y] = true;
// 左
if (y-1 >= 0 && 'X' == lev[x][y-1] && !v[x][y-1]) {
DFS(x, y-1);
}
// 右
if (y+1 <= M-1 && 'X' == lev[x][y+1] && !v[x][y+1]) {
DFS(x, y+1);
}
// 竖直方向
for (int i = 0; i < N; i++) {
if (abs(x-i) <= L && 'X' == lev[i][y] && !v[i][y]) {
DFS(i, y);
}
}
}
public:
int shortestLadder(vector <string> level, int coinRow, int coinColumn) {
int res = 0;
N = level.size();
M = level[0].size();
lev = level;
for (int i = 0; i < N; i++) {
memset(v, 0, sizeof(v));
L = i;
DFS(N-1, 0);
if (v[coinRow-1][coinColumn-1]) {
res = i;
break;
}
}
return res;
}
};
/************** Program End ************************/
相关文章推荐
- SRM 576 D2 L2:ArcadeManao,DFS,善于根据实际问题使用最简便的方法
- vs中使用M_PI的问题及解决 角度转弧度&根据弧度计算圆周上点的坐标的方法
- Kafka实际使用过程中遇到的一些问题及解决方法
- 声明方法java实际开发中泛型使用需要注意的一些问题
- 使用itext根据freemarks模版生成pdf,表格产生跨页问题解决方法
- 前端页面使用CSS方法处理字符串过长的问题最简便
- 创业日记-使用数学方法解决实际问题
- ORA-14450: 试图访问已经在使用的事务处理临时表,根据网上文章提供的方法,问题是解决了
- [置顶] MFC树形控件CTreeCtrl使用方法、遍历、SetCheck无效、根据名称获取节点、获取选中节点等问题
- Android Studio该如何导入别人的开源库到自己的项目中并使用库中的控件,网上找过方法但是并不能搞定,希望实际问题 实际解决,拯救我这个傻瓜
- android模拟按键问题总结[使用IWindowManager.injectKeyEvent方法](转)
- GitHub版本控制使用(命令行实际操作图解Git使用方法)
- Material Design Button的使用方法以及默认间距问题的适配
- struts2 token 使用方法及问题解决
- 关于物体 '固有类别' 与 '实际使用类别' 分离的情况,结构体定义方法
- ASP.NET Core2.0 使用VSCode 版本1.13.1工具 开发控制台运行出现中文乱码问题及解决方法
- GitHub使用elasticsearch遇到的一些问题及解决方法
- vlc 嵌入到 MFC 使用步骤以及相关问题解决方法
- Apache Hawq-使用中遇到的问题和解决方法
- mysql使用source 命令乱码问题解决方法