NC26010. smart robot
描述
输入描述
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; } } }