2012年4月2日

(转)一种变进制数及其应用(全排列之Hash实现)

摘要: 其实和康托展开差不多我们经常使用的(9php.com)数的(9php.com)进制为“常数进制”,即始终逢p进1。例如,p进制数K可表示为 K = a0*p^0 + a1*p^1 + a2*p^2 + ... + an*p^n (其中0 <= ai <= p-1),它可以表示任何一个自然数。对于这种常数进制表示法,以及各种进制之间的(9php.com)转换大家应该是很熟悉的(9php.com)了,但大家可能很少听说变进制数。这里我要介绍一种特殊的(9php.com)变进制数,它能够被用来实现全排列的(9php.com)Hash函数,并且该Hash函数能够实现完美的(9php.com 阅读全文

posted @ 2012-04-02 17:09 andy_ 阅读(302) 评论(0) 推荐(0)

2012年3月20日

Astar算法

摘要: A Star算法是一智能找最短路径算法(下面简称A算法), 与 Dijkstra算法相比,A算法访问的节点比较少,因此可以缩短搜索时间。他的算法思想是:这里有公式f 最终路径长度f = 起点到该点的已知长度h + 该点到终点的估计长度g。 O表(open): 待处理的节点表。 C表(close): 已处理过的节点表。算法流程:1. 从起点开始,起点的f = 1 + g, 1表示此节点已走过的路径是1,g是此节点到终点的估计距离, 放入链表O中。可以假设g值的计算使用勾股定理公式来计算此点到终点的直线距离。2. 当O不为空时,从中取出一个最小f值的节点x。3.如果x等于终点,找到路径,算法结束. 阅读全文

posted @ 2012-03-20 23:01 andy_ 阅读(3176) 评论(3) 推荐(0)

康托展开

摘要: 来自 http://archive.cnblogs.com/a/2026276/在上一篇的方法一里,我们使用把数组的下标每次增加1的方法得到重复的全排列,然后再挑出不重复的全排列。如下图所示,绿颜色表示想要得到的结果。0 0 00 0 10 0 20 1 00 1 10 1 20 2 00 2 10 2 21 0 01 0 11 0 21 1 01 1 11 1 21 2 01 2 11 2 22 0 02 0 12 0 22 1 02 1 12 1 22 2 02 2 12 2 2 这种方法虽然简单,但是效率比较差。要生成n个元素的全排列需要遍历 nn 次才能得到 n! 个解(看上面那个绿. 阅读全文

posted @ 2012-03-20 22:01 andy_ 阅读(280) 评论(0) 推荐(0)

2012年3月19日

POJ3669 Meteor Shower BFS

摘要: #include<iostream> #include<queue> using namespace std; #define MAXN 302 struct point{ int x,y; }; int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; int map[MAXN][MAXN]; bool visted[MAXN][MAXN]; int steps; void bfs() { queue<point> Q; point p1={0,0},p2; int i; ... 阅读全文

posted @ 2012-03-19 23:58 andy_ 阅读(224) 评论(0) 推荐(0)

2012年3月6日

poj3670 eating together 简单的dp

摘要: 方法一:LIS/LDS#include"stdio.h" int lis[30001],lds[30001]; int main() { int n,t,len1=0,len2=0,i,left,right,mid; lds[0]=0x7fffffff; lis[0]=-1; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&t); if(t>=lis[le... 阅读全文

posted @ 2012-03-06 17:02 andy_ 阅读(226) 评论(0) 推荐(0)

2012年2月28日

POJ 1328 Radar Installation 贪心

摘要: #include"stdio.h" #include"math.h" #include"stdlib.h" typedef struct { double x1,x2; }POINT; int cmp(const void *a,const void *b) { return (*(POINT *)a).x1>(*(POINT *)b).x1?1:-1; } POINT t[1000]; main() { int n,d,ans,i=0,j,judge,tag; double tx,ty; while(scanf... 阅读全文

posted @ 2012-02-28 20:58 andy_ 阅读(174) 评论(0) 推荐(0)

HDOJ 1039 Easier Done Than Said? 简单的字符串处理

摘要: #include"stdio.h" #include"string.h" int vo(char *psw,int i) {return (psw[i]=='a'||psw[i]=='e'||psw[i]=='i'||psw[i]=='o'||psw[i]=='u');} int main() { char psw[21],ans[2][15]={{"not acceptable"},{"acceptable"}}; while(get 阅读全文

posted @ 2012-02-28 00:55 andy_ 阅读(205) 评论(0) 推荐(0)

HDOJ 1052 Tian Ji -- The Horse Racing 贪心

摘要: 田忌要以最小的代价赢和输对田忌和齐王的马从小到大各自排序,然后以田忌的马从两边对齐王的扫描,如果小的能赢,就赢,如果不能赢,就比两方最大的马,如果能赢则赢,如果也不能赢就用最小的和齐王最大的比掉,注意这里不管是否存在平局都保证了最优选择;#include"stdio.h" #include"algorithm" using namespace std; #define F(i,n) for((i)=0;(i)<(n);i++) int t[1000],q[1000],n,i,ans,ht,tt,hq,tq; main() { while(scanf 阅读全文

posted @ 2012-02-28 00:48 andy_ 阅读(245) 评论(0) 推荐(0)

HDOJ 2602 Bone Collector 01背包问题

摘要: #include"stdio.h" #include"string.h" #include"stdlib.h" #define MAX 1010 int f[MAX],value[MAX],cost[MAX]; main() { int i,v,n,t,j; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&v); memset(f,0,sizeof(f)); ... 阅读全文

posted @ 2012-02-28 00:34 andy_ 阅读(202) 评论(0) 推荐(0)

HDOJ 2099 水

摘要: 学到一种输出策略。#include"stdio.h" main() { int a,b,i,f; while(scanf("%d%d",&a,&b)!=EOF&&(a||b)) { a*=100,f=0; for(i=0;i<100;i++) if((a+i)%b==0) printf(f++?" %02d":"%02d",i); putchar('\n... 阅读全文

posted @ 2012-02-28 00:33 andy_ 阅读(116) 评论(0) 推荐(0)

导航