TLE 60pts 求调QwQ
查看原帖
TLE 60pts 求调QwQ
742017
zhangxiao666楼主2023/11/5 20:11
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10;
int n, m, sum, cnt;
int a[N][N];
pair<int, int> p[N * N];
map<pair<int, int>, int> mp;
int du[N * N], to[2 * N * N], nxt[2 * N * N], head[N * N];
int dx[5] = {0, 1, 0, -1, 0};
int dy[5] = {0, 0, 1, 0, -1};

void read()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            char s;
            cin >> s;
            if(s == 'S') a[i][j] = 1;
            if(s == 'E') a[i][j] = 2;
            if(s == 'N') a[i][j] = 3;
            if(s == 'W') a[i][j] = 4;
            if(s != '.') p[++sum] = make_pair(i, j), mp[p[sum]] = sum;
        }
    }
}

void add(int x, int y)
{
    du[y]++;
    to[++cnt] = y;
    nxt[cnt] = head[x];
    head[x] = cnt;
}

void init()
{
    for(int i = 1; i <= sum; i++)
    {
        int  x = p[i].first, y = p[i].second;
        int nowx = x + dx[a[x][y]], nowy = y + dy[a[x][y]];
        while(nowx && nowy && nowx <= n && nowy <= m)
        {
            if(a[nowx][nowy])
            {
                add(mp[make_pair(nowx, nowy)], i);
            }
            nowx += dx[a[x][y]];
			nowy += dy[a[x][y]]; 
        }
    }
}

void topsort()
{
    int top = 0, st[N * N];
    for(int i = 1; i <= sum; i++) 
    {
        if(!du[i]) st[++top] = i;
    }
    sum = 0;
    while(top)
    {
        int h = st[top];
        top--;
        cout << "(" << p[h].first - 1 << "," << p[h].second - 1 << ")" << "\n";
        for(int i = head[h]; i; i = nxt[i])
        {
            du[to[i]]--;
            if(du[to[i]] == 0) st[++top] = to[i];
        }
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    read();
    init();
    topsort();
    return 0;
}
2023/11/5 20:11
加载中...