이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <queue>
#define NMAX 4000
using namespace std;
int n, m, viz[NMAX+10][NMAX+10], v[NMAX+10][NMAX+10], ans;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
queue <pair <int, int> > Q[2], auxQ;
void init_bfs()
{   while(!auxQ.empty())
        {   pair <int, int> a = auxQ.front();
            auxQ.pop();
            for(int i=0; i<4; i++)
                {   pair <int, int> vec = {dx[i] + a.first, dy[i] + a.second};
                    if(vec.first && vec.first <= n && vec.second && vec.second <= m
                       && !viz[vec.first][vec.second] && v[vec.first][vec.second] == v[1][1])
                        {   viz[vec.first][vec.second] = 1;
                            auxQ.push(vec);
                            Q[0].push(vec);
                        }
                }
        }
}
void bfs(char ch, int t)
{   if(Q[t].empty()) return;
    ans++;
    while(!Q[t].empty())
        {   pair <int, int> a = Q[t].front();
            Q[t].pop();
            for(int i=0; i<4; i++)
                {   pair <int, int> vec = {dx[i] + a.first, dy[i] + a.second};
                    if(vec.first && vec.first <= n && vec.second && vec.second <= m
                       && !viz[vec.first][vec.second] && v[vec.first][vec.second] == ch)
                        {   viz[vec.first][vec.second] = 1;
                            Q[t].push(vec);
                            Q[1-t].push(vec);
                        }
                }
        }
    if(ch == 'F') bfs('R', 1 - t);
    else bfs('F', 1 - t);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
        {   string s;
            cin >> s;
            for(int j=0; j<m; j++) v[i][j+1] = s[j];
        }
    auxQ.push({1, 1});
    Q[0].push({1, 1});
    viz[1][1] = 1;
    init_bfs();
    if(v[1][1] == 'F') bfs('R', 0);
    else bfs('F', 0);
    cout << ans << '\n';
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |