《啊哈!算法》第四章代码下

唉,真抱歉,这次拖了几天,小啾沉迷哈利波特里面少爷的颜出不来啦!完全不思进取了,真可怕,这就是美少年的魅力吗!?
好了,继续继续。
4.5 宝岛探险
这里就只写最后一种情况的代码啦~

static int a[][]=new int[50][50];
    static int book[][]=new int[50][50];
    static int n,m;
    public void ExploreIsland(int x,int y,int color){
        int nx,ny;
        int[][] k={{1,0},
                {0,1},
                {-1,0},
                {0,-1}};
        a[x][y]=color;
        for(int i=0;i<=3;i++){
            nx=x+k[i][0];
            ny=y+k[i][1];
            if(nx<1||nx>m||ny<1||ny>n){
                continue;
            }
            if(a[nx][ny]>0&&book[nx][ny]==0){
                book[nx][ny]=1;
                ExploreIsland(nx,ny,color);
            }
        }
        return;
    }
    public static void main(String[] args){
        algorithm test=new algorithm();
        Scanner sc=new Scanner(System.in);
        int num=0;
        System.out.println("请输入地图的行列长:");
        m=sc.nextInt();
        n=sc.nextInt();
        System.out.println("请输入地图:");
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++)
                a[i][j]=sc.nextInt();
        }
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++)
                book[i][j]=0;
            }
        for(int i=1;i<=m;i++){
             for(int j=1;j<=n;j++){
                 if(a[i][j]>0&&book[i][j]==0){
                     num--;
                     book[i][j]=1;
                     test.ExploreIsland(i,j,num);
                 }
             }
             }
        for(int i=1;i<=m;i++) {
            for (int j = 1; j <= n; j++) {
                System.out.print(String.format("%3d",a[i][j]));
            }
            System.out.println(" ");
        }
}
/*
1 2 1 0 0 0 0 0 2 3
3 0 2 0 1 2 1 0 1 2
4 0 1 0 1 2 3 2 0 1
3 2 0 0 0 1 2 4 0 0
0 0 0 0 0 0 1 5 3 0
0 1 2 1 0 1 5 4 3 0
0 1 2 3 1 3 6 2 1 0
0 0 3 4 8 9 7 5 0 0
0 0 0 3 7 8 6 0 1 2
0 0 0 0 0 0 0 0 1 0
 */

最后一节了,胜利就在眼前!
4.6 水管工游戏

static int m,n,flag=0,top=0;
    static int[][] pipe=new int[50][50];
    static int[][] book=new int[50][50];
    class node{
        int x;
        int y;
    }
    static node[] s=new node[50];
    public void pipegame(int x,int y,int dirct){
        if(x==m&&y==n+1){
            flag=1;
            for(int i=1;i<=top;i++)
                System.out.print("("+s[i].x+","+s[i].y+") ");
            return;
        }
        if(x<1||x>m||y<1||y>n)
            return;
        if(book[x][y]==1)
            return;
        book[x][y]=1;
        top++;
        s[top]=new node();
        s[top].x=x;
        s[top].y=y;
        if(pipe[x][y]==5||pipe[x][y]==6){
            if(dirct==1)
                pipegame(x,y+1,1);
            if(dirct==2)
                pipegame(x+1,y,2);
            if(dirct==3)
                pipegame(x,y-1,3);
            if(dirct==4)
                pipegame(x-1,y,4);
        }
        if(pipe[x][y]>=1&&pipe[x][y]<=4){
            if(dirct==1){
                pipegame(x+1,y,2);
                pipegame(x-1,y,4);
            }
            if(dirct==2){
                pipegame(x,y+1,1);
                pipegame(x,y-1,3);
            }
            if(dirct==3){
                pipegame(x+1,y,2);
                pipegame(x-1,y,4);
            }
            if(dirct==4){
                pipegame(x,y+1,1);
                pipegame(x,y-1,3);
            }
        }
        book[x][y]=0;
        top--;
        return;
    }
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        algorithm test=new algorithm();
        System.out.println("请输入地图的行宽列宽:");
        m=sc.nextInt();
        n=sc.nextInt();
        System.out.println("请输入水管地图:");
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++)
                pipe[i][j]=sc.nextInt();
        }
        for(int i=1;i<=m;i++) {
            for (int j = 1; j <= n; j++)
                book[i][j] = 0;
        }
        test.pipegame(1,1,1);
        if(flag==0)
            System.out.println("方案不存在。");
    }
    /*
5 3 5 3
1 5 3 0
2 3 5 1
6 1 1 5
1 5 5 4
 */

其实这个例子还是不难理解的,把水管类型作为数组内容,方向作为函数参数,一次函数的运行只能影响一步水管的选择。这个的最小解想了又想还是不会做(才菜了鞠躬),有没有人指点一下呀~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值