【搜索】优雅的暴力合集

 
 
B - Lucky Numbers (easy)
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 477444 are lucky and 517467 are not.

Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 477744,474477 are super lucky and 4744467 are not.

One day Petya came across a positive integer n. Help him to find the least super lucky number which is not less than n.

Input

The only line contains a positive integer n (1 ≤ n ≤ 109). This number doesn't have leading zeroes.

Output

Output the least super lucky number that is more than or equal to n.

Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.

Sample Input

Input
4500
Output
4747
Input
47
Output
47


#include
#include
#include
#include
using namespace std;

int a[10000];
int len,deep;
long long n,mini,minn=100000000000000;

void dfs(int n4,int n7,long long x)
{
        if (n4==n7) 
                if (x>=n && x-n<</span>minn)
                {
                        minn=x-n;
                        mini=x;
                }
        if (n4+n7>deep) return;
        dfs(n4+1,n7,x*10+4);
        dfs(n4,n7+1,x*10+7);
}

int main()
{
        scanf("%I64d",&n);
        long long x=n;
        while (x>0) { deep++; x=x/10; }
        if (deep % 2==1 ) deep++;
        else deep+=2;
    dfs(0,0,0);
        printf("%I64d\n",mini);
        return 0;
}


 
 
A - Dice
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a  1.a  2,a  3,a  4,a  5,a  6  to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b  1.b  2,b  3,b  4,b  5,b  6  to be numbers on specific faces of dice B. It’s guaranteed that all numbers written on dices are integers no smaller than 1 and no more than 6 while a  i  ≠ a  j  and b  i  ≠ b  j  for all i ≠ j. Specially, sum of numbers on opposite faces may not be 7. 

At the beginning, the two dices may face different(which means there exist some i, a  i  ≠ b  i). Ddy wants to make the two dices look the same from all directions(which means for all i, a  i  = b  i) only by the following four rotation operations.(Please read the picture for more information) 

【搜索】优雅的暴力合集

Now Ddy wants to calculate the minimal steps that he has to take to achieve his goal. 
 

Input

There are multiple test cases. Please process till EOF. 

For each case, the first line consists of six integers a  1,a  2,a  3,a  4,a  5,a  6, representing the numbers on dice A. 

The second line consists of six integers b  1,b  2,b  3,b  4,b  5,b  6, representing the numbers on dice B.
 

Output

For each test case, print a line with a number representing the answer. If there’s no way to make two dices exactly the same, output -1.
 

Sample Input

 
       
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 5 6 4 3 1 2 3 4 5 6 1 4 2 5 3 6
 

Sample Output

 
       
0 3 -1
 


 
 
#include
#include
#include
#include
using namespace std;

int b[7];
int a[7];
int c[7];
int q[10000][8];
int head, tail;

bool f1()
{
                for (int i=1; i<=6; i++) a[i]=q[head][i];
                        c[5]=a[5];
                        c[6]=a[6];
                        c[1]=a[4];
                        c[2]=a[3];
                        c[3]=a[1];
                        c[4]=a[2];

                        for (int j=0; j<</span>tail; j++)
                        {
                                int i;
                        for (i=1; i<=6; i++) 
                                if (c[i]!=q[j][i]) break;
                        if (i==7) return 0;
                        }

                for (int i=1; i<=6; i++) q[tail][i]=c[i];
                q[tail][7]=q[head][7]+1;
       
                int i;
                for (i=1; i<=6; i++) 
                                if (c[i]!=b[i]) break;
                if (i==7) 
                {
                        printf("%d\n",q[tail][7]);
                        return 1;
                }
                tail++;
                return 0;
}
bool f2()
{
                for (int i=1; i<=6; i++) a[i]=q[head][i];
                        c[5]=a[5];
                        c[6]=a[6];
                        c[1]=a[3];
                        c[2]=a[4];
                        c[3]=a[2];
                        c[4]=a[1];

                        for (int j=0; j<</span>tail; j++)
                        {
                                int i;
                        for (i=1; i<=6; i++) 
                                if (c[i]!=q[j][i]) break;
                        if (i==7) return 0;
                        }

                for (int i=1; i<=6; i++) q[tail][i]=c[i];
                q[tail][7]=q[head][7]+1;
       
                int i;
                for (i=1; i<=6; i++) 
                                if (c[i]!=b[i]) break;
                if (i==7) 
                {
                        printf("%d\n",q[tail][7]);
                        return 1;
                }
                tail++;
                return 0;
}

bool f3()
{
                for (int i=1; i<=6; i++) a[i]=q[head][i];
                        c[3]=a[3];
                        c[4]=a[4];
                        c[1]=a[6];
                        c[2]=a[5];
                        c[5]=a[1];
                        c[6]=a[2];

                        for (int j=0; j<</span>tail; j++)
                        {
                                int i;
                        for (i=1; i<=6; i++) 
                                if (c[i]!=q[j][i]) break;
                        if (i==7) return 0;
                        }

                for (int i=1; i<=6; i++) q[tail][i]=c[i];
                q[tail][7]=q[head][7]+1;
       
                int i;
                for (i=1; i<=6; i++) 
                                if (c[i]!=b[i]) break;
                if (i==7) 
                {
                        printf("%d\n",q[tail][7]);
                        return 1;
                }
                tail++;
                return 0;
}

bool f4()
{
                for (int i=1; i<=6; i++) a[i]=q[head][i];
                        c[3]=a[3];
                        c[4]=a[4];
                        c[1]=a[5];
                        c[2]=a[6];
                        c[5]=a[2];
                        c[6]=a[1];

                        for (int j=0; j<</span>tail; j++)
                        {
                                int i;
                        for (i=1; i<=6; i++) 
                                if (c[i]!=q[j][i]) break;
                        if (i==7) return 0;
                        }

                for (int i=1; i<=6; i++) q[tail][i]=c[i];
                q[tail][7]=q[head][7]+1;
       
                int i;
                for (i=1; i<=6; i++) 
                                if (c[i]!=b[i]) break;
                if (i==7) 
                {
                        printf("%d\n",q[tail][7]);
                        return 1;
                }
                tail++;
                return 0;
}

int main()
{
        int n;
        while (scanf("%d",&n)!=EOF)
        {
                q[0][1]=n;
                for (int i=2; i<</span>7; i++) scanf("%d",&q[0][i]);
                for (int i=1; i<</span>7; i++) scanf("%d",&b[i]); q[0][7]=0; int i; for (i=1; i<=6; i++) if (q[0][i]!=b[i]) break; if (i==7) { printf("0\n"); continue; } head=0; tail=1; while (head!=tail) { if (f1()) break; if (f2()) break; if (f3()) break; if (f4()) break; head++; } if (head==tail) printf("-1\n"); } }

 
 
E - Catch That Cow
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points - 1 or + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers:  N  and  K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

 
 
#include
#include
#include
#include
using namespace std;

const int  maxn=1000000;
int q[maxn+100][2];
bool mark[1100000];

int main()
{
        int n,k;
        scanf("%d%d",&n,&k);
        if (n==k) { printf("0\n"); return 0; }
        int head=0;
        q[0][0]=n;
        q[0][1]=0;
        mark[n]=1;
    int tail=1;
        while (head!=tail)
        {
                int x=q[head][0];
            if (x+1<</span>1001000 && !mark[x+1])
                {
                        q[tail][0]=x+1;
                        q[tail][1]=q[head][1]+1;
                        if (x+1==k) 
                        {
                                printf("%d\n",q[tail][1]);
                                return 0;
                        }
                        mark[x+1]=1;
                        tail=(tail+1)%maxn;
                }
                 if (x-1>=0 && !mark[x-1])
                {
                        q[tail][0]=x-1;
                        q[tail][1]=q[head][1]+1;
                        if (x-1==k) 
                        {
                                printf("%d\n",q[tail][1]);
                                return 0;
                        }
                        mark[x-1]=1;
                        tail=(tail+1)%maxn;
                }
         if (x*2<</span>1001000 && !mark[x*2])
                {
                        q[tail][0]=x*2;
                        q[tail][1]=q[head][1]+1;
                        if (x*2==k) 
                        {
                                printf("%d\n",q[tail][1]);
                                return 0;
                        }
                        mark[x*2]=1;
                        tail=(tail+1)%maxn;
                }
                head=(head+1)%maxn;
        }
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值