05 2012 档案

UVa 10181 - 15-Puzzle Problem
摘要:题目链接不好找:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1122用的 IDA*,跑了6.536s,时限是45s(刚看了627个人通过,我是第515名……,排行榜比较恐怖啊,见下图)这道题的逆序剪枝和八数码不同,具体参考这篇文章:http://mathworld.wolfram.com/15Puzzle.html1A! 1 # include <stdio.h> 2 # include <math.h> 阅读全文

posted @ 2012-05-27 13:09 getgoing 阅读(1772) 评论(0) 推荐(0)

ZOJ 1217 eight
摘要:八数码,双广、A*都超时了(可能是写得不好),IDA*通过了;在POJ上是16MS(和双广一样,A* 60MS左右),HDOJ上是800MS左右(双广是500MS),ZOJ上只有IDA*没超时(2480MS)。 1 # include <stdio.h> 2 # include <math.h> 3 # include <string.h> 4 5 # define MIN(x, y) ((x)<(y) ? (x):(y)) 6 7 # define N 9 8 # define DIR_N 4 9 # define INF 0x7fffffff 10 阅读全文

posted @ 2012-05-27 01:07 getgoing 阅读(1255) 评论(0) 推荐(0)

八数码(IDA*)
摘要:IDA*+曼哈顿 相当快! 通过五组数据总计用时 0.015000 s.把宏定义中的N和SIZE改一下,就可以处理十五数码问题了,终于修炼到第八层了;刚开始还是使用结构体定义了状态,写得相当麻烦,后来发现IDA*中不像BFS和A*,只需要考虑一条路,,参考了lym(http://www.cnblogs.com/liyongmou/archive/2010/07/19/1780861.html)的(后面调试的受不了了,对照着此大牛的代码一点点改,最后发现是更新状态时,npos写成了pos……)# include <stdio.h># include <math.h># i 阅读全文

posted @ 2012-05-27 00:51 getgoing 阅读(2029) 评论(0) 推荐(0)

八数码(A*)
摘要:第一道A*,照着A*的框架写的,不是很理解A*,写的也很长,中间有些标记的不太理解,去掉标记在POJ提交通过了,还是没理解A*的思想;犯了个低级错误:在判断新状态 0 的位置(nx, ny)是否越界时,应该是0 =< nx < 3,结果写成了0<= nx <=3,还叫LJ大牛一块查,最后终于给发现了;在写 A* 之前以为 A* 会省不少空间(扩展的节点相比 BFS 少很多),实际上却开了两个大数组: f[] 和 g[],一下子感觉也挺浪费的,时间上感觉快了很多,我测试的几组都是0MS,在POJ上提交是64MS;两周以前就决心花几天时间搞八数码和十五数码(主要是想学学A* 阅读全文

posted @ 2012-05-24 23:49 getgoing 阅读(1316) 评论(0) 推荐(0)

COJ 1080 A simple maze
摘要:地图很小,50×50,所以普通的BFS就行了;1WA:地图用%s读取时,读入的是字符0,在判断是否有路径时和数值0比较。 1 # include <stdio.h> 2 # include <string.h> 3 4 typedef struct {short x, y;}queue; 5 6 const short dir[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}}; 7 8 int r, c, sr, sc, er, ec; 9 char m[55][55], v[55][55], d[55][55];10 queue 阅读全文

posted @ 2012-05-23 17:17 getgoing 阅读(419) 评论(0) 推荐(0)

HDOJ 1043 eight
摘要:实际上和POJ 1077一样,但是这里有多组输入,并且有一组输入为12345678x(也就是不需要移动就达到了目标状态),这时还要输出一个空行。 1 # include <stdio.h> 2 # include <string.h> 3 4 # define N 9 5 # define MAXN (362880 + 10) 6 7 typedef struct{ int k; char d; } foot; 8 typedef struct{ char a[N]; } state; 9 10 const char md[4] = {'u', ' 阅读全文

posted @ 2012-05-21 13:50 getgoing 阅读(564) 评论(0) 推荐(0)

POJ 1077 eight
摘要:双广,16MS,相当快。 1 # include <stdio.h> 2 # include <string.h> 3 4 # define MAXN (362880 + 5) 5 6 typedef struct 7 { 8 char a[9]; 9 }state; 10 11 typedef struct 12 { 13 int k; 14 char d; 15 }path; 16 17 path p[MAXN]; 18 const char md[4] = {'u','l','r','d'}; 19 阅读全文

posted @ 2012-05-20 21:33 getgoing 阅读(435) 评论(0) 推荐(0)

八数码(双向广搜)
摘要:早上看了提到双向广搜的一篇文章,其中讲了双向广搜可以节约一半的时间和一半的空间(理论上),我画了一幅图:(上面的对应普通BFS,下面的对应双向广搜)可以看出简单BFS的搜索节点大约是双向广搜的二倍。对于八数码问题,由于逆序剪枝可以将所有无解的状态全部剪掉,剩余的都是有解的状态,所以使用双向广搜速度可能会更快;对下面两组数据(分别输入)1 2 3 4 5 6 7 8 08 7 6 5 4 3 2 1 02 6 4 1 3 7 0 5 88 1 5 7 3 6 4 0 2正确输出对应是 30、 31使用BFS的运行时间:0.390s 0.359s使用双广的运行时间:0.109s 0.046s双广我 阅读全文

posted @ 2012-05-20 18:16 getgoing 阅读(5063) 评论(0) 推荐(1)

八数码(BFS)
摘要:参考白书的方法,将每一个状态映射到一个9位整数,然后再映射到它在所有状态中的大小位置(编码)来减少内存使用;按照 POJ eight这道题的输入形式写的(对空格的处理参考了Staigner大牛的做法:用scanf一个字符串来读取);输入包括初始状态和目标状态,输出为最短距离,如果无解,输出 -1;逆序的剪枝比较难理解:当前状态逆序数+已走步数与目标态逆序数同奇偶则有解,有解状态不会扩展出无解状态,反之也成立,初始状态的逆序数+初始态与目标态之间空格的Manhattan距离与目标态同奇偶择有解,反之无解;LJ 大牛提出了另一种剪枝的方法:将目标态中任意非零数互换位置得到的状态是无解状态可以达到的 阅读全文

posted @ 2012-05-15 22:39 getgoing 阅读(1179) 评论(0) 推荐(0)

POJ 2406 Power Strings
摘要:白书上看过这道题,枚举即可,530MS左右,这道题分类是 KMP ,可能是用 next 数组。# include <stdio.h># include <string.h>char s[1000005];int check(int i, int t);int solve(int len);int main(){ int len; while (1) { scanf("%s", s); len = strlen(s); if (len == 1 && s[0] == '.') break; else... 阅读全文

posted @ 2012-05-11 11:13 getgoing 阅读(184) 评论(0) 推荐(0)

字符串模式匹配:POJ 3461 Oulipo
摘要:这道题是字符串的模式匹配,要求计算出模式串在文本串中出现的次数,比如:"AZA" 在 "AZAZAZA" 中出现了 3 次;这道题使用 KMP 过的,但是 horspool 却不能过,尝试了一下各种方法后,还是回到了麻烦的 KMP,留下了以下几个模版:1. shift-or,据说比 KMP 快两倍,但只适用于模式串在 32 位以内,64位以内的也可以,需要把 b[] 改为 long long int 型,超出范围的可以尝试自己构造长类型……这道题不适用# include <string.h>int b[128]; /* int型共32位,模式 阅读全文

posted @ 2012-05-11 10:43 getgoing 阅读(559) 评论(0) 推荐(0)

POJ 1401 Factorial
摘要:求 n! 末尾零的个数,在编程之美上看过,其实也不难:求因子2和5的个数取最小值,因为5出现的频率远低于2。# include <stdio.h>int solve(int n);int main(){ int T, n; scanf("%d", &T); while (T--) { scanf("%d", &n); printf("%d\n", solve(n)); } return 0;}int solve(int n){ int t; t = 0; while (n>0) {n /=... 阅读全文

posted @ 2012-05-06 18:25 getgoing 阅读(172) 评论(0) 推荐(0)

POJ 2407 Relatives
摘要:欧拉函数的应用,素数定理;看了DISCUSS中那段超短的代码后才明白欧拉函数直接解决,我之前想的是集合中的容斥定理,实际上这里欧拉公式也实质上就是容斥定理;另外用了刚学的优化的筛法计算素数表。# include <stdio.h># define N 1000005int m;char pt[N];int p[N/10];void build_ptable(void);int solve(int n);int main(){ int n; build_ptable(); while (~scanf("%d", &n) && n) prin 阅读全文

posted @ 2012-05-05 14:48 getgoing 阅读(251) 评论(0) 推荐(0)

[TLE] POJ 1730 Perfect Pth Powers
摘要:给出一个32位整型数 n,形如 n = x^p 的最大的 p;质因数分解;TLE,DISCUSS 中有的用枚举做的(要考虑浮点数精度问题),但是质因数分解应该也可以啊,为什么老是TLE?求大牛指点。# include <stdio.h># include <math.h>int gcd(int a, int b){return !b ? a:gcd(b, a%b);}int m;char t[60005];int p[10005];void solve(long long int n);void build_ptable(void);int main(){ long l 阅读全文

posted @ 2012-05-02 22:02 getgoing 阅读(257) 评论(0) 推荐(0)

POJ 2262 Goldbach's Conjecture
摘要:在 1000000 以内验证哥德巴赫猜想:筛法求素数;为了减少空间,可能要用素数定理,但这道题空间足够了;1WA,太着急了没注意到输入为 0 时结束。# include <stdio.h># define MAXN 1000000int m;int ptable[MAXN+1], p[MAXN/4];void build_ptable(void);void solve(int n);int main(){ int n; build_ptable(); while (~scanf("%d", &n) && n) solve(n); r... 阅读全文

posted @ 2012-05-02 15:29 getgoing 阅读(292) 评论(0) 推荐(0)

POJ 2551 Ones
摘要:题目描述:Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1's. How many digits are in the smallest such a multiple of n?a multiple of n 的意思是某一个数与 n 的积(吧?)开始我愣是没读懂题,一搜都说是水题,没看别人的题解,自己想了两种方法:1. 试因子法:每次选取一个0-9的数字使 阅读全文

posted @ 2012-05-02 13:04 getgoing 阅读(420) 评论(0) 推荐(0)

导航