luyencode / comments

Server lưu trữ bình luận trên Luyện Code
https://luyencode.net
6 stars 3 forks source link

https://oj.luyencode.net/problem/Baovebaibien #831

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Chi tiết bài tập - Luyện Code Online

https://oj.luyencode.net/problem/Baovebaibien

DangLun commented 1 year ago

AC

Xem code AC

```cpp #include #define ll long long #define pb push_back inline ll gcd(ll a, ll b){ll x; while(b){x = a%b; a = b; b = x;} return a;} inline ll lcm(ll a, ll b){return (a*b)/gcd(a, b);} using namespace std; const int maxn = 1001; int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; int a[maxn][maxn]; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for(int i = 1; i<= n; i++){ for(int j = 1; j<= m; j++){ cin >> a[i][j]; } } int cl = 2; int res = 0; for(int i = 1; i<= n; i++){ for(int j = 1; j<= m; j++){ if(a[i][j] == 1){ res++; for(int k = 0; k< 8; k++){ if(a[i + dx[k]][j + dy[k]] == 0 && i + dx[k] >0 && i + dx[k] <= n && j + dy[k] > 0 && dy[k] + j <= m){ res++; a[i + dx[k]][j + dy[k]] = cl; } } cl++; } } } cout << res; } ```

QuyetFCB275 commented 1 year ago

Sao em dùng cấp phát động mà không đc vậy ạ

hoangdev0 commented 1 year ago
Xem code AC:
#include <bits/stdc++.h>
        #define fast() ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
        using namespace std;
        // const int N = 1e6+2;
        int n, m, res(0), b(2);
        int main()
        {
            fast();
            cin >> n >> m;
            vector<vector<int>> a;
            a.resize(n);
            for (int i = 0; i < n; i++)
            {
                a[i].resize(m);
                for (int j = 0; j < m; j++)
                    cin >> a[i][j];
            }
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                {
                    if (a[i][j] == 1)
                    {
                        res++;
                        int xi = i - 1, yj = j - 1;
                        for (int k = 0; k < 9; k++)
                        {
                            if ((xi >= 0 && xi < n) && (yj >= 0 && yj < m))
                                if (a[xi][yj] == 0)
                                    res++, a[xi][yj] = b;
                            yj++;
                            if (yj == j + 2)
                                yj = j - 1, xi++;
                        }
                    }
                }
            cout << res;
            return 0;
        }
        
m0nters commented 1 year ago

Sử dụng mảng tĩnh

Xem code AC

```ruby #include #define MAX 10000 using namespace std; int m, n; int arr[MAX][MAX]; const int d8x[8] = { -1,-1,0,1,1,1,0,-1 }; const int d8y[8] = { 0,1,1,1,0,-1,-1,-1 }; void nhapMang() { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cin >> arr[i][j]; } } } bool istrue(int x, int y) { if (x < 0 || x >= m || y < 0 || y >= n) return 0; else return 1; } void DFS(int x, int y, int& count) { count++; for (int i = 0; i < 8; i++) { int newX = x + d8x[i]; int newY = y + d8y[i]; if (istrue(newX, newY) && arr[newX][newY] == 0) { count++; arr[newX][newY] = 2; } } } int main() { cin >> m >> n; int count = 0; nhapMang(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (arr[i][j] == 1) DFS(i, j, count); } } cout << count; return 0; } ```

m0nters commented 1 year ago

Cấp phát động

Xem code AC

```ruby #include #define MAX 10000 using namespace std; int m, n; const int d8x[8] = { -1,-1,0,1,1,1,0,-1 }; const int d8y[8] = { 0,1,1,1,0,-1,-1,-1 }; void nhapMang(int **arr) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cin >> arr[i][j]; } } } bool istrue(int x, int y) { if (x < 0 || x >= m || y < 0 || y >= n) return 0; else return 1; } void DFS(int** arr, int x, int y, int& count) { count++; for (int i = 0; i < 8; i++) { int newX = x + d8x[i]; int newY = y + d8y[i]; if (istrue(newX, newY) && arr[newX][newY] == 0) { count++; arr[newX][newY] = 2; } } } int main() { cin >> m >> n; int count = 0; int** arr = new int* [m]; for (int i = 0; i < m; i++) { arr[i] = new int[n]; } nhapMang(arr); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (arr[i][j] == 1) DFS(arr, i, j, count); } } cout << count; return 0; } ```

tambl2004 commented 1 year ago

mn tham khảo code bằng python 3:

dx = [-1, -1, -1, 0, 0, 1, 1, 1] dy = [-1, 0, 1, -1, 1, -1, 0, 1]

n, m = map(int, input().split()) a = [list(map(int, input().split())) for _ in range(n)]

cl = 2 res = 0 for i in range(n): for j in range(m): if a[i][j] == 1: res += 1 for k in range(8): x = i + dx[k] y = j + dy[k] if 0 <= x < n and 0 <= y < m and a[x][y] == 0: res += 1 a[x][y] = cl cl += 1

print(res)

quanghuy121 commented 1 year ago
Xem code AC ```cpp #include using namespace std; #define maxn 1004 #define ll long long int n, m; bool visited[maxn][maxn]; int a[maxn][maxn]; int dx[9] = {0, -1, 0, 1, 1, 1, 0, -1, -1}; int dy[9] = {0, -1, -1, -1, 0, 1, 1, 1, 0}; ll check(int i,int j){ ll res = 1; visited[i][j] = true; for (int k = 1; k<=8; k++) { int xo = dx[k] + i; int yo = dy[k] + j; if (xo >= 1 && xo <= n && yo >= 1 && yo <= m && visited[xo][yo] == false && a[xo][yo] == 0) { res++; visited[xo][yo] = true; }; } return res; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); memset(visited,false, sizeof(visited)); cin >> n >> m; for (int i = 1; i<=n; i++){ for (int j = 1; j<=m; j++){ cin >> a[i][j]; } } ll g = 0; for (int i = 1; i<=n; i++){ for (int j = 1; j <=m; j++){ if (a[i][j] == 1) { g += check(i,j); } } } cout << g; } ```