T1
TLE代码:
#include<bits/stdc++.h>
using namespace std;
struct st {
int x, y, s;
};
queue<st> q;
char a[2001][2001];
bool vis[2001][2001];
int nx[] = {0, 0, 1, -1};
int ny[] = {1, -1, 0, 0};
int n, m;
int flag = 0;
void bfs(){
while(!q.empty()){
st ch = q.front();
q.pop();
vis[ch.x][ch.y] = 1;
if(a[ch.x][ch.y] == 'd'){
cout << ch.s;
flag = 1;
return;
}
for(int i = 0; i < 4; i++){
int hx = ch.x + nx[i];
int hy = ch.y + ny[i];
if(hx < 1 || hx > n || hy < 1 || hy > m) continue;
if(a[hx][hy] == '#') continue;
if(vis[hx][hy]) continue;
q.push({hx, hy, ch.s + 1});
}
}
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i][j];
if(a[i][j] == 'm') {
q.push({i, j, 0});
}
}
}
bfs();
if(!flag) cout << "No Way!";
return 0;
}AC代码:
#include<bits/stdc++.h>
using namespace std;
struct st {
int x, y, s;
};
queue<st> q;
char a[2001][2001];
bool vis[2001][2001];
int nx[] = {0, 0, 1, -1};
int ny[] = {1, -1, 0, 0};
int n, m;
int flag = 0;
void bfs(){
while(!q.empty()){
st ch = q.front();
q.pop();
if(vis[ch.x][ch.y]) continue; // 关键在于提上来,以前是在for里
vis[ch.x][ch.y] = 1;
if(a[ch.x][ch.y] == 'd'){
cout << ch.s;
flag = 1;
return;
}
for(int i = 0; i < 4; i++){
int hx = ch.x + nx[i];
int hy = ch.y + ny[i];
if(hx < 1 || hx > n || hy < 1 || hy > m) continue;
if(a[hx][hy] == '#') continue;
q.push({hx, hy, ch.s + 1});
}
}
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i][j];
if(a[i][j] == 'm') {
q.push({i, j, 0});
}
}
}
bfs();
if(!flag) cout << "No Way!";
return 0;
}T7
AC思路: 使用并查集串联各个洞,判断:距离的平方 >= 4 * r * r 用祖先的数组登记是否连接上底和下底。 判断是否有至少一个的洞串连接上底和下底,是就输出“Yes”,否则输出“No” AC代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct st{
int x,y,z;
}a[10001];
int vis1[10010],vis2[10001];
int vvis1[10001],vvis2[10001];
int fa[10001];
int find(int x){
if(fa[x] == x)return x;
return fa[x] = find(fa[x]);
}
int su(st u,st v){
return (u.x - v.x) * (u.x - v.x) + (u.y - v.y) * (u.y - v.y) + (u.z - v.z) * (u.z - v.z);
}
signed main(){
int t;
cin >> t;
while(t--){
int n,h,r;
cin >>n>>h>>r;
for(int i = 1;i <= n;i++){
fa[i] = i;
vis1[i] = vis2[i] = vvis1[i] = vvis2[i] = 0;
}
for(int i = 1;i <= n;i++){
cin >> a[i].x>>a[i].y>>a[i].z;
vis1[i] = (a[i].z <= r);
vis2[i] = (a[i].z + r >= h);
}
for(int i = 1;i <= n;i++){
for(int j = i + 1;j <= n;j++){
if(find(i) != find(j)){
if(su(a[i],a[j]) <= 4 * r * r){
fa[find(i)] = find(j);
}
}
}
}
int flag = 0;
for(int i = 1;i <= n;i++){
if(vis1[i]) vvis1[find(i)] = 1;
if(vis2[i]) vvis2[find(i)] = 1;
}
for(int i = 1;i <= n;i++){
if(fa[i] == i){
if(vvis1[i] && vvis2[i]){
flag = 1;
break;
}
}
}
if(!flag)
cout << "No\n";
else
cout << "Yes\n";
}
}