每日一题冲刺大厂第十三天提高组 第46届ICPC 东亚区域赛 A题

大家好,我是泡泡,给大家带来每日一题的目的是为了更好的练习算法,我们的每日一题提高组是为了有余力的同学准备的,让大家练到各种各样的题目,一年以后,蜕变成为一个不一样的自己!

🎉欢迎关注🔎点赞👍收藏⭐️留言📝

❤️ :热爱C/C++与算法学习,云计算等,期待一起交流!

🙏作者水平有限,如果发现错误,求告知,多谢!

👻高校算法学习社区:https://bbs.csdn.net/forums/Suanfa

一起加入刷题内卷大军,还可以加入专属内卷群,里面福利多多大佬多多!

今日题目:icpc A 

 链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

BaoBao the Witch is stuck in a maze with nnn rows and nnn columns, where the height of the cell in the iii-th row and the jjj-th column is hi,jh_{i,j}hi,j​. To get out of the maze, BaoBao has to find a path which passes through each cell exactly once. Each time she can only move into the neighboring cell sharing a same edge with the current one. But as we know, BaoBao is super lazy, so every time when she climbs up (that is to say, moving from a cell with a smaller height to another with a larger height) her happiness value will decrease. As her helping hand, your task is to find a valid path so that when moving along the path, the number of times BaoBao climbs up will not be more than the number of times she climbs down.

More formally, you need to find a sequence (x1,y1),(x2,y2),⋯ ,(xn2,yn2)(x_1, y_1), (x_2, y_2), \cdots, (x_{n^2}, y_{n^2})(x1​,y1​),(x2​,y2​),⋯,(xn2​,yn2​) such that:

  • For all 1≤i≤n21 \le i \le n^21≤i≤n2, 1≤xi,yi≤n 1 \le x_i, y_i \le n1≤xi​,yi​≤n;
  • For all 1≤i,j≤n2,i≠j1 \le i, j \le n^2, i \neq j1≤i,j≤n2,i​=j, (xi,yi)≠(xj,yj) (x_i, y_i) \neq (x_j, y_j)(xi​,yi​)​=(xj​,yj​);
  • For all 2≤i≤n22 \le i \le n^22≤i≤n2, ∣xi−xi−1∣+∣yi−yi−1∣=1|x_i - x_{i-1}| + |y_i - y_{i-1}| = 1∣xi​−xi−1​∣+∣yi​−yi−1​∣=1;
  • ∑i=2n2[hxi−1,yi−1<hxi,yi]≤∑i=2n2[hxi−1,yi−1>hxi,yi]\sum\limits_{i=2}^{n^2}{[h_{x_{i-1}, y_{i-1}} < h_{x_i, y_i}]} \le \sum\limits_{i=2}^{n^2}{[h_{x_{i-1}, y_{i-1}} > h_{x_i, y_i}]}i=2∑n2​[hxi−1​,yi−1​​<hxi​,yi​​]≤i=2∑n2​[hxi−1​,yi−1​​>hxi​,yi​​], where [P][P][P] equals 111 when PPP is true, and equals 000 when it is false.

Additionally, you discover that the heights in all cells are a permutation of n2n^2n2, so you just need to output the height of each cell in a valid path.

输入描述:

 
 

There are multiple test cases. The first line of the input contains an integer T (1≤T≤1001 \le T \le 1001≤T≤100) indicating the number of test cases. For each test case:

The first line contains an integer nnn (2≤n≤642 \le n \le 642≤n≤64) indicating the size of the maze.

For the following nnn lines, the iii-th line contains nnn integers hi,1​,hi,2​,⋯,hi,n​ (1≤hi,j≤n21) where hi,jh_{i,j}hi,j​ indicates the height of the cell in the iii-th row and the jjj-th column. It's guaranteed that all integers in the input make up a permutation of n2.

输出描述:

For each test case output one line containing n2n^2n2 separated by a space indicating the heights of each cell in a valid path. If there are multiple valid answers you can output any of them. It's easy to prove that an answer always exists.

Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!

题目分析

题目难度:⭐️⭐️

题目涉及算法:bfs,队列,暴力。

ps:有能力的小伙伴可以尝试优化自己的代码或者一题多解,这样能综合提升自己的算法能力

题解报告:

1.思路

如果符合条件直接输出,不符合就直接反着来就好。

2.代码

#include<bits/stdc++.h>
using namespace std;
int a[100][100],b[100*100];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cin>>a[i][j];
            }
        }
        int len=0,num=0,sum=0;
        for(int i=1;i<=n;i++)
        {
            if(i%2==0)
            {
                for(int j=n;j>=1;j--)
                {
                    b[++len] = a[i][j];
                }
            }
            else
            {
                for(int j=1;j<=n;j++)
                {
                    b[++len] = a[i][j];
                }
            }
        }
        for(int i=1;i<len;i++)
        {
            if(b[i]>b[i+1])
            {
                num++;
            }
            else if(b[i]<b[i+1])
            {
                sum++;
            }
        }
        if(sum<=num)
        {
            cout<<b[1];
            for(int i=2;i<=len;i++)
            {
                cout<<" "<<b[i];
            }
        }
        else
        {
            cout<<b[len];
            for(int i=len-1;i>=1;i--)
            {
                cout<<" "<<b[i];
            }
        }
        cout<<endl;
    }
	return 0;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cloud、泡泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值