Java P2196 挖地雷(附数组拷贝)

63 篇文章 1 订阅

题目链接
dfs和回溯题目。

通过这个题还学学会了数组的整体赋值。

        int[] a={1,2,3};
    	int[] b=new int[3];
    	b=a;
    	a[0]=a[1]=a[2]=0;

本来我是这样赋值的,直接用=把一个数组赋值给另一个数组,可是结果总是不对,查阅后才知道,这只是赋给b了a地址,a改变b也会改变,不能实现对当前数据的拷贝。
最方便的是用

b=(int[])a.clone();

这样就达到目的了。(强转不能省)

import java.util.*;
import java.math.*;
public class Main {
	public static int[][] is;
	public static int r,res=0,max=0;
	public static int[] nums,route,ans;
    public static void main(String[] args){
    	Scanner sc=new Scanner(System.in);
    	r=sc.nextInt();
    	nums=new int[r];
    	route=new int[r];
    	ans=new int[r];
    	is=new int[r][r];
    	for(int i=0;i<r;i++)
    		nums[i]=sc.nextInt();
    	for(int i=0;i<r-1;i++){
    		for(int j=i+1;j<r;j++){
    			is[i][j]=sc.nextInt();
    		}
    	}
    	
        int[] rou=new int[r];
        for(int i=0;i<r;i++){
        	Arrays.fill(route,-1);
        	res=nums[i];
        	rou[i]=1;
        	route[0]=i;
        	dfs(i,rou,1);
        	Arrays.fill(rou,0);
        	
        }
        for(int i=0;i<r;i++)
        	if(ans[i]!=-1)
        		System.out.print(ans[i]+1+" ");
        System.out.println();
        System.out.println(max);
    }
    public static void dfs(int cur,int[] rou,int k){  //k是第几层
    	int i,j=0;
    	for(i=0;i<r;i++){
    		if(is[cur][i]!=0&&rou[i]==0){
    			rou[i]=1;
    			res+=nums[i];
    			route[k]=i;
    			dfs(i,rou,k+1);
    			res-=nums[i];
    			rou[i]=0;
    		}
    		else
    			j++;
    	}
    	if(j==r){
    		if(res>max){
    			ans=(int[])route.clone();
    			max=res;
    		}
    		return;
    	}
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
文件里共包含有14个文件,部分代码如下: package 挖雷游戏; /** * * @author Administrator */ import java.awt.event.*; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.util.*; import java.io.*; public class Game extends JFrame implements ActionListener { JMenuBar bar; JMenu fileMenu; JMenuItem 初级,中级,高级,扫雷英雄榜; int grade=2; MineSquare 雷阵; JButton buttonPerson; Container con; JPanel box; File 英雄榜=new File("英雄榜.txt"); Hashtable hashtable=null; Record 记录对话框=null; ShowRecord 显示英雄榜对话框=null; Game() { buttonPerson=new JButton(new ImageIcon("微笑脸.gif")); 雷阵=new MineSquare("中级",40,16,16,buttonPerson,记录对话框); buttonPerson.addActionListener(this); con=getContentPane(); con.add(雷阵,BorderLayout.CENTER); box=new JPanel(); box.setLayout(new GridLayout(1,3)); box.add(雷阵.count.textShowMine); box.add(buttonPerson); box.add(雷阵.time.textShowTime); con.add(box,BorderLayout.NORTH); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setBounds(100,50,360,380); bar=new JMenuBar(); fileMenu=new JMenu("游戏"); 初级=new JMenuItem("初级"); 中级=new JMenuItem("中级"); 高级=new JMenuItem("高级"); fileMenu.setMnemonic('G'); 初级.setMnemonic('B'); 中级.setMnemonic('I'); 高级.setMnemonic('E'); 扫雷英雄榜=new JMenuItem("扫雷英雄榜"); 初级.setMnemonic('T'); fileMenu.add(初级); fileMenu.add(中级); fileMenu.add(高级); fileMenu.add(扫雷英雄榜); bar.add(fileMenu); setJMenuBar(bar); 初级.addActionListener(this); 中级.addActionListener(this); 高级.addActionListener(this); 扫雷英雄榜.addActionListener(this); hashtable=new Hashtable(); hashtable.put("初级","初级#"+999+"#匿名"); hashtable.put("中级","中级#"+999+"#匿名"); hashtable.put("高级","高级#"+999+"#匿名"); 记录对话框=new Record(this,hashtable); 记录对话框.setGrade("高级"); if(!英雄榜.exists()) { try{ FileOutputStream out=new FileOutputStream(英雄榜); ObjectOutputStream object_out=new ObjectOutputStream(out); object_out.writeObject(hashtable); object_out.close(); out.close(); } catch(IOException e) { } } 显示英雄榜对话框=new ShowRecord(this,hashtable); validate(); } public void newGame(String grade,int number,int rows,int cols,int w,int h) { buttonPerson.setIcon(new ImageIcon("微笑脸.gif")); 记录对话框.setGrade(grade); 雷阵=new MineSquare(grade,number,rows,cols,buttonPerson,记录对话框); con.removeAll(); box.removeAll(); box.add(雷阵.count.textShowMine); box.add(buttonPerson); box.add(雷阵.time.textShowTime); con.add(box,BorderLayout.NORTH); con.add(雷阵,BorderLayout.CENTER); setBounds(10,10,w,h); validate(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==初级) { grade=1; newGame("初级",10,8,8,180,220); } if(e.getSource()==中级) { grade=2; newGame("中级",40,16,16,360,380); } if(e.getSource()==高级) { grade=3; newGame("高级",99,22,22,500,520); } if(e.getSource()==扫雷英雄榜) { 显示英雄榜对话框.显示记录(); 显示英雄榜对话框.setVisible(true); } if(e.getSource()==buttonPerson) { if(grade==1) { newGame("初级",10,8,8,180,220); } if(grade==2) { newGame("中级",40,16,16,360,380); } if(grade==3) { newGame("高级",99,22,22,500,520); } } } public static void main(String args[]) { new Game(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小崔崔谁用的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值