复盘2.
15.bfs
1)从(x1,y1)到(x2,y2)最短距离
#include
using namespace std;
const int N=3e4+5;
const int inf=0x3f3f3f3f;
#define int long long
int a[110][110],b[110][110];
int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
typedef struct{
int x,y,step;
}row;
queuequ;
void solve()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
int sx,sy,p,q;
cin>>sx>>sy>>p>>q;
row r;
r.x=sx,r.y=sy,r.step=0;
qu.push(r);
b[sx][sy]=1;
while(qu.size())
{
int x,y;
x=qu.front().x,y=qu.front().y;
if(x==p&&y==q)
{
cout<
2)1002-走出迷宫_2021秋季算法入门班第六章习题:搜索与搜索剪枝 (nowcoder.com)
#include
using namespace std;
const int N=510;
const int inf=0x3f3f3f3f;
#define int long long
string sr[N];
int n,m;
int b[N][N];
typedef struct pair pii;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
#define check(i,j) (i>=0&&i=0&&jqu;
qu.push({i,j});
b[i][j]=1;
while(qu.size())
{
int x=qu.front().first;
int y=qu.front().second;
if(sr[x][y]=='E')
{
cout<<"Yes"<>n>>m)
{
for(int i=0;i>sr[i];
for(int i=0;i
3)D-剪纸游戏_牛客小白月赛86 (nowcoder.com)
#include
using namespace std;
const int N=1100;
const int inf=0x3f3f3f3f;
#define int long long
int n,m;
string sr[N];
typedef pair pii;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int ans=0;
setse1,se2;//长,宽
#define check(i,j) (i>=0&&i=0&&jqu;
qu.push({i,j});
int cn=0;
while(qu.size())
{
int x=qu.front().first,y=qu.front().second;
qu.pop();
if(sr[x][y]=='*')continue;
sr[x][y]='*';
se1.insert(x);
se2.insert(y);
cn++;
for(int i=0;i<4;i++)
{
int px=x+dir[i][0],py=y+dir[i][1];
if(check(px,py))
{
qu.push({px,py});
}
}
}
if(se1.size()*se2.size()==cn)
{
ans++;
}
}
void solve()
{
cin>>n>>m;
for(int i=0;i>sr[i];
for(int i=0;i
4)1003-模拟战役_2021秋季算法入门班第六章习题:搜索与搜索剪枝 (nowcoder.com)
#include
using namespace std;
const int N=3e4+5;
const int inf=0x3f3f3f3f;
#define int long long
int n;
string str1[4],str2[4];
int dir[8][2]={{1,0},{0,1},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
#define check1(i,j) (i>=0&&i<4&&j>=0&&j=0&&i<4&&j>=0&&j pii;
vectorve1,ve2;
void bfs1(int i,int j)
{
queuequ;
qu.push({i,j});
int cn=1;
while(qu.size())
{
int x=qu.front().first,y=qu.front().second;
qu.pop();
for(int i=0;i<8;i++)
{
int px=x+dir[i][0],py=y+dir[i][1];
if(check1(px,py))
{
str1[px][py]='.';
qu.push({px,py});
cn++;
}
}
}
ve1.push_back(cn);
}
void bfs2(int i,int j)
{
queuequ;
qu.push({i,j});
int cn=1;
while(qu.size())
{
int x=qu.front().first,y=qu.front().second;
qu.pop();
for(int i=0;i<8;i++)
{
int px=x+dir[i][0],py=y+dir[i][1];
if(check2(px,py))
{
str2[px][py]='.';
qu.push({px,py});
cn++;
}
}
}
ve2.push_back(cn);
}
void solve()
{
cin>>n;
for(int i=0;i<4;i++)cin>>str1[i];
for(int i=0;i<4;i++)cin>>str2[i];
for(int i=0;i<4;i++)
{
for(int j=0;jve2.size())cout<<-1;
else
{
int sum=0;
for(int i=ve2.size()-1;i>=0&&i>=ve1.size()-1;i--)
{
sum+=ve2[i];
}
cout<
5)蓝桥杯2022年第十三届省赛真题-扫雷 - C语言网 (dotcpp.com)
╥﹏╥... ,,,,,,,时间还超限,,(((φ(◎ロ◎;)φ))),,╥﹏╥...
*1*(85/100)
#include
using namespace std;
const int N=3e4+5;
const int inf=0x3f3f3f3f;
#define int long long
int n,m,cn=0;
struct node{
int x,y,r;
}no[N];
int mp(int x1,int y1,int x2,int y2,int d)
{
if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<=d*d)return 1;
return 0;
}
void bfs(int i,int j,int u)
{
queuequ;
qu.push({i,j,u});
while(qu.size())
{
int dx=qu.front().x;
int dy=qu.front().y;
int dr=qu.front().r;
qu.pop();
for(int i=0;i>n>>m;//炸雷,排雷火箭
for(int i=0;i>no[i].x>>no[i].y>>no[i].r;
}
for(int i=0;i>o>>p>>q;
bfs(o,p,q);
}
cout<
*2*(85/100)
#include
using namespace std;
const int N=3e4+5;
const int inf=0x3f3f3f3f;
#define int long long
int n,m,cn=0;
struct node{
int x,y,r;
}no[N];
int mp(int x1,int y1,int x2,int y2,int d)
{
if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<=d*d)return 1;
return 0;
}
void solve()
{
queuequ;
cin>>n>>m;//炸雷,排雷火箭
for(int i=0;i>no[i].x>>no[i].y>>no[i].r;
}
for(int i=0;i>o>>p>>q;
qu.push({o,p,q});
}
while(qu.size())
{
int dx=qu.front().x,dy=qu.front().y,dr=qu.front().r;
qu.pop();
for(int i=0;i
16.dp
多重背包用二进制转换,最后可以当成01背包解决
01背包最后j的for循环是从V到v[i],完全背包是v[i]到V
1)5. 多重背包问题 II - AcWing题库
#include
using namespace std;
const int N=3e4+5;
const int inf=0x3f3f3f3f;
#define int long long
int w[N],v[N],s[N];
void solve()
{
int n,V,ans=0;
int p,q,r;
cin>>n>>V;
while(n--)
{
int k=1;
cin>>p>>q>>r;//体积,价值,数量
while(k<=r)
{
ans++;
v[ans]=p*k;
w[ans]=q*k;
r-=k;
k*=2;
}
if(r>0)
{
ans++;
v[ans]=p*r;
w[ans]=q*r;
}
}
for(int i=1;i<=ans;i++)
{
for(int j=V;j>=v[i];j--)
{
s[j]=max(s[j],s[j-v[i]]+w[i]);
}
}
cout<
2)7. 混合背包问题 - AcWing题库
#include
using namespace std;
const int N=3e4+5;
const int inf=0x3f3f3f3f;
#define int long long
int v[N],w[N],s[N];
void solve()
{
int n,V,p,q,r,ans=0;
cin>>n>>V;
while(n--)
{
int k=1;
cin>>p>>q>>r;
if(r==-1)r=1;
else if(r==0)r=V/p;
while(k<=r)
{
ans++;
v[ans]=p*k;
w[ans]=q*k;
r-=k;
k*=2;
}
if(r>0)
{
ans++;
v[ans]=p*r;
w[ans]=q*r;
}
}
for(int i=1;i<=ans;i++)
{
for(int j=V;j>=v[i];j--)
{
s[j]=max(s[j],s[j-v[i]]+w[i]);
}
}
cout<
17.快速幂
G-夜雷の史莱姆农场_浙江机电职业技术学院第八届新生亮相赛(同步赛) (nowcoder.com)
#include
using namespace std;
const int N=3e4+5;
const int inf=0x3f3f3f3f;
#define int long long
int fast(int a,int b,int mod)
{
int result=1;
while(b)
{
if(b&1)
{
result=result*a%mod;
a=a*a%mod;
b/=2;
}
else
{
a=a*a%mod;
b/=2;
}
}
return result;
}
void solve()
{
int t;
cin>>t;
while(t--)
{
int a,b,p;
cin>>a>>b>>p;
cout<
18.蓝桥杯2022年第十三届省赛真题-统计子矩阵 - C语言网 (dotcpp.com)
using namespace std;
#define int long long
const int N = 510;
const int inf = 0x3f3f3f3f;
int a[N][N], b[N][N];
void solve() {
int n, m, k, cn = 0;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
b[i][j] = b[i][j - 1] + a[i][j];
}
}
for (int j1 = 1; j1 <= m; j1++) {
for (int j2 = j1; j2 <= m; j2++) {
int j = 1;
int sum = b[1][j2] - b[1][j1 - 1];
for (int i = 1; i <= n; i++) {
while (j <= n && sum <= k) {
j++;
sum += b[j][j2] - b[j][j1 - 1];
}
cn += j - i;
sum -= b[i][j2] - b[i][j1 - 1];
}
}
}
cout << cn;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}