列表

详情


NC26010. smart robot

描述

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.

输出描述

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 27ms, 内存消耗: 1004K, 提交时间: 2019-05-25 14:28:25

#include<stdio.h>
int a[55][55],n,t[100005]={0},v;
void bfs(int x,int y,int k,int tt)
{
	t[k]=1;
	if(tt>=5)
	return ;
	if(x-1>=0)
	bfs(x-1,y,k*10+a[x-1][y],tt+1);
	if(x+1<n)
	bfs(x+1,y,k*10+a[x+1][y],tt+1);
	if(y-1>=0)
	bfs(x,y-1,k*10+a[x][y-1],tt+1);
	if(y+1<n)
	bfs(x,y+1,k*10+a[x][y+1],tt+1);
	return ;
}
int main()
{
	scanf("%d",&n);
	int i,j,k;
	for(i=0;i<n;i++)
	for(j=0;j<n;j++)
	{
		scanf("%d",&a[i][j]);
	}
	for(i=0;i<n;i++)
	for(j=0;j<n;j++)
	bfs(i,j,0,0);
	for(i=1;i<=100005;i++)
	if(t[i]==0)
	{
		printf("%d",i);
		break;
	}
 } 
 //2500*64

C++11(clang++ 3.9) 解法, 执行用时: 14ms, 内存消耗: 768K, 提交时间: 2020-08-13 14:06:28

#include<stdio.h>
int a[55][55];
int n,v[1000006];
void dfs(int x,int y,int k,int t)
{
	v[k]=1;
	if(t>=5)
	return;
	if(x-1>=0)
	dfs(x-1,y,k*10+a[x][y],t+1);
	if(y-1>=0)
	dfs(x,y-1,k*10+a[x][y],t+1);
	if(x+1<n)
	dfs(x+1,y,k*10+a[x][y],t+1);
	if(y+1<n)
	dfs(x,y+1,k*10+a[x][y],t+1);
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	for(int j=0;j<n;j++)
	scanf("%d",&a[i][j]);
	for(int i=0;i<n;i++)
	for(int j=0;j<n;j++)
	dfs(i,j,0,0);
	for(int i=0;;i++)
	{
		if(v[i]==0)
		{
			printf("%d",i);
			break;
		}
	}
}

上一题