POJ 2907
来源:互联网 发布:奥卡姆剃刀知乎 编辑:程序博客网 时间:2024/06/06 18:35
题意:平面上给你n个点。1为起始点,问你从1号点出发经过其他所有点再回到1号点的最短距离,两点之间的距离是曼哈顿距离(abs(x1-x2)+abs(y1-y2))。
思路:DFS,算出每次的距离,取最短即可。
#include<cmath>#include<cstdio>#include<string>#include<cstring>#include<algorithm>#define INF 0x7fffffffusing namespace std;typedef struct POS{ int x, y; int dist(const POS &A){ return abs(x-A.x) + abs(y-A.y); }}Node;Node node[11], start;int vis[11], ans, N;void dfs(int dep, int len, Node tmp){ if(dep == N){ ans = min(ans, len+tmp.dist(start)); return ; } for(int i = 0;i < N;i ++){ if(!vis[i]){ vis[i] = 1; dfs(dep+1, len+tmp.dist(node[i]), node[i]); vis[i] = 0; } }}int main(){ int t, n, m; /* freopen("in.c", "r", stdin); */ scanf("%d", &t); while(t--){ scanf("%d%d", &n, &m); scanf("%d%d", &start.x, &start.y); scanf("%d", &N); for(int i = 0;i < N;i ++) scanf("%d%d", &node[i].x, &node[i].y); ans = INF; dfs(0, 0, start); printf("The shortest path has length %d\n", ans); } return 0;}
0 0
- POJ 2907
- poj 2907
- POJ 2907 Collecting Beepers
- poj 2907 dfs
- poj 2907 dfs题
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- OSAL机制的ZigBee低功耗管理(CC2530)
- POJ 3070
- POJ 3233
- POJ 3842
- JAVA 大数运算
- POJ 2907
- POJ 3628 Bookshelf 2
- Cocos2d-x 3.0多线程异步资源加载代码
- Codeforces 242(DIV 2) A题
- Codeforces 242(DIV 2) B题
- Codeforces 242(DIV 2) C题
- HDU 1711 Number Sequence
- POJ 2387
- Z-STACK网络建立过程