摘要: http://poj.org/problem?id=2488题意:给定一个row*col 的棋盘,列上用字母A,B,C······ 表示,行上用数字1,2,3······表示,问马是否能走遍整个棋盘,并将走的路径按字典序顺序输出。思路:只要按照dir[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};这个方向搜索,得出的路径即按字典序输出的。 1 #include 2 #include 3 ch 阅读全文
posted @ 2013-08-30 00:10 N_ll 阅读(156) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=3414题意:给出1号杯子的容量a,2号杯子的容量b,目标盛水量c。问两个杯子经过怎样的变换能盛出c容量的水,输出最小步数及转换过程,如不能转换,输出“‘impossible”。思路:总共有6种转换,bfs搜索每一种转换,并记录前一个状态。最后将记录的状态倒着输出。 1 #include 2 #include 3 #include 4 #include 5 #include 6 const int N=112; 7 using namespace std; 8 string oper[7] = {"","FILL 阅读全文
posted @ 2013-08-29 00:43 N_ll 阅读(193) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=3126题意:给两个四位数n,m,将n变成m需要多少步,要求每次只能改变n的某一位数,即改变后的数与改变前的数只有一位不同,且每次改变后的数都是素数。思路:bfs+枚举每一位+素数筛选。 1 #include 2 #include 3 #include 4 using namespace std; 5 const int N=10000; 6 int prime[N],vis[N],b[4]; 7 void is_prime() 8 { 9 memset(prime,0,sizeof(prime));10 for (in... 阅读全文
posted @ 2013-08-28 11:17 N_ll 阅读(190) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=2251题意:输出从S->E的最少时间,即最少步数。BFS搜索六个方向。 1 #include 2 #include 3 #include 4 using namespace std; 5 int e_x,e_y,e_z; 6 int n,row,col,vis[32][32][32]; 7 char map[32][32][32]; 8 int dir[6][3] = {{0,0,1},{0,0,-1},{0,-1,0},{0,1,0},{1,0,0},{-1,0,0}}; 9 struct node10 {11 int x;... 阅读全文
posted @ 2013-08-27 21:56 N_ll 阅读(222) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=3087题意:每组3个串,前两个串长度为n,第三个串长度为2*n,依次从第二个串(s2)中取一个字符,从第一个串(s1)中取一个字符,......,直至取完,如果组成的新串(s)和第三个字符串相同则输出组数和匹配成功的次数,如果不相同,则将s串的前n个字符作为s1,后n个字符作为s2,接着匹配,如果永远匹配不成s,则输出组数和-1。思路:简单的字符串模拟,关键是判断输出-1的情况,如果一直匹配与 s不同,但与原来的strcat(s1,s2)相同,则证明无法匹配成功,因为此时字符串已经匹配了一个周期。 1 #include 2 #includ. 阅读全文
posted @ 2013-08-27 19:55 N_ll 阅读(309) 评论(0) 推荐(0)
摘要: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1634将一个数表示成素数相乘的形式,其中素数从小到大排列。 1 #include 2 #include 3 #include 4 const int N=65536; 5 int a[N]; 6 void is_prime() 7 { 8 a[1] = 0,a[2] = 1; 9 for (int i = 3; i x)38 break;39 while(a[i])40 ... 阅读全文
posted @ 2013-08-24 14:49 N_ll 阅读(191) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=3349题意:给出n组数据,每组数据有六个数,这n组数据中若有两组数据不管是从某个数顺时针读还是逆时针读都相同,输出“Twin snowflakes found.”,否则,输出"No two snowflakes are alike."思路:将每组数据求和对大素数取余,将sum相同的放在同一个邻接表中,然后从邻接表中查找符合条件的数据。 1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 7 const int p=99983; 8 co 阅读全文
posted @ 2013-08-23 20:31 N_ll 阅读(293) 评论(0) 推荐(0)
摘要: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1468简单的素数筛选会超时,因为是多组,所以先打表会更快,这样就不用每次都筛选了。 1 #include 2 #include 3 #include 4 const int N=1000002; 5 long long a[N]; 6 void printf_prime( ) 7 { 8 a[1] = 0; 9 a[2] = 1;10 for (int i = 3; i < N; i ++)11 {12 ... 阅读全文
posted @ 2013-08-23 17:22 N_ll 阅读(163) 评论(0) 推荐(0)
摘要: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2497题意:给定一些点和边的关系,判断S点是否在所构成无向图的所有环里。思路:用并查集将所有(除去S及与 S有关的点)有关系的点放在一个集合里,若此时图中还存在环,那么一定不包含S。 1 #include 2 #include 3 const int maxn = 10002; 4 5 int f[maxn],n; 6 int find(int x) 7 { 8 if (x!=f[x]) 9 f[x] = find(f[x... 阅读全文
posted @ 2013-08-23 15:36 N_ll 阅读(226) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=1840题意:给出系数a1,a2,a3,a4,a5,求满足方程的解有多少组。思路:有a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 可得 -(a1x13+ a2x23) = a3x33+ a4x43+ a5x53;先枚举x1,x2,用hash[]记录 sum出现的次数,然后枚举后三个点,若左边出现的sum在右边可以找到,那么hash[sum]即为解的个数。 1 #include 2 #include 3 #include 4 #define N 25000000 5 using namespace std; 6 ... 阅读全文
posted @ 2013-08-22 15:08 N_ll 阅读(237) 评论(0) 推荐(0)