swpu winter training - part of the solution to a problem Simple search

B - Catch That Cow

The following questions

Then the meaning of the questions it very intuitive. To both the number nk, in abscissa, n represents the position of a person, k indicates the position of the cow. There are two types of people of action (action once take a minute) a forward or reverse 1 1 a direct position * 2, and the cattle will not move, people just cattle to position ended, the minimum time required.

Bfs written step by step would be finished.

Create a queue, a number of store location, store a number of times action.

The single set of input nk, if n> = k, before the bovine certifier, who need only the nk retracted position, i.e. nk minutes directly output. If not, it is added to the queue for processing.

If there is data in the queue, it is taken out. Analyzing is equal to k, is satisfied to a position representative of a cow, the number of output actions. Not satisfied with the action.

In action, you will find a problem, such as from 5-10, you can use the first class action, forward five times, it takes five minutes, you can also use the second category, it takes 1 minute. So how to choose the shortest it?

Then you need to open a bool type of array to record whether or not this has been to the location. If during the action, found next to the location that has been documented to before, this step action to stop it, if it is judged not been to this location, you can create branches. From the starting point, you can have three branches, each branch operations while the second step there are three branches, in order to form a "tree." According to the meaning of problems 0 ≤ n, k≤ 100,000, which is to build up a branch location to 100,000 (of course, in fact, when people stopped to position the cow is not built)

Remember: 1 to a position after the mark had been passed to. 2. When you are before the next move, the last step deleted queue position information, or has been the cycle continues.

Then the final code offer

#include<iostream>
#include<queue>
using namespace std;
int n,k;
bool visit[100005];
struct node {
    int x;
    int ans;
};
int bfs() {
    queue<node> q;
    q.push({n,0});
    while(q.size()) {
        node now=q.front();
        if(now.x==k)
            return now.ans;
        else if(!visit[now.x]) {
            visit[now.x]=1;
            if(now.x+1<=100000)
                q.push({now.x+1,now.ans+1});
            if(now.x-1>=0)
                q.push({now.x-1,now.ans+1});
            if(now.x*2<=100000)
                q.push({now.x*2,now.ans+1});
        }
        q.pop();
    }
    return 0;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    if(n>=k)
        cout<<n-k<<endl;
    else
        cout<<bfs()<<endl;
    return 0;
}
View Code

 

C - Find The Multiple

题目如下

这道题也比较一般,根据题意。多组数,求每个数的只含有0 1的最小公倍数。

输入的数>>需输出的数>>输出的数拆解后

1>>1>>1

2>>10>> 1*10

3>>111>> (1*10+1)*10+1

4>>100>> 1*10*10

5>>10>> 1*10

6>>1110>> ((1*10+1)*10+1)

 ······

可以看出,其实这是有规律的,那么可以从x=1开始,判断x*10%n或(x*10+1)%n是否为0,然后同样的,如果这步操作没有满足条件的,这两个分支再来开分支。

需要注意的是:1.数据大。2.每步操作完了删除队列中的这个数据,理由同上题。

完整代码

#include<iostream>
#include<queue>
using namespace std;
int n;
queue <long long int> q;
long long int bfs()
{
    while(q.size())
    {
        q.pop();
    }
    q.push(1);
    while(q.size())
    {
        if(q.front()%n==0)
        {
            return q.front();
        }
        long long int temp=q.front();
        q.pop();
        q.push(temp*10);
        q.push(temp*10+1);
    }
    return 0;
}

int main()
{
    while(cin>>n&&n)
    {
        cout<<bfs()<<endl;
    }
    return 0;
}
View Code

 

E - Oil Deposits

题目↓↓↓↓↓↓↓↓↓↓

 

这道题是多组数据。每组第一行给两个数,分别代表行数和列数,接下来有一个图,“*”为土,“@”为油田。规定一块油田可以上下左右斜着连在一起,找图里共有几块油田。

 

 

 

 

 

 

 

bcehj

Guess you like

Origin www.cnblogs.com/bigcn/p/12229668.html