
搜索
vsooda
这个作者很懒,什么都没留下…
展开
-
HDU 1142 SPFA + DFS记忆搜索 学习了!!
走AB这条路的前提是: B到home的最短路比A到home的最短路要短,求满足这个要求的office到home的路有多少条SPFA:参考http://sgeteternal.iteye.com/blog/1148891spfa比dijkstra极大程度减少不必要的松弛操作。故而比dij快。#include #include #include #include #include原创 2013-01-28 17:04:48 · 650 阅读 · 0 评论 -
HDU 2717 新型bfs方法
#include #include int a[200005]; //队列int b[200005]; //步数int main (){ int n,k,t,i,top; while (scanf("%d %d",&n,&k)!=EOF) { memset(b,0,sizeof(b)); b[n]=0; a[0]=n; top=1; for (i=0;;i++)原创 2012-09-13 10:03:13 · 946 阅读 · 0 评论 -
HDU 2612 两次bfs
求2个点到KFC的距离之和,使其最小,可用2次BFS,分别求出2个点到各个KFC的最短距离,然后找出和最小的即可#include #include #include #define max 0x7fffffff int n,m; char map[250][250]; int visit1[250][250]; int visit2[250][250];原创 2012-09-12 18:59:53 · 505 阅读 · 0 评论 -
HDU 1983 BFS + DFS
思路(大牛的):封锁出口或者入口周围的格子. 最多需要4个封锁点. 所以我们可以采取这样的策略: 1.寻找一条盗贼的可行路线,如果没有,返回0. 2.计算封锁出口和入口四周需要的封锁点数量,取小的一个,假设是k,k 3.从少到多,遍历所有封锁点个数小于k的方案,验证是否是一条有效的覆盖方案(可以通过是否阻止了1中的盗贼线路进行快速验证). 如果有有效覆盖方案,返回这转载 2012-09-12 14:43:32 · 1093 阅读 · 1 评论 -
HDU 1677 与1257类似 排序 + 二分
#include#include#includeusing namespace std;struct Node{ int w,h; bool operator<(Node a) { if(a.w==w) return h>a.h; return w<a.w; }};Node A[20002];int p[20002];int main(){ int T,原创 2012-09-11 16:33:42 · 808 阅读 · 0 评论 -
HDU 1239 思路值得学习
刘春英课件说的很清楚。#include#includeusing namespace std;int prime[1000]={0,2,3};void getprime(int n)//试除法求素数{ int k=2; for(int i=5;i<=n;i+=2) { bool f=true; for(int j=1;pri原创 2012-09-09 18:27:58 · 814 阅读 · 0 评论 -
HDU 1597 数学题
//S = 1121231234.......123456789123456789112345678912.........//对于任意的数 N,//先确定其区间 si - si+1//确定其区间后//再确定 在si中第几个 1234456789 ,//然后再确定第几位数#include #include using namespace std;#define sum(n原创 2012-09-09 22:38:21 · 1212 阅读 · 0 评论 -
HUD 1548 BFS 换了一种实现方式
#include #include #include using namespace std;int main(){ int n, a, b; vector v(1000); vector visited(1000); vector step(1000); vector f(10000); while (true) { cin>>n; if (n == 0)原创 2012-09-09 20:56:55 · 580 阅读 · 0 评论 -
HDU 1372 骑士漫游 bfs
“骑士漫游”的问题大意如下: 在8*8的棋盘方格上,一个骑士可以自由移动(移动方法为走L形,即横着走两格然后竖着走一格,或是竖着走两格然后横着走一格)。骑士可以从棋盘上任一个方格出发,在64(8*8=64)步内是否可以漫游整个棋盘,每个方格都走到并且只走一次。 用一个初始化为0的8*8的二维数组来代表棋盘: 0 1 2 3原创 2012-09-09 20:33:24 · 765 阅读 · 0 评论 -
HDU 2102 双层bfs
这个题目其实也是广搜,只是不同于以往的两层监狱的直接广搜,而是当遇到时空传输机的时候如果条件满足会被送到另一层对应位置。因此,该题还在一维空间上广搜,当遇到时空传输机时跳转到另一层然后接着执行一维空间上的广搜。#include #include using namespace std;typedef struct{ int x; int y; int z; int time;原创 2012-09-12 15:03:48 · 778 阅读 · 0 评论 -
HDU 2553 N皇后,没预处理会超时
这题写了几个版本,第一次写超时,于是用一个数组保存当前路径,这样对于判断是否可以放置有利,但是还是超时。最后发现是之所以会超时,是因为测试的例子非常多,应该使用预处理,如果每次都重新计算会超时,只要把结果保存在数组,然后每次查询即可。代码最终版本:#include using namespace std;const int N = 11;int map[N][N];in原创 2012-09-12 16:33:42 · 3224 阅读 · 2 评论 -
HDU 1428 漫步校园 SPFA + DFS记忆搜索
类似上题。#include #include #define inf 0x7fffffffstruct{ int v, pow, next;}edge[52*52*4];int n = 0, map[52][52], head[52*52], dis[52*52], dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};__int64 c原创 2013-01-29 10:20:08 · 795 阅读 · 0 评论 -
HDU 1051 与1257类似 二维排序,然后贪心
#include #include #include #include using namespace std; typedef struct{ int x, y;} node;bool cmp(node n1, node n2){ if(n1.x != n2.x) return (n1.x < n2.x); else return (n1.y原创 2012-09-21 09:18:46 · 676 阅读 · 0 评论 -
HDU 1044 BFS + DFS
分别用dfs, bfs做了一次dfs超时,bfs超内存题目意思 求解是否能够到达出口 如果能 求解到达时的最大携带价值首先使用广搜搜出包括起点和终点在内 所有特殊点之间的最短距离 建立一个隐式图然后使用DFS枚举各个组合 然后求出最大值 注意DFS的强剪代码思想很好,但是代码风格让人看起来难受,改了半天还是这个样子,下次重写。#include #in原创 2012-09-20 16:12:39 · 1127 阅读 · 0 评论 -
HDU 1015 dfs回溯
有用六重循环的,无敌了。。。以下代码采用回溯法。#include #include #include #include using namespace std;bool visit[30] ,judge;int ans[6] ,N[26] ,total;long long target ,sum;void set(char *a){ int i ,len; for(i原创 2012-09-19 16:53:48 · 2049 阅读 · 0 评论 -
HDU 1560 DNA sequence hh大神代码
#include "stdio.h"#include "string"#define M 1679616+100000struct H{ int st; int len;}q[M];int hash[M],cas =1;int n , end ,len;char str[8][8];char ku[] ="ACGT";int bfs() { int head,tail,i,原创 2012-09-18 15:33:25 · 1407 阅读 · 1 评论 -
HDU 1253 3维bfs 一直wa,原来是小错误。
wa!!!教训 。continue的地方写成了return#include #include using namespace std;int map[51][51][51];int visit[51][51][51];int a, b, c, step;int dir[6][3] = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}原创 2012-09-14 16:47:38 · 864 阅读 · 0 评论 -
HDU 2614 dfs
其实这题是一DFS的简单题,但是题目意思,是在让人蛋痛啊!这题就是解决一个问题后,解决下一个问题的时间一定要比解决这题的多或者等于这题。。。。。但那英文是在太难看;第1个是第0个,时间为0,然后解决下一个。#include#includeint n,des[20],max;int num[20][20];void DFS( int p,int val,int nu原创 2012-09-13 09:44:15 · 697 阅读 · 0 评论 -
HDU 2141 二分查找,实现上简单,思想很重要
用三个for循环会超时后来就改进了一下把函数改为:A+B=X-C,然后二分搜一下就可以了;#include #include using namespace std;const int N = 505;__int64 ab[N * N];int num;int search(__int64 x){ int f = 0, l = num - 1; int mi原创 2012-09-12 15:37:32 · 4929 阅读 · 1 评论 -
HDU 1312 矩阵中找出初始点的可达点数 简单bfs
#include #include using namespace std;const int N = 21;int dir[4][2] = {{0,1}, {0, -1}, {1, 0}, {-1, 0}};char a[N][N];int cnt = 0;int n, m;struct node { int x, y;};void bfs(int x, int y)原创 2012-09-08 20:21:12 · 860 阅读 · 0 评论 -
HDU 2952 与1241类似
#include char a[110][110];int m, n;int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};void ss(int x, int y){ int i, j, xx, yy; a[x][y] = '1'; for(i = 0; i < 4; i++) { xx = x + dir[i][0];原创 2012-09-08 09:20:54 · 490 阅读 · 0 评论 -
HDU 1238 Substring
#include #include #include using namespace std;int main(){ int t, n, i; cin >> t; while(t--) { string s[101]; cin >> n; int len = 1000, j = 0, max, f = 0; for(i = 0; i < n; i++) {原创 2012-09-07 19:26:02 · 595 阅读 · 0 评论 -
HDU 1175 连连看
#include#include#includeusing namespace std;struct ss{ int x,y; int num,di;//di记录上次的方向}en,st;int map[1001][1001];int r[4][2]={{0,1},{1,0},{0,-1},{-1,0}};int mark[1001][1001];int n,m,ex,ey,原创 2012-09-07 10:20:48 · 539 阅读 · 0 评论 -
HDU 1072 BFS
#include #include using namespace std;struct node{ int x, y, time, step;};int map[20][20];int n, m, sx, sy, dx, dy;queue que;node now, next1;int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};i原创 2012-09-07 08:58:55 · 498 阅读 · 0 评论 -
HDU 1180 诡异楼梯 bfs
#include #include using namespace std;const int N = 25;struct node{ int x, y, time;};char a[N][N];int mark[N][N];int x1, y11, x2, y2;int dis[4][2] = {{1,0}, {-1, 0}, {0, 1}, {0, -1}};void原创 2012-09-07 14:31:22 · 943 阅读 · 2 评论 -
HDU 1104 BFS 队列
#include #include using namespace std;const int MAX = 1000000 + 10;char sign[4] = {'+','-','*','%'};int used[MAX];int n,k,m,km;struct Node{ int N; queue sign;};int BFS(){ memset(used原创 2012-09-07 09:39:06 · 799 阅读 · 0 评论 -
HDU 1045 二分匹配
//hdu 1045 Fire Net//这题意思是给出一张图,图中'X'表示wall,'.'表示空地,可以放置blockhouse//同一条直线上只能有一个blockhouse,除非有wall隔开,问在给出的图中//最多能放置多少个blockhouse//二分匹配,别人都说水题,但我没看出它是二分图匹配//看了别人的解题报告后还有点懵懵懂懂的//这题是把原始图分别按行和列缩点转载 2012-09-06 15:32:21 · 4238 阅读 · 0 评论 -
HDU 1043 Eight
#include #include #include using namespace std; #define N 10 #define MAX 362881 //最多可能有9!个排列 int fac[]={1,1,2,6,24,120,720,5040,40320,362880};//康托展开数列 bool visited[MAX]; string route[转载 2012-09-06 14:11:05 · 578 阅读 · 0 评论 -
HDU 1016
#include #include using namespace std;int vis[21],A[21]; //素数表,加快后续判断 int primelist[38] = {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1}; void dfs(int cur, int原创 2012-09-06 09:58:40 · 652 阅读 · 0 评论 -
HDU 1026 迷宫战斗问题 使用了优先队列
#include #include #include #include using namespace std;const int MAXN = 100;struct node{ int x, y; int time; friend bool operator <(node a, node b) //time小的优先级高 { return a.time > b.tim原创 2012-08-23 09:48:02 · 610 阅读 · 0 评论 -
HDU 1195 Open the Lock BFS
#include #include using namespace std;int visit[11][11][11][11];struct node{ int num[4]; int step;};void bfs(node s1, node s2){ node st, ed; queue q; int flag, i; st = s1; st.step = 0原创 2012-09-07 15:40:26 · 501 阅读 · 0 评论 -
HDU 1208 Pascal's Travels
#include using namespace std;const int MAX = 50;int a[MAX][MAX];__int64 sum[MAX][MAX];int main(){ int n; char ch[50]; while(cin >> n, n+1) { for(int i = 0; i < n; i++) { cin >> ch;原创 2012-09-07 16:00:16 · 936 阅读 · 0 评论 -
HDU 1075 用gets可以获取字符串输入的空格回车等。以后用字典树尝试提高效率
#include #include #include #include using namespace std;int main(){ freopen("1075.txt", "r", stdin); string first; string second; cin >> first; map word; while(cin >> first, first != "END原创 2012-09-09 10:12:40 · 690 阅读 · 0 评论 -
HDU 1271 数论,有点巧妙,改天再研究
假设A中去掉的数在第k+1位,可以把A分成三部分,低位,k,和高位。A == a + b * 10^k + c * 10^(k+1)B == a + c * 10^kN == A + B == 2 * a + b * 10^k + c * 10^k * 11其中b是一位数,b * 10^k不会进位,用10^k除N取整就可以得到b转载 2012-09-08 19:52:12 · 576 阅读 · 0 评论 -
HDU 1240 三维bfs
#include #include using namespace std;int map[15][15][15];int flag, a, b, c, d, e, f, n;char str[15][15][15];int dir[6][3] = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};stru原创 2012-09-08 08:49:07 · 567 阅读 · 0 评论 -
HDU 1226 超级密码
#include #include #include using namespace std; int n,c,m; int a[20]; int flag; bool visited[5100]; string ans; struct node{ string ans; int mod; }now; void bfs(){ queue转载 2012-09-07 18:54:16 · 685 阅读 · 0 评论 -
HDU 1258 Sum it up 回溯法 暴力
#include #include using namespace std;int t, n;int a[20];int save[20];int index;int sign;int cmp(const int &a, const int & b){ return a > b;}void dfs(int k , int sum){ if(sum > t) r原创 2012-09-08 19:28:53 · 754 阅读 · 0 评论 -
HDU 1242 简单bfs
#include #include using namespace std;int n, m;const int MAX = 205;char a[MAX][MAX], map[MAX][MAX];int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};struct node{ int x, y, step;};void bfs(原创 2012-09-08 10:25:58 · 549 阅读 · 0 评论 -
HDU 1181 其实26 * 26的矩阵
想到了map, mutimap,其实根本不需要那么麻烦,把问题抽象一下,其实就是26 * 26 然后判断可达性#include using namespace std;int map[26][26];int set[26];char a[10001];int main(){ int la,i,j,k; while(cin>>a) { memset(map,0,size原创 2012-09-09 10:55:41 · 926 阅读 · 0 评论 -
HDU 1241
#include char a[110][110];int m, n;void ss(int x, int y){ int i, j, xx, yy; a[x][y] = '1'; for(i = -1; i < 2; i++) for(j = -1; j < 2; j++) { xx = x + i; yy = y + j; if(xx =m || yy原创 2012-09-08 09:07:06 · 714 阅读 · 0 评论