随笔分类 - dfs
摘要:题意:给你N 城市和M条道路,每条道路要付的钱,但是如果你在这个道路上你可以付其他道路的钱(跟走到的时候去的话不一样),问你从1走到N最少话费是多少。直接DFS搜。链接http://poj.org/problem?id=3411代码: 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #define loop(s,i,n) for(i = s;i g[15];19 int vis[15];20 int ans;21 int n,m;22 void ...
阅读全文
摘要:题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少链接:http://poj.org/problem?id=1724直接dfs搜一遍就是代码: 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #define loop(s,i,n) for(i = s;i dis)31 ans = dis;32 return ;33 }34 35 if(ans < di...
阅读全文
摘要:题意就是找一个 左右上下对称的正方形矩阵。连接:http://acm.hdu.edu.cn/showproblem.php?pid=4618没想到记忆+dp和暴力就能水过。。。//记忆话搜索+dp#include #include #include #include using namespace std;int a[301][301];char dp[301][301][301];int n,m;int judge(int i,int j,int len){ int k; if(i+len-1 >= n) return 0; if(j+len-1 >= m) return ...
阅读全文
摘要:题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4462当时比赛的时候读错题意了。。。很悲催的没有考虑n*n个稻草人的时候。。。。然后一直WA= =。。。悲剧啊。。。。回来的时候崔老师教了一个枚举的办法。。。就是位运算。。之前做的一道DFS也可以用位运算来写(其实取1或者取0的dfs都可以用位运算写= =)~、膜拜崔老师~View Code #include <iostream>#include <math.h>#include <stdio.h>#include <string.h>#include
阅读全文
摘要:题目连接http://poj.org/problem?id=3083题目大意:一看就是基本的迷宫问题,一个是优先往左走,一个优先往右走,还有一个是求最短。额。。。。好久没写bfs= =...上来bfs没加vis= =。。。。然后dfs又写错,因为是左右思路相同,我是直接复制的,找了好久一直是re= =、你妹妹的。。。用一个全局变量保存朝向,用一个%4来保存结果View Code 1 #include <stdio.h> 2 #include <string.h> 3 char map[100][100]; 4 int m,n,sx,sy,dx,dy; 5 int dir
阅读全文
摘要:题目连接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1035题目大意:给你一个黑白的棋盘,每次翻一个子(翻的子上下左右也自动翻),看最后能否返程全黑或者全白、最少要几次。View Code 1 #include <stdio.h> 2 #include <string.h> 3 int ans = 10000; 4 char map[10][10]; 5 int pan() 6 { 7 int i,j; 8 for(i = 0;i < 4;i++) 9 {1
阅读全文
摘要:题目连接http://poj.org/problem?id=1088这道题其实说实话知道了思路真的很简单,但是那天晚上做的时候很苦逼的错了好多地方然后查还没查出来= =。。。表示DFS真的很吃力= =。。。所谓的记忆化搜索现在看来就是吧上搜索过的给存起来。。。需要一个状态方程dp[i][j] = max(dp[i+1][j],dp[i-1][j],dp[i][j+1],dp[i][j-1]);四个方向搜一遍找到最合适的即可。。。代码View Code 1 #include <string.h> 2 #include <stdio.h> 3 int num[105][10
阅读全文
摘要:不大明白啥叫记忆化搜索,但是PC学长说就相当于打表~不大懂= =。。。回头好好看一下题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1501参考着崔老师的代码来的。View Code 1 #include<stdio.h> 2 #include<string.h> 3 4 char s1[205],s2[205],s3[410]; 5 int len,count,vis[205][205],leap; 6 void dfs(int a,int b,int c) 7 { 8 if(leap) 9 return;10 if(v..
阅读全文
摘要:题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1241题目大意是@是可以走的,让你找出是不是有几块是连载一起的。跟挑战编程上的一道题木很想、View Code 1 #include <stdio.h> 2 #include <string.h> 3 4 char map[110][110]; 5 int vis[110][110]; 6 int to[8][2] = {{0,1},{0,-1},{1,0},{1,1},{1,-1},{-1,0},{-1,1},{-1,-1}}; 7 int n,m,count; 8 9 vo
阅读全文
摘要:题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1015题目的大意就是给以一串字符串和一个目标数字,让你从中找出5个字母,依次可以满足v - w^2 + x^3 - y^4 + z^5 = target这个关系式如果有多个就按字典序最大的输出。。。View Code #include <stdio.h>#include <string.h>#include <stdlib.h>int vis[100005];char str[100005];int sum,leap;char end[10],temp[10];in
阅读全文

浙公网安备 33010602011771号