本周主要学了bfs
简单的说,bfs就是用一个队列和结构体数组,或者多个数组,用队列对查找目标进行遍历,用其他数据结构对查找目标进行标记;
洛谷 P1331 海战
#include<bits/stdc++.h>
using namespace std;
using ll= long long;
constexpr int MOD=1000000007;
char c[1005][1005];
deque<pair<int,int>> dq;
int n,m,ans;
bool used[1005][1005];//是否走过
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};//移动方向
bool check(int i,int j){
int ok=0;
if(c[i][j]=='#')ok++;
if(c[i+1][j]=='#')ok++;
if(c[i][j+1]=='#')ok++;
if(c[i+1][j+1]=='#')ok++;
if(ok==3)return false;
return true;
}
void bfs(int a,int b){
dq.push_back({a, b});
while(!dq.empty()){
auto [x,y]=dq.front();
for(int i{0};i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if((!used[xx][yy])&&c[xx][yy]=='#'){
dq.push_back({xx, yy});
used[xx][yy]=true;
//auto [x,y]=dq.front();
//cout<<x<<" "<<y<<"\n";
}
}
dq.pop_front();
}
}
int main(){
cin>>n>>m;
for(int i{0};i<n;i++){
for(int j{0};j<m;j++)cin>>c[i][j];
}
for(int i{0};i<n-1;i++){
for(int j{0};j<m-1;j++){
if(!check(i,j)){
cout<<"Bad placement.";
return 0;
}
}
}
for(int i{0};i<n;i++){
for(int j{0};j<m;j++){
if(c[i][j]=='#'&&(!used[i][j])){
ans++;used[i][j]=true;
bfs(i,j);
}
}
}
cout<<"There are "<<ans<<" ships.\n";
}
洛谷 B3625 迷宫寻路
#include<bits/stdc++.h>
using namespace std;
using ll= long long;
constexpr int MOD=1000000007;
char c[1005][1005];
deque<pair<int,int>> dq;
int n,m,ans;
bool used[1005][1005];//是否走过
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};//移动方向
void bfs(int a,int b){
dq.push_back({a, b});
while(!dq.empty()){
auto [x,y]=dq.front();
for(int i{0};i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if((!used[xx][yy])&&c[xx][yy]=='.'){
dq.push_back({xx, yy});
used[xx][yy]=true;
//auto [x,y]=dq.front();
//cout<<x<<" "<<y<<"\n";
}
}
dq.pop_front();
}
}
int main(){
cin>>n>>m;
for(int i{0};i<n;i++){
for(int j{0};j<m;j++)cin>>c[i][j];
}
bfs(0,0);
//for(int i{0};i<n;i++){
// for(int j{0};j<m;j++){
// if(c[i][j]=='.'&&(!used[i][j])){
// ans++;used[i][j]=true;
// bfs(i,j);
// }
// }
//}
if(used[n-1][m-1]){cout<<"Yes";}
else cout<<"No";
}
Wiki下象棋
题目链接:https://ac.nowcoder.com/acm/contest/74679/E
bfs搜两下就行了
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int fx[8] = {-1, 1, 0, 0, -1, 1, -1, 1};
int fy[8] = {0, 0, -1, 1, -1, -1, 1, 1};
const int mod = 1000000007;
const int N = 200005;
const double eps = 1e-6;
int dx[8] = {1, 1, -1, -1, 2, 2, -2, -2};
int dy[8] = {2, -2, 2, -2, 1, -1, 1, -1};
int bx[8] = {0, 0, 0, 0, 1, 1, -1, -1};
int by[8] = {1, -1, 1, -1, 0, 0, 0, 0};
int n, m;
bool vis[305][305], board[305][305];
int bfs1(int sx, int sy, int ex, int ey) {
memcpy(vis, board, sizeof(vis));
queue<pair<int, int> > q;
q.push(make_pair(sx, sy));
int cnt = 1, step = 0;
vis[sx][sy] = true;
while (!q.empty()) {
int sum = 0;
while (cnt--) {
auto [x, y] = q.front();
q.pop();
if (x == ex && y == ey) return step;
for (int i = 0; i < 8; ++i) {
int fx = x + dx[i], fy = y + dy[i];
if (fx < 1 || fy < 1 || fx > n || fy > m || vis[fx][fy]) continue;
q.push(make_pair(fx, fy));
vis[fx][fy] = true;
++sum;
}
}
++step;
cnt = sum;
}
return -1;
}
int bfs2(int sx, int sy, int ex, int ey) {
memcpy(vis, board, sizeof(vis));
queue<pair<int, int> > q;
q.push(make_pair(sx, sy));
int cnt = 1, step = 0;
vis[sx][sy] = true;
while (!q.empty()) {
int sum = 0;
while (cnt--) {
auto [x, y] = q.front();
q.pop();
if (x == ex && y == ey) return step;
for (int i = 0; i < 8; ++i) {
int fx = x + dx[i], fy = y + dy[i];
if (fx < 1 || fy < 1 || fx > n || fy > m || vis[fx][fy] ||
board[x + bx[i]][y + by[i]])
continue;
q.push(make_pair(fx, fy));
vis[fx][fy] = true;
++sum;
}
}
++step;
cnt = sum;
}
return -1;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
cin >> T;
while (T--) {
cin >> n >> m;
int k, sx, sy, ex, ey;
cin >> k >> sx >> sy >> ex >> ey;
memset(board, false, sizeof(board));
for (int i = 1; i <= k; i++) {
int x, y;
cin >> x >> y;
board[x][y] = true;
}
cout << bfs1(sx, sy, ex, ey) << " " << bfs2(sx, sy, ex, ey) << endl;
}
return 0;
}
学了一些python,主要是使用Decimal库对高精度浮点数进行操作;
学历一些现代c++,union类似于c的struct,但是内存管理上更优秀(打比赛没啥用);
浙公网安备 33010602011771号