蜘蛛 (DFS)
最编程
2024-04-20 22:10:47
...
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1584
全部状态都判断一遍
代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <ctype.h> 7 #include <iomanip> 8 #include <queue> 9 #include <stdlib.h> 10 using namespace std; 11 12 int ans; 13 int tep[11],vis[11]; 14 15 int abs(int x) 16 { 17 if(x>=0) 18 return x; 19 return -x; 20 } 21 22 void dfs(int cnt,int sum) 23 { 24 if(sum >= ans) 25 return ; 26 if(cnt==9){ 27 ans=sum; 28 return ; 29 } 30 for(int i=1; i<10; i++){ 31 if(!vis[i]){ 32 vis[i]=1; 33 for(int j=i+1; j<=10; j++){ 34 if(!vis[j]){ 35 dfs(cnt+1,sum+abs(tep[i]-tep[j])); 36 break; 37 } 38 } 39 vis[i]=0; 40 } 41 } 42 } 43 44 int main() 45 { 46 int t,x; 47 scanf("%d",&t); 48 while(t--){ 49 for(int i=1; i<=10; i++){ 50 scanf("%d",&x); 51 tep[x]=i; 52 } 53 memset(vis,0,sizeof(vis)); 54 ans=10000000; 55 dfs(0,0); 56 printf("%d\n",ans); 57 } 58 }
上一篇: 团队计划(二)(*团队)
下一篇: Java 课程设计 - 蜘蛛接龙
推荐阅读
-
LeetCode 1490. 克隆 N 树(DFS/BFS)
-
每日 OJ 问题_DFS 回溯剪枝 ①_强制按钮 46.完全对齐(回溯算法介绍)
-
知识产权不是万能药 拥有《蜘蛛侠》等大片的索尼为何要放弃电影?
-
LeetCode] 24 点游戏(回溯 DFS24 点游戏(回溯 DFS)
-
[HDU 1427] 速度计算 24 点(DFS 暴力搜索)
-
与胖胖牛一起进行障碍赛训练(DFS + 州 dp)
-
虚亮SEO如何有效优化百度蜘蛛的权重?
-
[Hadoop2.x] Hadoop 运行一段时间后,stop-dfs 等操作失败 原因及解决方案 - 永久解决方案:修改 $HADOOP_HOME/etc/hadoop/hadoop-env.sh 文件,将 ${HADOOP_PID_DIR} 的 export HADOOP_PID_DIR=${HADOOP_PID_ DIR} 改为你指定的目录,这样 Hadoop 就会将相关的 pid 进程文件保存在指定目录中,避免被 Linux 自动删除。例如 export HADOOP_PID_DIR=/usr/local/hadoop/pids/ 发现问题后的解决方案:
-
xp蜘蛛纸牌
-
为了研究蜘蛛接龙的规则,我玩了 200 局游戏!