[hihocoder][Offer收割]编程练习赛44

扫雷游戏

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
void makedata() {
    freopen("input.txt", "w", stdout);
    cout << 200000 << endl;

    for(int i = 0; i < 200000; i++) cout << 1000000000 << ' ';

    fclose(stdout);
}

char a[200][200];
const int dx[8] = { -1, 0, 1, 0, -1, 1, 1, -1 };
const int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            cin >> a[i][j];
        }
    }

    for(int i = 0; i <= n + 1; i++) {
        a[0][i] = '.';
        a[n + 1][n + 1] = '.';
        a[i][0] = '.';
        a[i][n + 1] = '.';
    }

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            int tmp = 0;

            for(int k = 0; k < 8; k++) {
                if(a[i + dx[k]][j + dy[k]] == '*') tmp++;
            }

            if(a[i][j] == '*') cout << a[i][j];
            else cout << tmp;
        }

        cout << endl;
    }

    return 0;
}
View Code

最大子矩阵2

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;


void makedata() {
    freopen("input.txt", "w", stdout);
    fclose(stdout);
}

lint a[400][400], s[400][400];
inline lint sum(int x1, int y1, int x2, int y2) {
    if (x1 == 0 && y1 == 0) return s[x2][y2];
    if (x1 != 0 && y1 != 0) return s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
    if (x1 == 0 && y1 != 0) return s[x2][y2] - s[x2][y1 - 1];
    if (x1 != 0 && y1 == 0) return s[x2][y2] - s[x1 - 1][y2];
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n, m;
    lint k;
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }
    memset(s, 0, sizeof(s));
    s[0][0] = a[0][0];
    for (int i = 1; i < n; i++) s[i][0] = a[i][0] + s[i - 1][0];
    for (int i = 1; i < m; i++) s[0][i] = a[0][i] + s[0][i - 1];
    for (int i = 1; i < n; i++) {
        for (int j = 1; j < m; j++) {
            s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + a[i][j];
        }
    }
    int ans = -1;
    for (int x1 = 0; x1 < n; x1++) {
        for (int y1 = 0; y1 < m; y1++) {
            if ((n - x1) * (m - y1) <= ans) break;
            for (int x2 = n - 1; x2 >= x1 ; x2--) {
                if ((x2 - x1 + 1) * (m - y1) <= ans) break;
                for (int y2 = m - 1; y2 >= y1; y2--) {
                    if ((x2 - x1 + 1) * (y2 - y1 + 1) <= ans) break;
                    if (sum(x1, y1, x2, y2) <= k) ans = max(ans, (x2 - x1 + 1) * (y2 - y1 + 1));
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/dramstadt/p/8328600.html