训练总结 7.31

来源:互联网 发布:第三方数据平台 编辑:程序博客网 时间:2024/06/07 06:53
一、时间安排

  今天做了几道搜索题。上午主要看了一下课件,回顾了搜索的知识点和几个经典的例题,发现好多东西都忘了。下午做了几道杭电上的题,样例是通过了,但是因为网站的原因,没有提交,暂时还不知道能不能过。

二、题目

  今天下午做的三道搜索题,都比较简单,而且之前也都做过。

  1、A Knight's Journey  一道深搜,遍历棋盘输出路径。深搜比较愁的是回溯,理解得不透彻。在这道题的具体实现过程遇到了两个问题

(1)题目要求按字典序输出,这就要求起点为(1,1),并且定义方向数组是必须按照一定的顺序。这一点在一开始没有注意到。

(2)还有一个小错误。if(vis)等价于if(vis!=0),在写的时候把这个想反了,找了好久的错。

#include<bits/stdc++.h>
using namespace std;

int mp[31][31];
bool vis[31][31];
int p,q;
bool success;

struct Push
{
    char xx;
    char yy;
}push[31];

//int dx[8]={-2,-2,-1,-1,1,1,2,2};
//int dy[8]={-1,1,-2,2,-2,2,-1,1};
int dx[8]={-1,1,-2,2,-2,2,-1,1};
int dy[8]={-2,-2,-1,-1,1,1,2,2};


bool check(int x,int y)
{
    if(x<1||x>p||y<1||y>q)
        return false;
    if(success)
        return false;
    if(vis[x][y])
        return false;
    return true;
}

void dfs(int x,int y,int step)
{
    push[step].yy=y-1+'A';
    push[step].xx=x+'0';
    if(step==p*q)
    {
        success=true;
        return;
    }
    for(int i=0;i<8;i++)
    {
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(check(nx,ny))
        //if (0<nx&&nx<=p&&0<ny&&ny<=q&&!vis[nx][ny]&&!success)
        {
            vis[nx][ny]=1;
            dfs(nx,ny,step+1);
            vis[nx][ny]=0;
        }
    }
}


int main()
{
    int T,cas=1;
    cin>>T;
    while(T--)
    {
        cin>>p>>q;
        success=0;
        memset(vis,0,sizeof(vis));
        vis[1][1]=1;
        dfs(1,1,1);
        cout<<"Scenario #"<<cas<<":"<<endl;
        if(success)
        {
            for(int i=1;i<=p*q;i++)
            {
                cout<<push[i].yy<<push[i].xx;
            }
            cout<<endl;
        }
        else
            cout<<"impossible"<<endl;
        cas++;
        if(T!=0)
            cout<<endl;
    }
    return 0;
}
2、Catch That Cow  比较简单的广搜

#include<bits/stdc++.h>
using namespace std;

int n,k;
bool vis[200001];

struct Node
{
    int x,step;
};

int bfs(int n,int k)
{
    queue<Node>q;
    Node now,next;
    vis[n]=1;
    now.x=n;
    now.step=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.x==k)
            return now.step;
        if(now.x>=1&&vis[now.x-1]==0)
        {
            next.x=now.x-1;
            next.step=now.step+1;
            q.push(next);
            vis[now.x-1]=1;
        }
        if(vis[now.x+1]==0&&now.x+1<100001)
        {
            next.x=now.x+1;
            next.step=now.step+1;
            q.push(next);
            vis[now.x+1]=1;
        }
        if(vis[2*now.x]==0&&2*now.x<100001)
        {
            next.x=2*now.x;
            next.step=now.step+1;
            q.push(next);
            vis[2*now.x]=1;
        }
    }

return 0;
}

int main()
{
    while(cin>>n>>k)
    {
memset(vis,0,sizeof(vis));       

if(n>=k)
            cout<<(n-k)<<endl;
        else
            cout<<bfs(n,k)<<endl;
    }
    return 0;
}

3、find the multiple  简单的深搜,需要控制long long 的最大位数

#include<bits/stdc++.h>
using namespace std;

bool vis;

void dfs(long long x,long long n,int step)
{
    if(vis)  return;
    if(x%n==0)
    {
        cout<<x<<endl;
        vis=true;
        return;
    }
    if(step==18)
        return;
    dfs(x*10,n,step+1);
    dfs(x*10+1,n,step+1);
}

int main()
{
    long long n;
    while(cin>>n&&n)
    {
        vis=false;
        dfs(1,n,0);
    }
    return 0;
}

 

 

 

 

原创粉丝点击