8.28赛后总结
Chinese_Cant_Fly · · 个人记录
:::::info[A] ::::error[错误思路] 不会写,写了个代码输出0骗分.最后拿到了 20pts。 :::: ::::error[错误代码]
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,c[15],a[15][15];
signed main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>c[i];
for(int j=1;j<=c[i];j++){
cin>>a[i][j];
}
}
cout<<0;
return 0;
}
:::: ::::success[正确思路] 我们可以使用 dfs ,通过进行选与不选的操作来决定回溯还是不回溯,选完之后,循环判断如果有没包含的数字,就直接跳过,否则就将答案加 1,最后输出最终的答案。 :::: ::::success[正确代码]
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,box[15],c[15],a[15][15],vis[15],ans;
void dfs(int x){
if(x==m+1){
for(int i=1;i<=n;i++){
if(vis[i]==0) return ;
}
ans++;
return ;
}
for(int i=1;i<=c[x];i++) vis[a[x][i]]++;
dfs(x+1);
for(int i=1;i<=c[x];i++) vis[a[x][i]]--;
dfs(x+1);
}
signed main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>c[i];
for(int j=1;j<=c[i];j++){
cin>>a[i][j];
}
}
dfs(1);
cout<<ans;
return 0;
}
| :::: |
|---|
| :::::info[B] |
| ::::error[错误思路] |
| 还是不会写,看懂了题目意思,最后直接输出了一个样例。 |
| :::: |
| ::::error[错误代码] |
| ```cpp line-numbers |
| #include<bits/stdc++.h> |
| #define int long long |
| using namespace std; |
| int n,k,a[10],b[10]; |
| signed main(){ |
| cin>>n>>k; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i]; |
| } |
| for(int i=1;i<=k;i++){ |
| cin>>b[i]; |
| } |
| cout<<"1 3 2 4 6 5"; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::success[正确思路] |
| 可以使用全排列来枚举出每一种排列组合的方式,最后每次判断这个排列是否可行,如果可行,就直接输出这个排列的方式,然后结束整个程序,否则,就判断下一次全排列的方式是否可行,如果无法分配,那么就输出 -1。 |
| :::: |
| ::::success[正确代码] |
| ```cpp line-numbers |
| #include<bits/stdc++.h> |
| #define int long long |
| using namespace std; |
| int n,k,a[15],vis[15],b[15],c[15]; |
| void dfs(int x){ |
| if(x==k+1){ |
| for(int i=1;i<=k;i++){ |
| if(a[c[i]]<b[i]){ |
| return ; |
| } |
| } |
| for(int i=1;i<=k;i++){ |
| cout<<c[i]<<' '; |
| } |
| exit(0); |
| } |
| for(int i=1;i<=n;i++){ |
| if(vis[i]==0){ |
| vis[i]=1; |
| c[x]=i; |
| dfs(x+1); |
| vis[i]=0; |
| } |
| } |
| } |
| signed main(){ |
| cin>>n>>k; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i]; |
| } |
| for(int i=1;i<=k;i++){ |
| cin>>b[i]; |
| } |
| dfs(1); |
| cout<<-1; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::: |
| :::::info[C] |
| ::::error[错误思路] |
| 通过获取每一个喷水器的具体坐标 x,y,将其放入队列中后进行 bfs,对于每一个喷水器都要这么做,然后判断每一个地方是否被经过了,如果下一个能到的地方被经过了,那么就换个方向走,否则就继续往前走,同时,如果这个喷水器喷出的水到另一个点的距离已经超过了 d,那么就直接结束这次枚举。最后统计答案就看这个点有没有被经过,有,ans 就加 1,否则跳过。 |
| :::: |
| ::::error[错误代码] |
| ```cpp line-numbers |
| #include<bits/stdc++.h> |
| #define int long long |
| using namespace std; |
| int h,w,d,sx[25005],sy[25005],vis[505][505]; |
| char mp[505][505]; |
| int dx[]={1,-1,0,0}; |
| int dy[]={0,0,1,-1}; |
| struct qwq{ |
| int hx,hy,cnt; |
| }; |
| queue<qwq>q; |
| void bfs(){ |
| while(q.size()){ |
| qwq now=q.front(); |
| q.pop(); |
| for(int i=0;i<4;i++){ |
| int xx=now.hx+dx[i]; |
| int yy=now.hy+dy[i]; |
| if(xx<1 |
| if(mp[xx][yy]!='.' |
| vis[xx][yy]=1; |
| q.push({xx,yy,now.cnt+1}); |
| } |
| } |
| } |
| signed main(){ |
| cin>>h>>w>>d; |
| for(int i=1;i<=h;i++){ |
| for(int j=1;j<=w;j++){ |
| cin>>mp[i][j]; |
| if(mp[i][j]=='H'){ |
| q.push({i,j}); |
| vis[i][j]=1; |
| } |
| } |
| } |
| bfs(); |
| int ans=0; |
| for(int i=1;i<=h;i++){ |
| for(int j=1;j<=w;j++){ |
| if(vis[i][j]==1) ans++; |
| } |
| } |
| cout<<ans; |
| return 0; |
| } |
::::
::::success[正确思路]
可以通过使用 dis 来表示从某个喷水器出发到某个地点所用的最小距离,在 bfs 中使用它来存储以避免一些小错误,最后统计答案就判断如果 dis 的值小于 d 并且这个地点走过,就将 ans+1 即可,最后输出 ans。
::::
::::success[正确代码]
```cpp line-numbers
#include<bits/stdc++.h>
#define int long long
using namespace std;
int h,w,d,sx[25005],sy[25005],vis[505][505],dis[505][505];
char mp[505][505];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
struct qwq{
int hx,hy;
};
queue<qwq>q;
void bfs(){
while(q.size()){
qwq now=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=now.hx+dx[i];
int yy=now.hy+dy[i];
if(xx<1||xx>h||yy<1||yy>w) continue;
if(mp[xx][yy]!='.'||vis[xx][yy]==1) continue;
vis[xx][yy]=1;
dis[xx][yy]=dis[now.hx][now.hy]+1;
q.push({xx,yy});
}
}
}
signed main(){
cin>>h>>w>>d;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
cin>>mp[i][j];
if(mp[i][j]=='H'){
q.push({i,j});
vis[i][j]=1;
}
}
}
bfs();
int ans=0;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(dis[i][j]<=d&&vis[i][j]==1) ans++;
}
}
cout<<ans;
return 0;
}
| :::: |
|---|
| :::::info[D] |
| ::::error[错误原因] |
| 有一部分调试的时候的小错误,改正过来就好了。 |
| :::: |
| ::::success[正确思路] |
| 通过使用两个 bfs 来解决问题,一个 bfs 来判断最开始是纵向移动,一个 bfs 来解决最开始是横向移动,最后将每个 bfs 的最终答案统计下来,判断,如果第一个到了终点且第二个也到了终点,输出两个的最小值,如果第一个到但第二个没到,输出第一个,如果第一个没到但第二个到,输出第二个,否则输出 -1。 |
| :::: |
| ::::success[正确代码] |
| ```cpp line-numbers |
| #include<bits/stdc++.h> |
| using namespace std; |
| int H,W,sx,sy,fx,fy,vis1[1005][1005],vis2[1005][1005],ans1,ans2; |
| int dx[]={-1,1,0,0}; |
| int dy[]={0,0,-1,1}; |
| char mp[1005][1005]; |
| struct qwq{ |
| int x,y,st,to; |
| }; |
| void bfs1(){ |
| queue<qwq>q; |
| q.push({sx,sy,0,0});//0表示纵向移动,1表示横向移动 |
| vis1[sx][sy]=1; |
| while(q.size()){ |
| qwq now=q.front(); |
| q.pop(); |
| if(now.x==fx&&now.y==fy){ |
| ans1=now.st; |
| return ; |
| } |
| if(now.to==0){ |
| for(int i=0;i<2;i++){ |
| int xx=now.x+dx[i]; |
| int yy=now.y+dy[i]; |
| if(xx<1 |
| if(mp[xx][yy]=='#' |
| vis1[xx][yy]=1; |
| q.push({xx,yy,now.st+1,1}); |
| } |
| } |
| else{ |
| for(int i=2;i<4;i++){ |
| int xx=now.x+dx[i]; |
| int yy=now.y+dy[i]; |
| if(xx<1 |
| if(mp[xx][yy]=='#' |
| vis1[xx][yy]=1; |
| q.push({xx,yy,now.st+1,0}); |
| } |
| } |
| } |
| } |
| void bfs2(){ |
| queue<qwq>q; |
| q.push({sx,sy,0,1});//0表示纵向移动,1表示横向移动 |
| vis2[sx][sy]=1; |
| while(q.size()){ |
| qwq now=q.front(); |
| q.pop(); |
| if(now.x==fx&&now.y==fy){ |
| ans2=now.st; |
| return ; |
| } |
| if(now.to==0){ |
| for(int i=0;i<2;i++){ |
| int xx=now.x+dx[i]; |
| int yy=now.y+dy[i]; |
| if(xx<1 |
| if(mp[xx][yy]=='#' |
| vis2[xx][yy]=1; |
| q.push({xx,yy,now.st+1,1}); |
| } |
| } |
| else{ |
| for(int i=2;i<4;i++){ |
| int xx=now.x+dx[i]; |
| int yy=now.y+dy[i]; |
| if(xx<1 |
| if(mp[xx][yy]=='#' |
| vis2[xx][yy]=1; |
| q.push({xx,yy,now.st+1,0}); |
| } |
| } |
| } |
| } |
| int main(){ |
| cin>>H>>W; |
| for(int i=1;i<=H;i++){ |
| for(int j=1;j<=W;j++){ |
| cin>>mp[i][j]; |
| if(mp[i][j]=='S'){ |
| sx=i; |
| sy=j; |
| } |
| if(mp[i][j]=='G'){ |
| fx=i; |
| fy=j; |
| } |
| } |
| } |
| bfs1(); |
| bfs2(); |
| if(vis1[fx][fy]!=1&&vis2[fx][fy]!=1){ |
| cout<<-1; |
| } |
| else if(vis1[fx][fy]==1&&vis2[fx][fy]!=1){ |
| cout<<ans1; |
| } |
| else if(vis1[fx][fy]!=1&&vis2[fx][fy]==1){ |
| cout<<ans2; |
| } |
| else if(vis1[fx][fy]==1&&vis2[fx][fy]==1){ |
| cout<<min(ans1,ans2); |
| } |
| return 0; |
| } |
| ``` |
| :::: |
|---|