题目来源
回收站选址
步骤
存储n个垃圾点的位置 数据存储格式: set< pair< int, int> > locatiion: {(
x
1
x_1
x 1 ,
y
1
y_1
y 1 ), (
x
2
x_2
x 2 ,
y
2
y_2
y 2 ), …, (
x
n
x_n
x n ,
y
n
y_n
y n )} 存储0、1、2、3、4各分值的回收站选址个数 数据存储格式:int score[5]; 循环遍历每个垃圾点的位置, 首先判断正四方(正东、正西、正南、正北)是否都存在垃圾点,若都存在垃圾点,则可作为回收站点。接着再对斜四方存在的垃圾点数进行计数 s, ( 0 <= s <= 4)。 最后输出 s
代码
# include <iostream>
# include <set>
# include <map>
using namespace std;
int main ( ) {
int N;
cin>> N;
set< pair< int , int > > location;
while ( N > 0 ) {
-- N;
int x, y;
cin>> x>> y;
location. insert ( pair < int , int > ( x, y) ) ;
}
int score[ 5 ] = { 0 } ;
for ( pair< int , int > p: location) {
pair< int , int > p1 = { p. first - 1 , p. second} ;
pair< int , int > p2 = { p. first + 1 , p. second} ;
pair< int , int > p3 = { p. first, p. second - 1 } ;
pair< int , int > p4 = { p. first, p. second + 1 } ;
pair< int , int > p5 = { p. first + 1 , p. second + 1 } ;
pair< int , int > p6 = { p. first - 1 , p. second - 1 } ;
pair< int , int > p7 = { p. first + 1 , p. second - 1 } ;
pair< int , int > p8 = { p. first - 1 , p. second + 1 } ;
int s = 0 ;
if ( location. find ( p1) != location. end ( ) && location. find ( p2) != location. end ( ) && location. find ( p3) != location. end ( ) && location. find ( p4) != location. end ( ) ) {
if ( location. find ( p5) != location. end ( ) ) {
++ s;
}
if ( location. find ( p6) != location. end ( ) ) {
++ s;
}
if ( location. find ( p7) != location. end ( ) ) {
++ s;
}
if ( location. find ( p8) != location. end ( ) ) {
++ s;
}
++ score[ s] ;
}
}
for ( int i = 0 ; i < 5 ; ++ i) {
cout<< score[ i] << endl;
}
return 0 ;
}