2016 百度校招研发算法题 9.12
Posted SeeKHit
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2016 百度校招研发算法题 9.12相关的知识,希望对你有一定的参考价值。
很简单,直接ac:
1 #include "iostream" 2 3 using namespace std; 4 5 int n, t, tag = 1; 6 int main() 7 { 8 cin >> n; 9 10 if (n % 2 == 1) 11 t = n; 12 else 13 t =n+ 1; 14 15 for (int i = 0; i < t; i++) 16 { 17 cout << n; 18 } 19 cout << endl; 20 21 for (int i = 0; i < n; i++) 22 { 23 for (int j = 1; j <= t; j++) 24 { 25 if (j == (t / 2 + 1)) 26 { 27 cout << tag++; 28 continue; 29 } 30 cout << n; 31 32 } 33 cout << endl; 34 } 35 }
2
思路:
dfs,找出联通的房子,再判断对角是否为空地
1 #include "iostream" 2 #include "vector" 3 using namespace std; 4 5 char s[101][101]; 6 int N, M; 7 int di[4] = { 1, -1, 0, 0 }; 8 int dj[4] = { 0, 0, 1, -1 }; 9 vector<int> area; 10 11 int dfs(int i, int j, int *mini, int *minj, int *maxi, int *maxj) { 12 if (i < *mini) 13 *mini = i; 14 if (i > *maxi) 15 *maxi = i; 16 if (j < *minj) 17 *minj = j; 18 if (j > *maxj) 19 *maxj = j; 20 s[i][j] = ‘x‘; 21 int a = 1; 22 for (int d = 0; d < 4; ++d) { 23 int ni = i + di[d], nj = j + dj[d]; 24 if (s[ni][nj] == ‘1‘) 25 a += dfs(ni, nj, mini, minj, maxi, maxj); 26 } 27 return a; 28 } 29 30 bool isBeauty(int mini, int minj, int maxi, int maxj) 31 { 32 if (s[mini - 1][minj - 1] == ‘0‘ 33 &&s[mini - 1][maxj + 1] == ‘0‘ 34 &&s[maxi + 1][minj - 1] == ‘0‘ 35 &&s[maxi + 1][maxj + 1] == ‘0‘) 36 return true; 37 else 38 return false; 39 } 40 41 int main() { 42 cin >> N >> M; 43 memset(s, ‘0‘, sizeof(s)); 44 for (int i = 0; i < N; ++i) 45 cin >> s[i + 1] + 1; 46 47 int n1 = 0, n2 = 0; 48 for (int i = 1; i <= N; ++i) 49 for (int j = 1; j <= M; ++j) 50 if (s[i][j] == ‘1‘) { 51 n1++; 52 int mini = i, maxi = i, minj = j, maxj = j; 53 int s = dfs(i, j, &mini, &minj, &maxi, &maxj); 54 area.push_back(s); 55 if (isBeauty(mini, minj, maxi, maxj)) 56 n2++; 57 } 58 cout << n2 << endl; 59 60 system("pause"); 61 return 0; 62 }
以上是关于2016 百度校招研发算法题 9.12的主要内容,如果未能解决你的问题,请参考以下文章