2019河北省赛L题(dfs)

链接:https://ac.nowcoder.com/acm/contest/903/L
来源:牛客网
 

Icebound dreams of being aLegendary grandmaster. So he built a smart robot to help him.

The robot works on a N*N matrix. All the numbers in the matrix are non-negative integers less than 10. The robot can move in the matrix. If it's in the (x,y) position of the matrix, it can move to (x+1,y) , (x,y+1), (x-1,y), (x,y-1), which are the adjacent positions of the robot. But the robot has to operate inside the matrix. It can't get out of the matrix.

The robot can start at any position in the matrix, take any step, and it can stop at any position in the matrix. We connect the numbers in the robot's path in order to get a magic number. For example, if the robot takes 3 steps and passes through 3 numbers of the matrix, 0,7 and 8, the magic number is 78.All the magic numbers are non-negative integers, and the number generated by the robot does not contain leading 0.

Through the robot, icebound got a lot of magic numbers. Now he wants to know what isthe smallest magic number he can't get.

输入描述:

The first line is an integer N, denoting the length and width of the matrix.
The next N lines, N numbers per line, separated by spaces, represent this matrix. Ensure that each number in the matrix is less than 10 and greater than or equal to zero.
The numbers in the matrix are randomly generated.
1≤N≤501≤N≤50

输出描述:

One integer per line, indicating the smallest magic number that icebound can not get.

示例1

输入

复制

4
1 2 3 4
3 6 7 8
0 1 5 4 
9 1 1 1

输出

复制

17

题意:
你可以从任意一个点开始走(上下左右)把走过的数连在一起成为一个魔幻数。例如:依次走过的数位0,7,8,那么魔幻数为78.

问你不能获得的魔幻数的最小是多少?

思路:

从每一个点开始搜索走5步(6步当然也可以了)所能获得的所有魔幻数。

然后从1开始到1e6枚举;

输出第一个没有出现的数就行了;

这里有两个坑

1,从A点--->B点然后从B点又可以再走到A点。

2,枚举时从1开始到1e6不是从0开始。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int vis[maxn];
int a[60][60];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int n;
void dfs(int b,int x,int y,int step)
{
    if(step>=5)return;
   for(int i=0;i<=3;i++)
   {
       int nx=x+dx[i];
       int ny=y+dy[i];
       if(nx>=1&&nx<=n&&ny>=1&&ny<=n)
       {
           int w=b*10+a[nx][ny];
           vis[w]=1;
           dfs(w,nx,ny,step+1);
       }
   }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int b=a[i][j];
            vis[b]=1;
            dfs(b,i,j,1);
        }
    }
    for(int i=1;i<=maxn;i++)
    {
        if(vis[i]==0)
        {
            cout<<i<<endl;
            break;
        }
    }
return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值