洛谷P6559 小镇

小镇 概述

n×n 的网格,其中有 k 个格子是房屋。

小镇里面还有灯。每两个相邻的格子之间有一盏灯。
如果两幢房屋相邻,那么这两幢房屋之间的灯就会点亮。

请问这座小镇一共有多少盏灯被点亮。
注意,本题中相邻指有公共边。
输入格式 第一行两个整数 n,kn,kn,k,表示网格边长以及房屋数量。
接下来 kkk 行每行两个整数 x,yx,yx,y,表示房屋的坐标。
输出格式 一行一个整数,表示答案。
输入输出样例
输入
6 12
1 1
2 1
2 2
1 4
3 3
4 3
4 4
3 4
3 6
4 6
5 6
6 6
输出
9

思路

每次读入一个房屋地点时,判断它周围有没有房屋,有的话累计个数,然后输出个数就行了
不是很简单吗,(鄙人错了10次,都是泪啊)
贴代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
using namespace std;
map<int,map<int,int> >a;
int main()
{
int n,m,s=0;
cin>>n>>m;
for (int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
a[x][y]=1;
s+=a[x-1][y]+a[x+1][y]+a[x][y+1]+a[x][y-1];
}
cout<<s;
return 0;
}

### 关于洛谷 P1833 题目解析 洛谷 P1833 是一道关于字符串处理的问题,具体来说是要求统计一段文字中不同单词的数量。为了高效解决这个问题,可以采用哈希表来记录各个单词出现的情况。 #### Python 实现方案 下面是一个基于Python实现的解决方案: ```python from collections import defaultdict def count_distinct_words(text): word_count = defaultdict(int) words = text.split() for word in words: cleaned_word = ''.join(filter(str.isalnum, word)).lower() # 清洗并转换成小写 if cleaned_word: word_count[cleaned_word] += 1 distinct_word_count = sum(1 for count in word_count.values() if count > 0) return distinct_word_count if __name__ == "__main__": input_text = input().strip() result = count_distinct_words(input_text) print(result) ``` 此程序首先读取输入的一段文本,接着通过`split()`函数分割这段文本成为单个词语列表;之后遍历这些词项,并利用正则表达式去除标点符号以及将其统一转为小写字母形式以便比较相同词汇的不同大小写的版本;最后统计去重后的唯一单词数量并输出结果[^1]。 #### C++ 实现方案 同样地,在C++中也可以采取类似的思路解决问题: ```cpp #include <iostream> #include <sstream> #include <map> #include <cctype> using namespace std; int main(){ string line; getline(cin,line); map<string,int> mp; stringstream ss(line); string s; while(ss >> s){ string temp = ""; for(auto ch : s){ if(isalpha(ch)){ temp+=tolower(ch); } } if(!temp.empty()){ ++mp[temp]; } } cout << mp.size()<< endl; } ``` 上述代码实现了同样的逻辑——接收一整行作为输入,使用stringstream按空格分隔单词,清理掉非字母字符并将它们全部变为小写后存入映射容器内计数,最终打印出独一无二的键值数目即为所求答案[^2]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值