2019河北省大学生程序设计竞赛:L.smart robot

104 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第19天,点击查看活动详情

2019河北省大学生程序设计竞赛——L-smart robot

L-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.
1≤N≤50

输出描述:

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

输入

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

输出

17

问题解析

其实可以感觉出,这个题的答案不可能太大。因为如果答案是一个很大的数,则这个答案之前的数都要能从矩阵中找出,这对于一个50*50大小的矩阵来说不太可能。(后来发现,数据最大的答案到700左右就没了)

我们可以从0开始枚举数,当有一个数不能从矩阵中找出后,说明这个数就是答案了。

至于将每个数从矩阵中找出,我们可以采用多源bfs的方式,比如我们想找出134,那可以先以矩阵中所有为4的位置为起点,然后看4的周围有没有3的格子,最后再看有没有1的位置。

至于找起点,我们还可以先遍历矩阵,把每个数的出现位置都存下来。

AC代码

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include <random>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<fstream>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>#pragma GCC optimize(2)
#pragma GCC optimize(3)#define endl '\n'
#define int ll
#define PI acos(-1)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 100, MOD = 998244353;
​
int f[N][N], n;
bool st[N][N];
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
vector<PII>pos[10];
bool bfs(int num)
{
    if (pos[num % 10].empty())return false;
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= n; j++)
            st[i][j] = false;
    queue<PII>que;
    int ans = num % 10;
    num /= 10;
    for (auto& i : pos[ans])
    {
        que.push(i);
    }
    while (!que.empty())
    {
        if (num == 0)return true;
        ans = num % 10;
        int len = que.size();
        for (int i = 0; i < len; i++)
        {
            int x = que.front().first, y = que.front().second;
            que.pop();
            for (int j = 0; j < 4; j++)
            {
                int a = x + dx[j], b = y + dy[j];
                if (a >= 1 && a <= n && b >= 1 && b <= n && f[a][b] == ans)
                {
                    que.push({ a,b });
                }
            }
        }
        num /= 10;
    }
    return false;
}
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            cin >> f[i][j];
            pos[f[i][j]].push_back({ i,j });
        }
    for (int i = 0; i <= 1e7; i++)
    {
        if (!bfs(i))
        {
            cout << i << endl;
            return;
        }
    }
}
​
signed main()
{
​
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    //cin >> t;
​
    while (t--)
    {
        solve();
    }
    return 0;
}