POJ 3126-Prime Path-广度优先搜索bfs

Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15440 Accepted: 8707

Description

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.

Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on… Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.

1033
1733
3733
3739
3779
8779
8179

The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

 

Input

One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

Output

One line for each case, either with a number stating the minimal cost or containing the word Impossible.

继续阅读POJ 3126-Prime Path-广度优先搜索bfs

POJ1426-Find The Multiple-深度优先搜索BFS

Find The Multiple
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 24137 Accepted: 9997 Special Judge

Description

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.

Input

The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.

Output

For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

继续阅读POJ1426-Find The Multiple-深度优先搜索BFS

poj1207-The 3n + 1 problem

 

POJ-2488 A Knights Journey-深度优先搜索DFS

 

POJ 1321-棋盘问题-简单搜索DFS

 

1048. 数字加密(20)-PAT乙级真题

本题要求实现一种数字加密方法。首先固定一个加密用正整数A,对任一正整数B,将其每1位数字与A的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对13取余——这里用J代表10、Q代表11、K代表12;对偶数位,用B的数字减去A的数字,若结果为负数,则再加10。这里令个位为第1位。
输入格式:
输入在一行中依次给出A和B,均为不超过100位的正整数,其间以空格分隔。
输出格式:
在一行中输出加密后的结果。
输入样例:
1234567 368782971
输出样例:
3695Q8118

分析:首先将a和b倒置,将字符串a和b中较短的那个末尾添加0直到两个字符串长度相等,然后从0开始依次处理每一位,如果当前位是奇数位(i % 2 == 0)则将a[i]的数字加上b[i]的数字再对13取余,结果添加在字符串c的末尾;如果是偶数位,计算b[i]和a[i]的差值,如果小于0就加10,然后将结果添加在字符串c的末尾,最后倒序输出字符串c~