算法刷题记录(1)

迷宫问题

一、洛谷P1683 入门

题目:

# 入门

## 题目描述

不是任何人都可以进入桃花岛的,黄药师最讨厌像郭靖一样呆头呆脑的人。所以,他在桃花岛的唯一入口处修了一条小路,这条小路全部用正方形瓷砖铺设而成。有的瓷砖可以踩,我们认为是安全的,而有的瓷砖一踩上去就会有喷出要命的毒气,那你就死翘翘了,我们认为是不安全的。你只能从一块安全的瓷砖上走到与他相邻的四块瓷砖中的任何一个上,但它也必须是安全的才行。

由于你是黄蓉的朋友,她事先告诉你哪些砖是安全的、哪些砖是不安全的,并且她会指引你飞到第一块砖上(第一块砖可能在任意安全位置),现在她告诉你进入桃花岛的秘密就是:如果你能走过最多的瓷砖并且没有死,那么桃花岛的大门就会自动打开了,你就可以从当前位置直接飞进大门了。

注意:瓷砖可以重复走过,但不能重复计数。

## 输入格式

第一行两个正整数 $W$ 和 $H$,分别表示小路的宽度和长度。

以下 $H$ 行为一个 $H\times W$ 的字符矩阵。每一个字符代表一块瓷砖。其中,`.` 代表安全的砖,`#` 代表不安全的砖,`@` 代表第一块砖。

## 输出格式

输出一行,只包括一个数,即你从第一块砖开始所能安全走过的最多的砖块个数(包括第一块砖)。

## 样例 #1

### 样例输入 #1

```
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
```

### 样例输出 #1

```
59
```

## 提示

#### 数据规模与约定

对于全部的测试点,保证 1≤W,H≤20。

代码

import java.io.*;
import java.util.*;

class Main {
    static int N = 25;
    static char[][] path = new char[N][N];
    static int n,m,res;
    static int[] dx = {-1,0,1,0};
    static int[] dy = {0,1,0,-1};
    static boolean[][] st = new boolean[N][N];
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        n = Integer.parseInt(input[0]);
        m = Integer.parseInt(input[1]);
        for(int i = 0;i < m;i++) {
            path[i] = br.readLine().toCharArray();
        }
        for(int i = 0;i < m;i++) {
            for(int j = 0;j < n;j++) {
                if(path[i][j] == '@') {
                    dfs(i,j);
                    break;
                }
            }
        }
        
        res++;
        System.out.print(res);
    }
    
    public static void dfs(int x,int y) {
        for(int i = 0;i < 4;i++) {
            int a = x + dx[i];
            int b = y + dy[i];
            
            if(a < 0 || a >= m || b < 0 || b >= n) continue;
            if(st[a][b]) continue;
            if(path[a][b] != '.') continue;
            
            st[a][b] = true;
            res++;
            dfs(a,b);
        }
    }
}

二、P1596 Lake Counting S

# [USACO10OCT]Lake Counting S

## 题面翻译

由于近期的降雨,雨水汇集在农民约翰的田地不同的地方。我们用一个 $N\times M(1\leq N\leq 100, 1\leq M\leq 100)$ 的网格图表示。每个网格中有水(`W`) 或是旱地(`.`)。一个网格与其周围的八个网格相连,而一组相连的网格视为一个水坑。约翰想弄清楚他的田地已经形成了多少水坑。给出约翰田地的示意图,确定当中有多少水坑。

输入第 $1$ 行:两个空格隔开的整数:$N$ 和 $M$。

第 $2$ 行到第 N+1 行:每行 M 个字符,每个字符是 `W` 或 `.`,它们表示网格图中的一排。字符之间没有空格。

输出一行,表示水坑的数量。

## 题目描述

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors. Given a diagram of Farmer John's field, determine how many ponds he has.

## 输入格式

Line 1: Two space-separated integers: N and M \* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

## 输出格式

Line 1: The number of ponds in Farmer John's field.

## 样例 #1

### 样例输入 #1

```
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
```

### 样例输出 #1

```
3
```

## 提示

OUTPUT DETAILS: There are three ponds: one in the upper left, one in the lower left, and one along the right side.

代码

import java.util.*;
import java.io.*;

class Main {
    static int N = 110;
    static int n,m,res;
    static char[][] path = new char[N][N];
    static int[] dx = {0,0,1,1,-1,-1,1,-1};
    static int[] dy = {1,-1,1,-1,1,-1,0,0};
    static boolean[][] st = new boolean[N][N];
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        n = Integer.parseInt(input[0]);
        m = Integer.parseInt(input[1]);
        
        for(int i = 0;i < n;i++) {
            path[i] = br.readLine().toCharArray();
        }
        
        for(int i = 0;i < n;i++) {
            for(int j = 0;j < m;j++) {
                if(path[i][j] == 'W' && !st[i][j]) {
                    dfs(i,j);
                    res++;
                }
            }
        }
        
        System.out.print(res);
    }
    
    public static void dfs(int x,int y) {
        for(int i = 0;i < 8;i++) {
            int a = x + dx[i];
            int b = y + dy[i];
            
            if(a < 0 || a >= n || b < 0 || b >= m) continue;
            if(path[a][b] != 'W') {
                continue;
            }
            
            if(st[a][b]) continue;
            
            st[a][b] = true;
            dfs(a,b);
            
        }
    }
}

三、acwing1114 棋盘问题

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。

要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放 k 个棋子的所有可行的摆放方案数目 C。

输入格式

输入含有多组测试数据。

每组数据的第一行是两个正整数 n,k,用一个空格隔开,表示了将在一个 n∗n 的矩阵内描述棋盘,以及摆放棋子的数目。当为-1 -1时表示输入结束。

随后的 n 行描述了棋盘的形状:每行有 n 个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。

输出格式

对于每一组数据,给出一行输出,输出摆放的方案数目 C(数据保证 C<231<231)。

输入样例:

2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1

输出样例:

2
1

 代码

import java.io.*;
import java.util.*;

class Main {
    static int N = 10;
    static char[][] p = new char[N][N];
    static int n,k,res;
    static boolean[] st = new boolean[N];
    
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        
        while(true) {
            n = s.nextInt();
            k = s.nextInt();
            
            if(n == -1 && k == -1) break;
            
            for(int i = 0;i < n;i++) {
                p[i] = s.next().toCharArray();
            }
            res = 0;
            dfs(0,0);
            System.out.println(res);
        }
    }
    
    public static void dfs(int x,int count) {
        if(count == k) {
            res++;
            return;
        }
        
        if(x >= n) return;
        
        for(int i = 0;i < n;i++) {
            if(!st[i] && p[x][i] == '#') {
                st[i] = true;
                dfs(x + 1,count + 1);
                st[i] = false;
            } 
        }
        
        //这里如果没有这句的话,那么比如样例一,第二行就不会走到
        dfs(x + 1,count);
    
    }
    
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值