深入浅出学算法059-挖地雷(动态分析)

题目描述

在一个地图上有N个地窖(N<=200),每个地窖中埋有一定数量的地雷。同时,给出地窖之间的连接路径,并规定路径都是单向的,也不存在可以从一个地窖出发经过若干地窖后又回到原来地窖的路径。某人可以从任一处开始挖地雷,然后沿着指出的连接往下挖(仅能选择一条路径),当无连接时挖地雷工作结束。设计一个挖地雷的方案,使他能挖到最多的地雷。

输入

第一行输入一个整数N
第2行输入N个整数,表示每个地窖的地雷数
之后有若干行表示2个地窖之间有路径,输入 0 0代表结束

输出

第一行输出挖地雷的顺序,之间用-隔开
第2行输出最多挖出的地雷数

样例输入 Copy

6
5 10 20 5 4 5
1 2
1 4
2 4
3 4
4 5
4 6
5 6
0 0

样例输出 Copy

3-4-5-6
34

提示

编号大的地窖没有到编号小的地窖的路径

AC代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 310
using namespace std;

int n,m,k;
int nex[N],a[N];
int dist[N];
int b[N][N];
int ans;
void print(int u)
{
	if(u==0)
		return;
	print(nex[u]);
	if(nex[u]==0)
		cout<<u;
	else
		cout<<"-"<<u;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		dist[i]=a[i];	
	}
		
	cin>>m>>k;
	mem
文件里共包含有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(); } }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值