P1028 [NOIP2001 普及组] 数的计算
解法一
C++#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, num[1000005];
inline int dfs(int x) {
if(x == 1) {
return 1;
} else if(num[x] != 0) {
return num[x];
} else {
num[x] = 1;
for(int i = 1 ; i <= x / 2 ; i ++) {
num[x] += dfs(i);
}
}
return num[x];
}
signed main() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
cin>>n;
cout<<dfs(n);
return 0;
}
P3958 [NOIP2017 提高组] 奶酪
解法一
C++#include<bits/stdc++.h>
#define int long long
using namespace std;
int t, n, h, r, temp, flag;
bool ander[100005];
struct point {
int x, y, z;
}ball[100005];
queue<int> bfs;
inline double dis(int a, int b) {
return sqrt((ball[a].x - ball[b].x) * (ball[a].x - ball[b].x) + (ball[a].y - ball[b].y) * (ball[a].y - ball[b].y) + (ball[a].z - ball[b].z) * (ball[a].z - ball[b].z));
}
signed main() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
cin>>t;
while(t --) {
memset(ander, false, sizeof(ander));
flag = 0;
cin>>n>>h>>r;
for(int i = 1 ; i <= n ; i ++) {
cin>>ball[i].x>>ball[i].y>>ball[i].z;
if(ball[i].z + r >= h) {
bfs.push(i);
ander[i] = true;
}
}
while(!bfs.empty()) {
temp = bfs.front(), bfs.pop();
if(ball[temp].z - r <= 0){
cout<<"Yes"<<endl, flag = 1;
break;
}
for(int i = 1 ; i <= n ; i ++) {
if(i != temp && !ander[i] && dis(i, temp) <= r * 2) {
bfs.push(i), ander[i] = true;
}
}
}
if(flag == 0) {
cout<<"No"<<endl;
}
while(!bfs.empty()){
bfs.pop();
}
}
return 0;
}
P1141 01迷宫
解法一
C++#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node {
int x, y;
};
int n, m, a, b, dis[5][5] = {{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int book[1005][1005], vis[1005][1005];
char ch;
int bfs(int x, int y) {
int ans = 1;
queue<node> qu, qu2;
qu.push({x, y}), qu2.push({x, y});
while(!qu.empty()) {
node temp = qu.front();
qu.pop();
for(int i = 1, dx, dy ; i <= 4; i ++) {
dx = temp.x + dis[i][0], dy = temp.y + dis[i][1];
if(dx >= 1 && dx <= n && dy >= 1 && dy <= n && !vis[dx][dy] && book[dx][dy] != book[temp.x][temp.y]) {
ans++, vis[dx][dy] = 1, qu.push({dx, dy}), qu2.push({dx, dy});
}
}
}
while(!qu2.empty()) {
vis[qu2.front().x][qu2.front().y] = ans;
qu2.pop();
}
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i = 1 ; i <= n ; i ++) {
for(int j = 1 ; j <= n ; j ++) {
cin>>ch, book[i][j] = (ch == '1');
}
}
for(int i = 1 ; i <= m ; i ++) {
cin>>a>>b;
if(vis[a][b]) {
cout<<vis[a][b]<<endl;
} else {
vis[a][b] = 1, cout<<bfs(a, b)<<endl;
}
}
return 0;
}