1 条题解

  • 0
    @ 2025-8-24 22:19:36

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar y0y68
    AFO

    搬运于2025-08-24 22:19:36,当前版本为作者最后更新于2020-04-16 21:09:28,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    怎么连一篇STLSTL 题解都没有啊!!!


    前置芝士:

    11. mapmap

    其实 mapmap 没不用理解成映射这么高大上的词语,我们可以这样理解:

    定义 map<string,bool>fmap<string,bool>f 就表示建立一个下标为 stringstring 类型的 ff 数组,里面存的都是 boolbool 值。

    :要加头文件 include<map>

    22. pairpair

    举个栗子:

    pair<int,char>a
    

    就相当于

    struct Node{
    	int first;
    	char second;
    }a;
    

    显然,a.firsta.firstintint 类型(不管是struct还是pair);同理,a.seconda.secondcharchar 类型(也不管是struct还是pair)。

    再举个栗子:

    再假设变量 iiintint 类型,jjcharchar类型,则

    a=Node{i,j};
    

    就相当于

    a=make_pair(i,j);
    

    33. 曼哈顿距离

    啥都不说,给公式:

    AA 点的坐标为 (x1,y1)(x_1,y_1)BB 点的坐标为 (x2,y2)(x_2,y_2)

    AABB 的曼哈顿距离为:

    x1x2+y1y2|x_1-x_2|+|y_1-y_2| (想想为什么)


    然后就没什么好说的了,上代码:

    #include<iostream>
    #include<cstdio>
    #include<map>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    char a[5][5];
    map< char,pair<int,int> >pos;
    //dis会返回x行y列到字符ch的标准位置的曼哈顿距离
    inline int dis(char ch,int x,int y){
    	return abs(pos[ch].first-x)+abs(pos[ch].second-y);
    	//套曼哈顿距离的公式
    }
    int main(){
    	//打表部分开始
    	pos['A']=make_pair(1,1);
    	pos['B']=make_pair(1,2);
    	pos['C']=make_pair(1,3);
    	pos['D']=make_pair(1,4);
    	pos['E']=make_pair(2,1);
    	pos['F']=make_pair(2,2);
    	pos['G']=make_pair(2,3);
    	pos['H']=make_pair(2,4);
    	pos['I']=make_pair(3,1);
    	pos['J']=make_pair(3,2);
    	pos['K']=make_pair(3,3);
    	pos['L']=make_pair(3,4);
    	pos['M']=make_pair(4,1);
    	pos['N']=make_pair(4,2);
    	pos['O']=make_pair(4,3);
    	//打表部分结束
    	int ans=0;
    	for(int i=1;i<=4;i++){
    		scanf("%s",a[i]+1);
    		for(int j=1;j<=4;j++)
    			if(a[i][j]!='.')ans+=dis(a[i][j],i,j);
    			//注意a[i][j]不能为'.'
        }
    	cout<<ans<<endl;
    	return 0;
    }
    
    • 1

    信息

    ID
    5349
    时间
    1000ms
    内存
    32MiB
    难度
    2
    标签
    递交数
    0
    已通过
    0
    上传者