7.6总结

· · 闲话

T1 P1238 走迷宫

思路:在(sx,sy)dfs,边走边保存路径,到(ex,ey)后输出保存的路径。

还算简单,但我在第一次写的时候把(sx,sy)写成了(1,1)。。。眼瞎 :(

这里解释一下 print 函数,此函数是一个递归函数,通过递归的方式输出答案。但是,递归是反向输出的啊。。。但是没事,嘿嘿嘿

Code:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=20+10;
int n,b[N],k,v[N][N],m,a[N][N],sx,sy,ex,ey,f;
int fx[]={0,0,-1,0,1};
int fy[]={0,-1,0,1,0};
struct node{
    int x,y;
}pre[N][N];
void print(int x,int y){
    if(x==sx&&y==sy){
        cout<<'('<<sx<<','<<sy<<')';
        return;
    }
    print(pre[x][y].x,pre[x][y].y);
    cout<<"->("<<x<<','<<y<<')';
}
void dfs(int x,int y){
    if(x==ex&&y==ey){
        print(x,y);
        cout<<'\n';
        f=1;
        return;
    }
    for(int i=1;i<=4;i++){
        int tx=x+fx[i];
        int ty=y+fy[i];
        if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&v[tx][ty]==0&&a[tx][ty]==1){
            v[tx][ty]=1;
            pre[tx][ty]={x,y};
            dfs(tx,ty);
            v[tx][ty]=0;
        }
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    cin>>sx>>sy>>ex>>ey;
    if(a[sx][sy]==0||a[ex][ey]==0){
        cout<<-1;
        return 0;
    }
    v[sx][sy]=1;
    dfs(sx,sy);
    if(!f){
        cout<<-1;
        return 0;
    }
    return 0;
}

T2 P1036 [NOIP 2002 普及组] 选数

不会。

T3 P1479 宿舍里的故事之五子棋

这check我就打了10分钟,还错了。。。还是老师大大帮我发现的 o(╥﹏╥)o

这题是个典型的dfs选与不选题,精髓就在下面 ( 解释看注释 ) :

    if(y==5) dfs(x+1,1,cnt);//当这一行遍历完后,跳至下一行 (不选!) 
    else dfs(x,y+1,cnt);//否则去下一个数字
    a[x][y]=1;//标记走过
    if(y==5)dfs(x+1,1,cnt+1);//当这一行遍历完后,跳至下一行,并将cnt++,即再下一颗棋子 (选!) 
    else dfs(x,y+1,cnt+1);//否则去下一个数字 (也选!) 
    a[x][y]=0;//回溯

Code:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=25+10;
int n,a[N][N],t[N],ans;
void check(){
    int cnt=0;
    for(int i=1;i<=5;i++){
        if(a[1][i]&&a[2][i]&&a[3][i]&&a[4][i]&&a[5][i])cnt++;
        if(a[i][1]&&a[i][2]&&a[i][3]&&a[i][4]&&a[i][5])cnt++;
    }
    if(a[1][1]&&a[2][2]&&a[3][3]&&a[4][4]&&a[5][5])cnt++;
    if(a[1][5]&&a[2][4]&&a[3][3]&&a[4][2]&&a[5][1])cnt++;
    t[cnt]=1;
}
void dfs(int x,int y,int cnt){
    if(x>5||cnt==n){
        if(cnt==n)check();
        return;
    }
    if(y==5) dfs(x+1,1,cnt);
    else dfs(x,y+1,cnt);
    a[x][y]=1;
    if(y==5)dfs(x+1,1,cnt+1);
    else dfs(x,y+1,cnt+1);
    a[x][y]=0;
    return;
}
int main(){
    ios::sync_with_stdio(0);
    cin>>n;
    dfs(1,1,0);
    for(int i=1;i<=12;i++){
        if(t[i]){
            ans+=i;
        }
    }
    cout<<ans;
    return 0;
}

T4 P1123 取数游戏

第一次写就AC了哇哈哈

思路:和上一题差不多,改一下 check 函数和主函数就行。

注意:选的时候要在能取数的时候才能进行!!!

还有,maxn记得归零!

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=6+10;
int t,n,m,a[N][N],maxn,v[N][N];
bool check(int x,int y){
    if(v[x-1][y-1]||v[x-1][y]||v[x-1][y+1]||v[x][y+1]||v[x+1][y+1]||v[x+1][y]||v[x+1][y-1]||v[x][y-1])return 0;
    return 1;
}
void dfs(int x,int y,int ans){
    if(x>n){
        maxn=max(maxn,ans);
        return;
    }
    if(y==m)dfs(x+1,1,ans);
    else dfs(x,y+1,ans);
    if(check(x,y)){
        v[x][y]=1;
        if(y==m)dfs(x+1,1,ans+a[x][y]);
        else dfs(x,y+1,ans+a[x][y]);
        v[x][y]=0;
    }
    return;
}
int main(){
    ios::sync_with_stdio(0);
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>a[i][j];
            }
        }
        dfs(1,1,0);
        cout<<maxn<<'\n';
        maxn=0;
    }
    return 0;
}

T5 T627328 简易版-齐国芒果的矩阵

不知道怎么截肢呜呜呜

齐国的芒果成精了?还要摆矩阵。。。

为什么每次写 check 都要写一大堆啊啊啊

思路:和前两题差不多,都是选和不选,只不过其余部分不一样

Code:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=8+10;
int t,n,cnt,a[N][N],sum[N];
bool check(){
//  for(int i=1;i<=n;i++){
//      for(int j=1;j<=n;j++){
//          cout << a[i][j]<<" ";
//      }
//      cout << endl;
//  }
    for(int i=1;i<=n;i++){
        if(sum[i]!=2||a[i][i]!=0)return 0;
        for(int j=1;j<=n;j++){
            if(a[i][j]!=a[j][i])return 0;
        }
    }
    return 1;
}
void dfs(int x,int y){
    if(x>n){
        if(check())cnt++;
        return;
    }
    for(int i=0;i<=2;i++){
        //if(sum[x]+i>2)break;
        a[x][y]=i;
        sum[x]+=i;
        if(y==n) dfs(x+1,1);
        else dfs(x,y+1);
        sum[x]-=i;
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin>>t;
    while(t--){
        cin>>n;
        memset(sum, 0, sizeof sum);
        dfs(1,1);
        cout<<cnt<<'\n';
        cnt=0;
    }
    return 0;
}

老师你看一下文章分类 :D 是不是非常合理