Skip to content

Commit d084993

Browse files
author
aaron.liu
committed
shuati
1 parent cb0a733 commit d084993

21 files changed

+642
-5
lines changed

BFS/BattleCity.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// link: https://blog.csdn.net/yinghui_yht/article/details/68920801
3+
//
4+
5+
#include <cstdio>
6+
#include <cstring>
7+
#include <iostream>
8+
#include <algorithm>
9+
#include <queue>
10+
using namespace std;
11+
12+
struct node {
13+
int x,y;
14+
int step;
15+
} st,ed;
16+
17+
int bfs(vector<vector<char>>& map, vector<vector<bool>>& visited) {
18+
int next[4][2]= {{-1,0}, {1,0}, {0,1}, {0,-1}};
19+
queue<node> que;
20+
que.push(st);
21+
22+
while (!que.empty()) {
23+
st = que.front();
24+
que.pop();
25+
26+
if (map[st.x][st.y] == 'T') {
27+
return st.step;
28+
}
29+
30+
if (map[st.x][st.y] == 'B') {
31+
st.step = st.step + 1;
32+
map[st.x][st.y] = 'E';
33+
que.push(st);
34+
continue;
35+
}
36+
37+
for (int i = 0; i < 4; i++) {
38+
ed.x = st.x + next[i][0];
39+
ed.y = st.y + next[i][1];
40+
if (ed.x < 0 || ed.x >= map.size() || ed.y < 0 || ed.y >= map[0].size() ||
41+
map[ed.x][ed.y] == 'S' || map[ed.x][ed.y] == 'R' || visited[ed.x][ed.y]) {
42+
continue;
43+
}
44+
45+
ed.step = st.step + 1;
46+
visited[ed.x][ed.y] = true;
47+
que.push(ed);
48+
}
49+
}
50+
51+
return -1;
52+
}
53+
54+
int main() {
55+
vector<vector<char>> map = {{'S', 'E', 'B', 'Y'}, {'S', 'T', 'B', 'B'}};
56+
int m = map.size(), n = map[0].size();
57+
vector<vector<bool>> visited(m, vector<bool>(n));
58+
st.x = 0, st.y = 3;
59+
st.step = 0;
60+
int ret = bfs(map, visited);
61+
cout << "ret == " << ret << endl;
62+
}

BFS/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
include_directories(.)
33

4-
add_executable(BFS LC1020NumberOfEnclaves.cpp LC1254NumberOfClosedIslands.cpp)
4+
add_executable(BFS LC1020NumberOfEnclaves.cpp LC1254NumberOfClosedIslands.cpp LGP1330BlockSunUniersity.cpp BattleCity.cpp)

BFS/LC1020NumberOfEnclaves.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class NumberOfEnclaves {
5757
return (i >= 0 && i < n && j >= 0 && j < m);
5858
}
5959
};
60-
60+
/*
6161
int main() {
6262
NumberOfEnclaves inst;
6363
vector<vector<int>> A =
@@ -69,4 +69,6 @@ int main() {
6969
return result;
7070
}
7171
72+
*/
73+
7274

BFS/LC1254NumberOfClosedIslands.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <vector>
66

77
using namespace std;
8-
8+
/*
99
class Solution {
1010
1111
public:
@@ -34,4 +34,5 @@ class Solution {
3434
return result;
3535
}
3636
37-
};
37+
};
38+
*/

BFS/LGP1330BlockSunUniersity.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// link: https://www.luogu.com.cn/problem/P1330
3+
//
4+
#include <cstring>
5+
#include <algorithm>
6+
#include <cstdio>
7+
#include <queue>
8+
using namespace std;
9+
10+
const int N = 10010, M = 200010;
11+
int h[N], e[M], ne[M], idx;
12+
bool visited[M]; // if visited
13+
int colors[N]; // color of each node
14+
int sum[2]; // each number of nodes of two colors
15+
queue<int> que;
16+
17+
void add(int a, int b) {
18+
e[idx] = b;
19+
ne[idx] = h[a];
20+
h[a] = idx;
21+
idx += 1;
22+
}
23+
24+
bool bfs(int start) {
25+
que.push(start);
26+
visited[start] = true;
27+
colors[start] = 0;
28+
sum[0] = 1;
29+
sum[1] = 0;
30+
31+
while (!que.empty()) {
32+
int u = que.front();
33+
que.pop();
34+
35+
for (int i = h[u]; i != -1; i = ne[i]) {
36+
int j = e[i];
37+
if (visited[j]) {
38+
if (colors[j] != 1 - colors[u]) return false;
39+
continue;
40+
}
41+
visited[j] = true;
42+
colors[j] = 1 - colors[u];
43+
sum[colors[j]] += 1;
44+
que.push(j);
45+
}
46+
}
47+
return true;
48+
}
49+
/*
50+
int main() {
51+
int n, m;
52+
scanf("%d%d", &n, &m);
53+
memset(h, -1, sizeof h);
54+
55+
int a, b;
56+
for (; m-- > 0;) {
57+
scanf("%d%d", &a, &b);
58+
add(a, b);
59+
add(b, a);
60+
}
61+
62+
int ret = 0;
63+
for (int i = 1; i <= n; i++) {
64+
if (visited[i]) continue;
65+
if (!bfs(i)) {
66+
printf("Impossible");
67+
return 0;
68+
}
69+
ret += min(sum[0], sum[1]);
70+
}
71+
72+
printf("%d", ret);
73+
return 0;
74+
}
75+
*/

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ ADD_SUBDIRECTORY(BFS)
1010
ADD_SUBDIRECTORY(LinkedLists)
1111
ADD_SUBDIRECTORY(Trie)
1212
ADD_SUBDIRECTORY(Graph)
13+
ADD_SUBDIRECTORY(Math)
14+
ADD_SUBDIRECTORY(Tree)
15+
ADD_SUBDIRECTORY(Hash)
1316

1417
add_executable(AlgoInCpp main.cpp)

DFS/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
include_directories(.)
33

4-
add_executable(DFS LC1020NumberOfEnclaves.cpp LC1254NumberOfClosedIslands.cpp)
4+
add_executable(DFS LC1020NumberOfEnclaves.cpp LC1254NumberOfClosedIslands.cpp LGP1330BlockSunUniersity.cpp)

DFS/LGP1330BlockSunUniersity.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// Created by Aaron Liu on 5/2/20.
3+
//
4+
5+
//
6+
// link: https://www.luogu.com.cn/problem/P1330
7+
//
8+
#include <cstring>
9+
#include <algorithm>
10+
#include <cstdio>
11+
12+
using namespace std;
13+
14+
const int N = 10010, M = 200010;
15+
int h[N], e[M], ne[M], idx;
16+
bool visited[M]; // if visited
17+
int colors[N]; // color of each node
18+
int sum[2]; // each number of nodes of two colors
19+
20+
void add(int a, int b) {
21+
e[idx] = b;
22+
ne[idx] = h[a];
23+
h[a] = idx;
24+
idx += 1;
25+
}
26+
27+
bool dfs(int node, int color) {
28+
if (visited[node]) {
29+
return colors[node] == color;
30+
}
31+
32+
visited[node] = true;
33+
colors[node] = color;
34+
sum[color]++;
35+
36+
for (int i = h[node]; i != -1; i = ne[i]) {
37+
int j = e[i];
38+
if (!dfs(j, 1-color)) return false;
39+
}
40+
return true;
41+
}
42+
43+
/*
44+
int main() {
45+
int n, m;
46+
scanf("%d%d", &n, &m);
47+
memset(h, -1, sizeof h);
48+
49+
int a, b;
50+
for (; m-- > 0;) {
51+
scanf("%d%d", &a, &b);
52+
add(a, b);
53+
add(b, a);
54+
}
55+
56+
int ret = 0;
57+
for (int i = 1; i <= n; i++) {
58+
if (visited[i]) continue;
59+
60+
sum[0] = sum[1] = 0;
61+
if (!dfs(i, 0)) {
62+
printf("Impossible");
63+
return 0;
64+
}
65+
ret += min(sum[0], sum[1]);
66+
}
67+
68+
printf("%d", ret);
69+
return 0;
70+
}
71+
*/

Hash/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
include_directories(.)
3+
4+
add_executable(Hash DuplicateElementsWithinKDistance.cpp)
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// link: https://www.geeksforgeeks.org/check-given-array-contains-duplicate-elements-within-k-distance/
3+
//
4+
5+
#include <iostream>
6+
#include <unordered_set>
7+
#include <vector>
8+
using namespace std;
9+
10+
/*
11+
1) Create an empty hashtable.
12+
2) Traverse all elements from left from right. Let the current element be ‘arr[i]’
13+
.a) If current element ‘arr[i]’ is present in hashtable, then return true.
14+
.b) Else add arr[i] to hash and remove arr[i-k] from hash if i is greater than or equal to k
15+
*/
16+
bool checkDupsWithinK(vector<int>& nums, int k) {
17+
unordered_set<int> set;
18+
for (int i = 0; i < nums.size(); i++) {
19+
// If already present n hash, then we found
20+
// a duplicate within k distance
21+
if (set.find(nums[i]) != set.end())
22+
return true;
23+
24+
// Add this item to hashset
25+
set.insert(nums[i]);
26+
27+
// Remove the k+1 distant item
28+
if (i >= k) set.erase(nums[i-k]);
29+
}
30+
31+
return false;
32+
}
33+
34+
int main () {
35+
vector<int> nums = {10, 5, 3, 4, 3, 5, 6};
36+
if (checkDupsWithinK(nums, 3))
37+
cout << "Yes";
38+
else
39+
cout << "No";
40+
}

Math/AW866TestDivPrime.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// AcWing 866 试除法判定质数
3+
// link: https://www.acwing.com/problem/content/868/
4+
5+
#include <cstring>
6+
#include <algorithm>
7+
8+
using namespace std;
9+
10+
bool isPrime(int n) {
11+
if (n < 2) return false;
12+
13+
// 注意这里循环停止的条件 i <= n / i 不要写成 i*i > n i*i会有溢出的风险
14+
for (int i = 2; i <= n / i; i++) {
15+
if (n % i == 0) return false;
16+
}
17+
return true;
18+
}
19+
20+
int main() {
21+
int n;
22+
scanf("%d", &n);
23+
24+
for (; n > 0; n--) {
25+
int x;
26+
scanf("%d", &x);
27+
if (isPrime(x)) puts("Yes");
28+
else puts("No");
29+
}
30+
31+
return 0;
32+
}

Math/AW867PrimFactors.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// 867. 分解质因数
3+
// link: https://www.acwing.com/problem/content/869/
4+
5+
#include <iostream>
6+
#include <algorithm>
7+
8+
using namespace std;
9+
10+
void divide(int x) {
11+
// 优化: i只需要枚举到x/i
12+
for (int i = 2; i <= x / i; i++) {
13+
if (x % i == 0) { /* 满足条件(x % i == 0)的i 一定是质数
14+
当枚举到i时 [2,i-1]所有的质因子已经被除干净了
15+
x已经不包含任何[2,i-1]内的质因子了
16+
如果i是合数 那么它肯定可以分解成比i小的质数 与假设矛盾
17+
*/
18+
int cnt = 0; // 记录质因子i对应的指数
19+
// 只要i可以整除x 就一直除下去
20+
for (; x % i == 0; ) {
21+
x /= i;
22+
cnt++; // i对应的指数自增
23+
}
24+
cout << i << " " << cnt << endl;
25+
}
26+
}
27+
// 如果x大于1 那么x就是大于sqrt(x)的那个质因子 没有被上面的for loop枚举到 这里特殊处理一下
28+
if (x > 1) cout << x << " " << 1 << endl;
29+
cout << endl;
30+
}
31+
32+
int main() {
33+
int n;
34+
scanf("%d", &n);
35+
for (; n > 0; n--) {
36+
int x;
37+
scanf("%d", &x);
38+
divide(x);
39+
}
40+
41+
return 0;
42+
}
43+

0 commit comments

Comments
 (0)