結果

問題 No.2731 Two Colors
ユーザー snrnsidy
提出日時 2024-04-19 23:18:07
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,347 bytes
コンパイル時間 2,587 ms
コンパイル使用メモリ 199,756 KB
最終ジャッジ日時 2025-02-21 05:33:47
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int dy[4] = {-1,0,1,0};
int dx[4] = {0,1,0,-1};
int n,m;
int A[1001][1001];
int board[1001][1001];

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

    cin >> n >> m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin >> A[i][j];
        }
    }

    priority_queue <pair<int,pair<int,int>>> pque[2];
    memset(board,-1,sizeof(board));
    pque[0].push(make_pair(-A[0][0],make_pair(0,0)));
    pque[1].push(make_pair(-A[n-1][m-1],make_pair(n-1,m-1)));

    int res = 0;
    while(1)
    {
        int y = pque[res%2].top().second.first;
        int x = pque[res%2].top().second.second;
        pque[res%2].pop();

        //cout << res+1 << ' ' << res%2 << ' ' << y << ' ' << x << '\n';
        board[y][x] = res%2;

        bool ok = true;
        for(int i=0;i<4;i++)
        {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny < 0 || ny >= n || nx < 0 || nx >= m) continue;
            if(board[ny][nx] == (1 - res%2))
            {
                ok = false;
            }
            else if(board[ny][nx] == -1)
            {
                pque[res%2].push(make_pair(-A[ny][nx],make_pair(ny,nx)));
            }
        }
        if(!ok) break;
        res+=1;
    }
    cout << res-1 << '\n';   
	return 0;
}
0