2019河北省赛L题(dfs)

编程入门 行业动态 更新时间:2024-10-26 08:26:02

2019<a href=https://www.elefans.com/category/jswz/34/1731571.html style=河北省赛L题(dfs)"/>

2019河北省赛L题(dfs)

链接:
来源:牛客网
 

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;
}


 

更多推荐

2019河北省赛L题(dfs)

本文发布于:2024-02-17 05:32:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1692825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:河北省   dfs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!