挖地雷_1996年分区联赛提高组之三_ssl1071_dfs

66 篇文章 0 订阅
55 篇文章 0 订阅

Description

  在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷。同时,给出地窖之间的连接路径。 
  例如: 

Input

Output

K1 K2,……,KV(挖地雷的顺序) 
MAX (挖地雷的数量) 

Sample Input

5                                       

10 8 4 7 6             

1 1  1  0

  0  0  0

     1  1

        1

Sample Output

1 3 4 5

27

思路:

有人说是dp题,但是因为比较懒也就用dfs刷过了。查找每一个可以挖的地雷,因为题目给出的邻接矩阵比较特殊所以不用标记重复的地雷,用一个数组l记录路径。手气很好一次过。

源代码/pas:

var
  n,max:Longint;
  a:array[1..21,1..21]of longint;
  l,r,v:array[0..21]of longint;
procedure dfs(x,y:longint);
var
  i:longint;
begin
  if y>max then
  begin
    max:=y;
    r:=l;
  end;
  for i:=1 to n do
  if a[x,i]=1 then
  begin
    l[x]:=i;
    dfs(i,y+v[i]);
    l[x]:=0;
  end;
end;
procedure init;
var
  i,j:longint;
begin
  readln(n);
  for i:=1 to n do read(v[i]);
  for i:=1 to n-1 do
  for j:=i+1 to n do
  read(a[i,j]);
  for i:=1 to n do
  begin
    l[0]:=i;
    dfs(i,v[i]);
    fillchar(l,sizeof(l),0);
  end;
end;
procedure print;
var
  i:longint;
begin
  i:=0;
  while r[i]<>0 do
  begin
    i:=r[i];
    write(i,' ');
  end;
  writeln;
  writeln(max);
end;
begin
  init;
  print;
end.


好的,以下是一个简单的 Java 实现扫雷游戏的代码示例: ```java import java.util.Random; import java.util.Scanner; public class Minesweeper { private static final int BOARD_SIZE = 10; private static final int NUM_MINES = 10; private static final char COVERED_CELL = '*'; private static final char MINE_CELL = 'X'; private static final char EMPTY_CELL = '-'; private static final char FLAGGED_CELL = 'F'; private char[][] gameBoard; private boolean[][] mineLocations; private int numUncoveredCells; public Minesweeper() { gameBoard = new char[BOARD_SIZE][BOARD_SIZE]; mineLocations = new boolean[BOARD_SIZE][BOARD_SIZE]; numUncoveredCells = 0; initializeBoard(); placeMines(); } public void playGame() { System.out.println("Welcome to Minesweeper!"); Scanner scanner = new Scanner(System.in); while (true) { printBoard(); System.out.println("Enter a row and column to uncover (e.g. 3 4), or enter -1 to flag/unflag a cell:"); int row = scanner.nextInt() - 1; int col = scanner.nextInt() - 1; if (row == -2 && col == -2) { System.out.println("Quitting game..."); break; } if (row == -1 && col == -1) { System.out.println("Enter a row and column to flag:"); row = scanner.nextInt() - 1; col = scanner.nextInt() - 1; flagCell(row, col); } else { if (uncoverCell(row, col)) { System.out.println("You lose!"); printBoard(); break; } else if (numUncoveredCells == BOARD_SIZE * BOARD_SIZE - NUM_MINES) { System.out.println("You win!"); printBoard(); break; } } } } private void initializeBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { gameBoard[i][j] = COVERED_CELL; } } } private void placeMines() { Random rand = new Random(); int minesPlaced = 0; while (minesPlaced < NUM_MINES) { int row = rand.nextInt(BOARD_SIZE); int col = rand.nextInt(BOARD_SIZE); if (!mineLocations[row][col]) { mineLocations[row][col] = true; minesPlaced++; } } } private void printBoard() { System.out.print(" "); for (int i = 1; i <= BOARD_SIZE; i++) { System.out.print(i + " "); } System.out.println(); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print((i + 1) + " "); for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(gameBoard[i][j] + " "); } System.out.println(); } } private boolean uncoverCell(int row, int col) { if (mineLocations[row][col]) { gameBoard[row][col] = MINE_CELL; return true; } else { int numAdjacentMines = countAdjacentMines(row, col); gameBoard[row][col] = Character.forDigit(numAdjacentMines, 10); numUncoveredCells++; if (numAdjacentMines == 0) { uncoverAdjacentCells(row, col); } return false; } } private int countAdjacentMines(int row, int col) { int count = 0; for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && mineLocations[i][j]) { count++; } } } return count; } private void uncoverAdjacentCells(int row, int col) { for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && gameBoard[i][j] == COVERED_CELL) { uncoverCell(i, j); } } } } private void flagCell(int row, int col) { if (gameBoard[row][col] == COVERED_CELL) { gameBoard[row][col] = FLAGGED_CELL; } else if (gameBoard[row][col] == FLAGGED_CELL) { gameBoard[row][col] = COVERED_CELL; } } public static void main(String[] args) { Minesweeper game = new Minesweeper(); game.playGame(); } } ``` 这个简单的实现中,我们使用了一个 10x10 的二维字符数来表示游戏板。我们还有一个相同大小的二维布尔数,用于跟踪哪些方格上有地雷。在每个游戏回合中,玩家输入要揭开的方格的行和列号,程序将检查这个位置是否有地雷。如果是,游戏结束,否则程序将显示该位置周围的地雷数量,并揭开与该位置相邻的所有空白方格。玩家还可以标记他们认为有地雷的方格。如果玩家揭开了所有不是地雷的方格,游戏结束,玩家获胜。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值