10571. MISA
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
A nice part of the Roman Catholic Mass is the rite of peace when people shake hands with their
neighbours and say "peace be with you". Mirko has found a way to turn this ritual into his own favor.
Inside the church, there are R rows of benches where each row can hold a capacity of S people. We can
imagine the seating order as a matrix sized R x S where each element represents either a person or an
empty seating space. Let us assume that each person shakes hands with their neighbours. That means
that the neighbours are located in one of the eight neighbouring elements (if such element exists):
A seating order of the people inside the church has been given before Mirko enters. Mirko is, of course,
late for the morning Mass and will sit in an empty space so that he shakes hands with as many people
as he can. If there are no empty seats left, Mirko will simply give up on the idea and go to the evening
Mass instead. We can assume that nobody enters the church after Mirko.
Calculate the total number of handshakes given during the morning Mass.
Input
The first line of input contains positive integers R and S (1 ≤ R, S ≤ 50) as stated in the text.
Each of the following R lines contains S characters. These R x S characters represent the seating order.
The character '.' (dot) represents an empty place and the character 'o' (lowercase letter o) represents a
person.
Output
The first and only line of output should contain the required number of handshakes.
Sample Input
====Sample 1==== 2 3 ..o o.. ====Sample 2==== 2 2 oo oo
Sample Output
====Sample 1==== 2 ====Sample 2==== 6
Problem Source
2014年每周一赛第四场暨校赛模拟赛I/COCI
很水啦,首先判断主角去哪个位置或者不去,然后判断成对数
// Problem#: 10571
// Submission#: 2809763
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <string.h>
char mapp[51][51];
int r, s;
int dir[8][2] = {-1, 0, -1, 1, 0, 1, 1, 1, 1, 0, 1, -1, 0, -1, -1, -1};
bool used[51][51];
bool is_valid(int ii, int jj) {
if (ii < 0 || jj < 0 || ii >= r || jj >= s || used[ii][jj] || mapp[ii][jj] == '.')
return false;
return true;
}
int is_valid_num(int ii, int jj) {
int counter = 0;
for (int i = 0; i < 8; i++) {
int temp_ii = ii + dir[i][0];
int temp_jj = jj + dir[i][1];
if (is_valid(temp_ii, temp_jj))
counter++;
}
return counter;
}
int main() {
scanf("%d%d\n", &r, &s);
for (int i = 0; i < r; i++) {
gets(mapp[i]);
}
int pos_ii, pos_jj;
int max_valid = -1;
for (int i = 0; i < r; i++) {
for (int j = 0; j < s; j++) {
if (mapp[i][j] == '.' && max_valid < 8 && is_valid_num(i, j) > max_valid) {
max_valid = is_valid_num(i, j);
pos_ii = i;
pos_jj = j;
}
}
}
if (max_valid != -1) {
mapp[pos_ii][pos_jj] = 'o';
}
int counter = 0;
memset(used, 0, sizeof(used));
for (int i = 0; i < r; i++) {
for (int j = 0; j < s; j++) {
if (mapp[i][j] == 'o') {
for (int k = 0; k < 8; k++) {
int temp_ii = i + dir[k][0];
int temp_jj = j + dir[k][1];
if (is_valid(temp_ii, temp_jj)) {
counter++;
}
}
used[i][j] = true;
}
}
}
printf("%d\n", counter);
return 0;
}