15届蓝桥杯cb组 数字接龙+r格式

本文介绍了如何利用深度优先搜索(DFS)算法在给定的二维整数网格中寻找特定模式。程序首先定义了一个检查函数,然后通过递归实现DFS,当找到目标值时,记录路径并判断是否满足条件。最后,还涉及到了字符串处理的部分,对R格式进行操作。
摘要由CSDN通过智能技术生成
#include<iostream>

#include<vector>

using namespace std;
const int N = 20;

int n,k;

bool vis[N][N];

int g[N][N];

bool f = false;

int dx[] = {-1,-1,0,1,1,1,0,-1},dy[] = {0,1,1,1,0,-1,-1,-1};

vector<int> res;


bool check()
{
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<n;j++)
        {
            if(vis[i][j] == false)
            return false;
        }
    }
    
    return true;
}

void dfs(int x,int y,int r)
{
    if(f) return ;
    
    if( x <0 || x>=n || y<0 || y>=n || g[x][y] != r)
    {
        return ;
    }
    
    if(x == n-1 && y == n-1 && g[x][y] == r)
    {
       vis[x][y] = true;
       if(check()) 
       {
           f = true;
           for(int i = 0;i<res.size();i++)
            {
            cout<<res[i];
            }
       }
       else
       {
           vis[x][y] = false;
           return ;
       }
    }
    
    for(int i = 0;i<=7;i++)
    {
        if(!vis[x][y])
        {
            /*vis[x][y] = true;
            res.push_back(i);
            int xx = x +dx[i],yy = y + dy[i];*/
            
            if(i == 1)
            {   
                if(x>0 && (!vis[x-1][y] || !vis[x][y+1]))
                {
                    vis[x][y] = true;
                     res.push_back(i);
                    int xx = x +dx[i],yy = y + dy[i];
                    dfs(xx,yy,(r+1)%k);
                    vis[x][y] = false;
                    res.pop_back();
                }
            }
            else if(i == 3)
            {   
                if(!vis[x+1][y] || !vis[x][y+1])
                {
                    vis[x][y] = true;
                    res.push_back(i);
                    int xx = x +dx[i],yy = y + dy[i];
                    dfs(xx,yy,(r+1)%k);
                    vis[x][y] = false;
                    res.pop_back();
                }
            }
            else if(i == 5)
            {   
                if(y>0 && (!vis[x][y-1] || !vis[x+1][y]))
                {
                    vis[x][y] = true;
                    res.push_back(i);
                    int xx = x +dx[i],yy = y + dy[i];
                    dfs(xx,yy,(r+1)%k);
                    vis[x][y] = false;
                    res.pop_back();
                }
            }
            else if(i == 7)
            {   
                if(x>0 && y>0 && (!vis[x-1][y] || !vis[x][y-1]))
                {
                    vis[x][y] = true;
                    res.push_back(i);
                    int xx = x +dx[i],yy = y + dy[i];
                    dfs(xx,yy,(r+1)%k);
                    vis[x][y] = false;
                    res.pop_back();
                }
            }
           else
           {
                vis[x][y] = true;
                res.push_back(i);
                int xx = x +dx[i],yy = y + dy[i];
                dfs(xx,yy,(r+1)%k);
                vis[x][y] = false;
                res.pop_back();
           }
        }
    }
}

int main()
{
    cin>>n>>k;
    
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<n;j++)
        {
            cin>>g[i][j];
        }
    }
    int r = 0;
    dfs(0,0,r);
    
    if(!f)
    {
        cout<<"-1";
    }
    
    
    
    return 0;
}

R格式 

#include<bits/stdc++.h>

using namespace std;

string s;

int n;

vector<char> A;


vector<char> mul(vector<char> A,int n)
{
    vector<char> C;
    int t = 0;
    for(int i = 0;i<A.size();i++)
    {
        if(A[i] == '.')
        {
            C.push_back(A[i]);
        }
        else
        {
            int x = A[i] - '0';
            t += x*n;
            C.push_back((t%10) + '0');
            t = t/10;
        }
    }
    
    if(t)
    {
        while(t)
        {
            C.push_back((t%10) + '0');
            t = t/10;
        }
    }
    
    return C;
}

int main()
{
    cin>>n;
    
    cin>>s;
    
    for(int i = s.size()-1;i>=0;i--)
    {
        A.push_back(s[i]);
    }
    vector<char> C;
    while(n--)
    {
        C = mul(A,2);
        A = C;
    }
    string res;
    for(int i = C.size()-1;i>=0;i--)
    {
        if(C[i]!= '.')
        res += C[i];
        
        else
        {
            if(C[i-1] >= '5')
            {
                
                int  y = C[i+1] -'0';
                
                char c = (y+1) + '0';
                
                res[res.size()-1] = c;
                
                cout<<res;
                
                return 0;
            }
            else
            {
                cout<<res;
                
                return 0;
            }
        }
    }
    
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值