[LeetCode]419 Battleships in a Board(暴力,dfs)

题目链接:https://leetcode.com/problems/battleships-in-a-board/?tab=Description

题意:问平面上有多少条船,船是1*n或者n*1的,并且不相交。

最简单的想法就是直接dfs,但是看到题目里有一个思考,那就是不修改平面并且空间O(1)的one-pass。

这时候就想到应该是遍历所有船的左上角的端点,统计就行了。很简单。

 1 public class Solution {
 2     public int countBattleships(char[][] board) {
 3         int ret = 0;
 4         for (int i = 0; i < board.length; i++) {
 5             for (int j = 0; j < board[0].length; j++) {
 6                 if (board[i][j] == ‘X‘ && (i == 0 || board[i-1][j] == ‘.‘) && (j == 0 || board[i][j-1] == ‘.‘)) {
 7                     ret++;
 8                 }
 9             }
10         }
11         return ret;
12     }
13 }

DFS的代码

 1 class Solution {
 2 public:
 3     const int dx[5] = {0, 0, 1, -1};
 4     const int dy[5] = {1, -1, 0, 0};
 5     int ret;
 6     int ok(vector<vector<char>>& board, int x, int y) {
 7         return x < board.size() && y < board[0].size();
 8     }
 9
10     void dfs(vector<vector<char>>& board, int x, int y) {
11         board[x][y] = ‘.‘;
12         for(int i = 0; i < 4; i++) {
13             int xx = x + dx[i];
14             int yy = y + dy[i];
15             if(ok(board, xx, yy) && board[xx][yy] == ‘X‘) {
16                 dfs(board, xx, yy);
17             }
18         }
19     }
20
21   int countBattleships(vector<vector<char>>& board) {
22       ret = 0;
23       for(int i = 0; i < board.size(); i++) {
24           for(int j = 0; j < board[i].size(); j++) {
25               if(board[i][j] == ‘X‘) {
26                   dfs(board, i, j);
27                   ret++;
28               }
29           }
30       }
31       return ret;
32   }
33 };
时间: 2024-08-05 18:59:24

[LeetCode]419 Battleships in a Board(暴力,dfs)的相关文章

LeetCode &quot;419. Battleships in a Board&quot;

The follow-up question is fun: "Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?" When we meet an 'X', we need to check if it is vertical or horizontal: they will never happen at the same ti

419. Battleships in a Board

Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: You receive a valid board, made of only battleships or empty slot

419 Battleships in a Board 甲板上的战舰

给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则:    给你一个有效的甲板,仅由战舰或者空位组成.    战舰只能水平或者垂直放置.换句话说,战舰只能由 1xN (1 行, N 列)组成,或者 Nx1 (N 行, 1 列)组成,其中N可以是任意大小.    两艘战舰之间至少有一个水平或垂直的空位分隔 - 即没有相邻的战舰.示例 :X..X...X...X在上面的甲板中有2艘战舰.无效样例 :...XXXXX...X你不会收到这样的无效

A. The Fault in Our Cubes 暴力dfs

http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行,就会早早退出. 这样写起来比较好写. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <

hdu 5024 Wang Xifeng&#39;s Little Plot【暴力dfs,剪枝】

2014年广州网络赛的C题,也是水题.要你在一个地图中找出一条最长的路,这条路要保证最多只能有一个拐角,且只能为90度 我们直接用深搜,枚举每个起点,每个方向进行dfs,再加上剪枝. 但是如果直接写的话,那一定会特别麻烦,再加上方向这一属性也是我们需要考虑的方面,我们将从别的地方到当前点的方向编一个号:往右为0,如下图顺时针顺序编号 (往右下方向为1,往下为2......以此类推) 我们知道了当前点的坐标,来到当前点的方向,以及到目前有没有拐弯,这几个属性之后,就可以记忆化搜索了,用一个四维数组

Codeforces Round #359 (Div. 2) C. Robbers&#39; watch (暴力DFS)

题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b < m 且a的7进制和n-1的7进制位数相同 且b的7进制和m-1的7进制位数相同,还有a和b的7进制上的每位上的数各不相同. 看懂题目,就很简单了,先判断a和b的7进制位数是否超过7,不超过的话就dfs暴力枚举计算就可以了. 1 //#pragma comment(linker, "/STACK

hihoCoder 1185 连通性&#183;三(Tarjan缩点+暴力DFS)

#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出去,就拜托小Hi和小Ho忙帮放牧. 约翰家一共有N个草场,每个草场有容量为W[i]的牧草,N个草场之间有M条单向的路径. 小Hi和小Ho需要将牛羊群赶到草场上,当他们吃完一个草场牧草后,继续前往其他草场.当没有可以到达的草场或是能够到达的草场都已经被吃光了之后,小hi和小Ho就把牛羊群赶回家. 一开

Strange Country II 暴力dfs

这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cmath> #include <stack> #

ACM: Gym 100935G Board Game - DFS暴力搜索

Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935G Description standard input/outputStatements Feras bought to his nephew Saleem a new game to help him learning calculating. The game consists of a boar