QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#873343#9977. Norte da UniversidadeMax_s_xaMWA 259ms37440kbC++178.2kb2025-01-26 12:17:332025-01-26 12:17:34

Judging History

你现在查看的是最新测评结果

  • [2025-01-26 12:17:34]
  • 评测
  • 测评结果:WA
  • 用时:259ms
  • 内存:37440kb
  • [2025-01-26 12:17:33]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <random>
#include <ctime>
#include <chrono>
#include <numeric>
#include <iomanip>
#include <cassert>

typedef long long ll;
typedef double lf;
typedef unsigned long long ull;

// #define DEBUG 1
struct IO
{
    #define MAXSIZE (1 << 20)
    #define isdigit(x) (x >= '0' && x <= '9')
    char buf[MAXSIZE], *p1, *p2;
    char pbuf[MAXSIZE], *pp;
    #if DEBUG
    #else
    IO() : p1(buf), p2(buf), pp(pbuf) {}
    ~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
    #endif
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
    #define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')

    template <typename T>
    void Read(T &x)
    {
        #if DEBUG
        std::cin >> x;
        #else
        bool sign = 0; char ch = gc(); x = 0;
        for (; !isdigit(ch); ch = gc())
            if (ch == '-') sign = 1;
        for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
        if (sign) x = -x;
        #endif
    }
    void Read(char *s)
    {
        #if DEBUG
        std::cin >> s;
        #else
        char ch = gc();
        for (; blank(ch); ch = gc());
        for (; !blank(ch); ch = gc()) *s++ = ch;
        *s = 0;
        #endif
    }
    void Read(char &c) {for (c = gc(); blank(c); c = gc());}

    void Push(const char &c)
    {
        #if DEBUG
        putchar(c);
        #else
        if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
        *pp++ = c;
        #endif
    }
    template <typename T>
    void Write(T x)
    {
        if (x < 0) x = -x, Push('-');
        static T sta[35];
        int top = 0;
        do sta[top++] = x % 10, x /= 10; while (x);
        while (top) Push(sta[--top] ^ 48);
    }
    template <typename T>
    void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)

using namespace std;

const int MAXN = 1010, mod = 998244353;

inline void add(int &x, const int &y) { (x += y) >= mod && (x -= mod); }

int n, m;
char mp[MAXN][MAXN];

int mxN[MAXN], mxW[MAXN], mxS[MAXN], mxE[MAXN];
int alN[MAXN], alW[MAXN], alS[MAXN], alE[MAXN];
int nwv[MAXN][MAXN], nwh[MAXN][MAXN];
int wsv[MAXN][MAXN], wsh[MAXN][MAXN];
int nev[MAXN][MAXN], neh[MAXN][MAXN];
int esv[MAXN][MAXN], esh[MAXN][MAXN];
int s[MAXN];

int main()
{
    // freopen("C.in", "r", stdin);
    // freopen("C.out", "w", stdout);
    #if DEBUG
    #else
    ios::sync_with_stdio(0), cin.tie(0);
    #endif
    int T;
    Read(T);
    while (T--)
    {
        Read(n), Read(m);
        for (int i = 1; i <= n; i++) Read(mp[i] + 1);
        for (int i = 1; i <= m; i++) mxN[i] = 0, mxS[i] = n + 1, alN[i] = n, alS[i] = 1;
        for (int i = 1; i <= n; i++) mxW[i] = 0, mxE[i] = m + 1, alW[i] = m, alE[i] = 1;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
            {
                if (mp[i][j] == 'N') mxN[j] = max(mxN[j], i), alW[i] = min(alW[i], j - 1), alS[j] = max(alS[j], i + 1), alE[i] = max(alE[i], j + 1);
                if (mp[i][j] == 'W') mxW[i] = max(mxW[i], j), alN[j] = min(alN[j], i - 1), alS[j] = max(alS[j], i + 1), alE[i] = max(alE[i], j + 1);
                if (mp[i][j] == 'S') mxS[j] = min(mxS[j], i), alW[i] = min(alW[i], j - 1), alN[j] = min(alN[j], i - 1), alE[i] = max(alE[i], j + 1);
                if (mp[i][j] == 'E') mxE[i] = min(mxE[i], j), alW[i] = min(alW[i], j - 1), alS[j] = max(alS[j], i + 1), alN[j] = min(alN[j], i - 1);
            }
        memset(nwv, 0, sizeof(nwv)), memset(nwh, 0, sizeof(nwh));
        nwh[0][0] = 1;
        for (int j = 1; j <= m; j++)
            for (int i = 0; i <= n; i++)
            {
                if (i && mxW[i] < j && alW[i] >= j - 1) add(nwv[i][j], nwv[i - 1][j]), add(nwv[i][j], nwh[i - 1][j - 1]);
                if (mxN[j] <= i && alN[j] >= i) add(nwh[i][j], nwh[i][j - 1]), add(nwh[i][j], nwv[i][j]);
            }
        memset(wsv, 0, sizeof(wsv)), memset(wsh, 0, sizeof(wsh));
        wsh[n + 1][0] = 1;
        for (int j = 1; j <= m; j++)
            for (int i = n + 1; i >= 0; i--)
            {
                if (i <= n && mxW[i] < j && alW[i] >= j - 1) add(wsv[i][j], wsv[i + 1][j]), add(wsv[i][j], wsh[i + 1][j - 1]);
                if (mxS[j] >= i && alS[j] <= i) add(wsh[i][j], wsh[i][j - 1]), add(wsh[i][j], wsv[i][j]);
            }
        memset(nev, 0, sizeof(nev)), memset(neh, 0, sizeof(neh));
        neh[0][m + 1] = 1;
        for (int j = m; j >= 1; j--)
            for (int i = 0; i <= n; i++)
            {
                if (i && mxE[i] > j && alE[i] <= j + 1) add(nev[i][j], nev[i - 1][j]), add(nev[i][j], neh[i - 1][j + 1]);
                if (mxN[j] <= i && alN[j] >= i) add(neh[i][j], neh[i][j + 1]), add(neh[i][j], nev[i][j]);
            }
        memset(esv, 0, sizeof(esv)), memset(esh, 0, sizeof(esh));
        esh[n + 1][m + 1] = 1;
        for (int j = m; j >= 1; j--)
            for (int i = n + 1; i >= 0; i--)
            {
                if (i <= n && mxE[i] > j && alE[i] <= j + 1) add(esv[i][j], esv[i + 1][j]), add(esv[i][j], esh[i + 1][j + 1]);
                if (mxS[j] >= i && alS[j] <= i) add(esh[i][j], esh[i][j + 1]), add(esh[i][j], esv[i][j]);
            }
        int ans = 0, sum = nwv[n][1];
        for (int j = 1; j <= m; j++)
        {
            int mul = 0, cur = 0;
            for (int i = 0; i <= n; i++)
                if (mxN[j] <= i && alN[j] >= i && mxS[j] > i && alS[j] <= i + 1) mul++;
            for (int i = 1; i <= n; i++) cur = (cur + (ll)neh[i - 1][j + 1] * esv[i][j]) % mod;
            if (j == m) cur = nev[n][m];
            sum = (ll)sum * mul % mod, ans = (ans + (ll)sum * cur) % mod;
            cur = 0;
            for (int i = 1; i <= n; i++) cur = (cur + (ll)nwh[i - 1][j] * wsv[i][j + 1]) % mod;
            add(sum, cur);
        }
        for (int j = 1; j < m; j++)
        {
            for (int i = 1; i < n; i++) s[i] = (s[i - 1] + (ll)nev[i][j] * esh[i + 1][j + 1]) % mod;
            sum = 0;
            for (int i = n; i > 1; i--)
                sum = (sum + (ll)nwh[i - 1][j] * wsv[i][j + 1]) % mod, ans = (ans + (ll)sum * s[i - 2]) % mod;
            for (int i = 1; i < n; i++) s[i] = (s[i - 1] + (ll)nwv[i][j + 1] * wsh[i + 1][j]) % mod;
            sum = 0;
            for (int i = n; i > 1; i--)
                sum = (sum + (ll)neh[i - 1][j + 1] * esv[i][j]) % mod, ans = (ans + (ll)sum * s[i - 2]) % mod;
        }
        sum = nwh[0][m];
        for (int i = 1; i <= n; i++)
        {
            int mul = 0, cur = 0;
            for (int j = 0; j <= m; j++)
                if (mxW[i] <= j && alW[i] >= j && mxE[i] > j && alE[i] <= j + 1) mul++;
            for (int j = 1; j <= m; j++) cur = (cur + (ll)wsv[i + 1][j] * esh[i + 1][j]) % mod;
            if (i == n) cur = wsh[n + 1][m];
            sum = (ll)sum * mul % mod, ans = (ans + (ll)sum * cur) % mod;
            cur = 0;
            for (int j = 1; j <= m; j++) cur = (cur + (ll)nwv[i][j] * neh[i][j]) % mod;
            add(sum, cur);
        }
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < m; j++) s[j] = (s[j - 1] + (ll)esv[i + 1][j] * wsh[i + 1][j]) % mod;
            sum = 0;
            for (int j = m; j > 1; j--)
                sum = (sum + (ll)nwv[i][j] * neh[i][j]) % mod, ans = (ans + (ll)sum * s[j - 2]) % mod;
            for (int j = 1; j < m; j++) s[j] = (s[j - 1] + (ll)nwh[i][j] * nev[i][j]) % mod;
            sum = 0;
            for (int j = m; j > 1; j--)
                sum = (sum + (ll)wsv[i + 1][j] * esh[i][j]) % mod, ans = (ans + (ll)sum * s[j - 2]) % mod;
        }

        for (int i = 1; i < n; i++)
            for (int j = 1; j < m; j++)
            {
                ans = (ans + (ll)nwh[i][j] * wsv[i + 1][j + 1] % mod * nev[i][j] % mod * esh[i + 1][j + 1]) % mod;
                ans = (ans + (ll)nwv[i][j + 1] * wsh[i + 1][j] % mod * neh[i][j + 1] % mod * esv[i + 1][j]) % mod;
            }
        cout << ans << "\n";
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 10ms
memory: 37080kb

input:

5
11 5
NNNNN
NN???
WW???
WWEEE
WEEEE
WEEEE
WWEEE
WWEE?
SSSSS
?SSS?
??SS?
2 7
??S?N??
??S?N??
3 4
W??E
WEEE
?E??
2 2
??
??
3 3
???
???
???

output:

26
1587
18
56
1112

result:

ok 5 number(s): "26 1587 18 56 1112"

Test #2:

score: -100
Wrong Answer
time: 259ms
memory: 37440kb

input:

100
4 4
??NN
????
????
S?S?
4 4
????
????
??E?
??S?
4 4
????
??NE
?S?E
S?S?
4 4
WN??
W???
?S??
??S?
4 4
?N??
????
?S??
??S?
4 4
????
W???
????
????
4 4
W???
W?N?
??E?
???E
4 4
??N?
????
W?EE
?S??
4 4
??N?
??N?
?S??
S??E
4 4
????
????
???E
?S?E
4 4
W???
W?N?
W?E?
S???
4 4
WN??
????
????
????
4 4
?N??...

output:

3288
4020
468
936
3627
16026
462
1362
870
5959
147
5544
1216
9727
2944
410
144
823
11945
7712
1221
477
456
96
2004
6971
2216
144
4622
16026
62
108
108
342
1221
936
3132
749
1572
954
104
4931
3362
276
1168
2649
980
360
374
1761
756
4397
474
1280
9865
72
980
1752
3288
3863
892
3222
1020
72
5708
1046
3...

result:

wrong answer 1st numbers differ - expected: '3280', found: '3288'