CHESSBOARD(模拟)

田阳泽
2023-12-01

CHESSBOARD

题解

所有都遍历一遍,如此暴力居然没超时。

#include <string>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
#define ll long long
int n, m, o, l;
bool map[600][600];
char road[600];

int main() {
    int t;
    int cas = 0;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d%d%d", &n, &m, &o, &l);
        memset(map, 1, sizeof(map));
        for (int i = 1; i <= o; i++) {
            int x, y;
            scanf("%d%d", &x, &y);
            map[x][y] = 0;
        }
        scanf("%s", road + 1);
        ll ans = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (map[i][j] != 0) {
                    int endI = i, endJ = j;
                    for (int k = 1; k <= l; k++) {
                        if (road[k] == 'U' && endI - 1 >= 1 && map[endI - 1][endJ]) {
                            endI -= 1;
                        } else if (road[k] == 'R' && endJ + 1 <= m && map[endI][endJ + 1]) {
                            endJ += 1;
                        } else if (road[k] == 'L' && endJ - 1 >= 1 && map[endI][endJ - 1]) {
                            endJ -= 1;
                        } else if (road[k] == 'D' && endI + 1 <= n && map[endI + 1][endJ]) {
                            endI += 1;
                        }
                    }
                    ans = ans + (ll) ((i - endI) * (i - endI) + (j - endJ) * (j - endJ));
                }
            }
        }
        printf("Case #%d: %lld\n", ++cas, ans);
    }

    return 0;
}
 类似资料: