Codeforces Beta Round #73(Div2)

B - Keyboard

题意:给键盘有n行键,每行有m个键。有些键上是小写的字母,有些键像标准键盘上的“Shift”键一样工作,也就是说,它们把小写字母变成大写。Vasya可以用一只手按一个或两个键(这些键被认为是边长为1的正方形 ),如果某些符号需要按"Shift"完成,它和最近的“Shift”键之间的距离严格大于x,那么就需要另一只手。求Vasya必须使用另一只手的最小次数。

题解:模拟。声明两个数组,一个数组用来储存键盘中存在的键,另一个数组用来储存S键与其它字母之间距离不大于X的键。如果给出的字符串中有小写字母,并且键盘中不存在输出-1,return 0;如果有大写字母但没有S键输出-1,return 0;,如果有S键并且键盘中存在该字母的小写字母且与S键的距离大于X则cnt++,否则输出-1,return 0;

#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath> 
#include<map>
using namespace std;
int m1[10010],m2[10010];
//m1判断shift能到哪些键,m2判断键盘中是否存在这些键 
string s[35];//键盘 
string str;
int main(){
    int n,m,x;
    ios::sync_with_stdio(false);
    cin>>n>>m>>x;
    for(int i=0;i<n;i++){
        cin>>s[i];
    }
    int flag=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='S'){
                flag=1;
                for(int p=0;p<n;p++){
                    for(int q=0;q<m;q++){
                        if((p-i)*(p-i)+(q-j)*(q-j)<=x*x&&s[p][q]!='S'){
                            m1[s[p][q]-'a']=1;
                        }
                    }
                }
            }else{
                m2[s[i][j]-'a']=1;
            }
        }
    }
    int cnt=0,t;
    cin>>t>>str;
    for(int i=0;i<t;i++){
        if(str[i]>='a'&&str[i]<='z'){
            if(!m2[str[i]-'a']){//这个键不存在 
                cout<<"-1"<<endl;
                return 0;
            }
        }else if(str[i]>='A'&&str[i]<='Z'){
            if(flag==0){
                cout<<"-1"<<endl;//没有S键 
                return 0;
            }
            if(m2[tolower(str[i])-'a']){//这个键存在并且不是S键 
                if(m1[tolower(str[i])-'a']==0){//和S键的距离大于X 
                    cnt++;
                }
            }else{
                cout<<"-1"<<endl;
                return 0;
            }
        }
    }

    cout<<cnt<<endl;
    return 0;
}
View Code

 

posted @ 2020-12-06 12:28  Endeavo_r  阅读(96)  评论(0)    收藏  举报