AtCoder Regular Contest 005

Submission #758360

Source codeソースコード

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pi;

int h,w;

inline bool in(int y, int x)
{
    return (0<=y&&y<h && 0<=x && x<w);
}

int main()
{
    cin >>h >>w;
    vector<string> c(h);
    rep(i,h) cin >>c[i];

    pi start,goal;
    rep(i,h)rep(j,w)
    {
        if(c[i][j]=='s')
        {
            start.fi=i;
            start.se=j;
        }
        if(c[i][j]=='g')
        {
            goal.fi=i;
            goal.se=j;
        }
    }

    int vs[500][500]={0};
    int vg[500][500]={0};

    int dx[4]={1,-1,0,0}, dy[4]={0,0,1,-1};

    queue<pi> que;

    //startから壁を壊さずに訪れられる場所
    que.push(start);
    vs[start.fi][start.se]=1;
    while(!que.empty())
    {
        pi now=que.front();
        que.pop();
        rep(i,4)
        {
            int ny=now.fi+dy[i], nx=now.se+dx[i];
            if(in(ny,nx) && !vs[ny][nx] && c[ny][nx]!='#')
            {
                vs[ny][nx]=1;
                que.push(pi(ny,nx));
            }
        }
    }

    //goalから壁を壊さずに訪れられる場所
    que.push(goal);
    vg[goal.fi][goal.se]=1;
    while(!que.empty())
    {
        pi now=que.front();
        que.pop();
        rep(i,4)
        {
            int ny=now.fi+dy[i], nx=now.se+dx[i];
            if(in(ny,nx) && !vg[ny][nx] && c[ny][nx]!='#')
            {
                vg[ny][nx]=1;
                que.push(pi(ny,nx));
            }
        }
    }

    int vs2[500][500]={0};
    int vg2[500][500]={0};

    //startから訪れられる場所に隣接している壁を壊してみる
    rep(i,h)rep(j,w) vs2[i][j]=vs[i][j];

    rep(i,h)rep(j,w)
    {
        if(vs[i][j])
        {
            rep(k,4)
            {
                int ny=i+dy[k], nx=j+dx[k];
                if(in(ny,nx) && c[ny][nx]=='#' && !vs2[ny][nx])
                {
                    vs2[ny][nx]=1;
                    que.push(pi(ny,nx));
                    while(!que.empty())
                    {
                        pi now=que.front();
                        que.pop();
                        rep(l,4)
                        {
                            int nny=now.fi+dy[l], nnx=now.se+dx[l];
                            if(in(nny,nnx) && !vs2[nny][nnx] && c[nny][nnx]!='#')
                            {
                                vs2[nny][nnx]=1;
                                que.push(pi(nny,nnx));
                            }
                        }
                    }


                }
            }
        }
    }

    //goalから訪れられる場所に隣接している壁を壊してみる
    rep(i,h)rep(j,w) vg2[i][j]=vg[i][j];

    rep(i,h)rep(j,w)
    {
        if(vg[i][j])
        {
            rep(k,4)
            {
                int ny=i+dy[k], nx=j+dx[k];
                if(in(ny,nx) && c[ny][nx]=='#' && !vg2[ny][nx])
                {
                    vg2[ny][nx]=1;
                    que.push(pi(ny,nx));
                    while(!que.empty())
                    {
                        pi now=que.front();
                        que.pop();
                        rep(l,4)
                        {
                            int nny=now.fi+dy[l], nnx=now.se+dx[l];
                            if(in(nny,nnx) && !vg2[nny][nnx] && c[nny][nnx]!='#')
                            {
                                vg2[nny][nnx]=1;
                                que.push(pi(nny,nnx));
                            }
                        }
                    }


                }
            }
        }
    }

    /*
    rep(i,h)
    {
        rep(j,w) printf("%d",vs2[i][j]);
        printf("\n");
    }
    printf("\n");
    rep(i,h)
    {
        rep(j,w) printf("%d",vg2[i][j]);
        printf("\n");
    }
    */

    bool ok=false;
    rep(i,h)rep(j,w)
    {
        if(!vs2[i][j]) continue;

        rep(k,4)
        {
            int ny=i+dy[k], nx=j+dx[k];
            if(in(ny,nx) && vg2[ny][nx]) ok=true;
        }

        if(ok) break;
    }

    string ans="NO";
    if(ok) ans="YES";
    cout << ans << endl;

    return 0;
}

Submission

Task問題 C - 器物損壊!高橋君
User nameユーザ名 imulan
Created time投稿日時
Language言語 C++11 (GCC 4.8.1)
Status状態 AC
Score得点 100
Source lengthソースコード長 4562 Byte
File nameファイル名
Exec time実行時間 107 ms
Memory usageメモリ使用量 5100 KB

Test case

Set

Set name Score得点 / Max score Cases
All 100 / 100 00_min_01.txt,00_min_02.txt,00_min_03.txt,00_min_04.txt,00_min_05.txt,00_min_06.txt,00_min_07.txt,00_min_08.txt,00_sample_01.txt,00_sample_02.txt,00_sample_03.txt,00_sample_04.txt,00_sample_05.txt,01_rnd_00.txt,01_rnd_01.txt,01_rnd_02.txt,01_rnd_03.txt,01_rnd_04.txt,01_rnd_05.txt,01_rnd_06.txt,01_rnd_07.txt,01_rnd_08.txt,01_rnd_09.txt,01_rnd_10.txt,01_rnd_11.txt,01_rnd_12.txt,01_rnd_13.txt,01_rnd_14.txt,01_rnd_15.txt,01_rnd_16.txt,01_rnd_17.txt,01_rnd_18.txt,01_rnd_19.txt,02_rndhard_00.txt,02_rndhard_01.txt,02_rndhard_02.txt,02_rndhard_03.txt,02_rndhard_04.txt,02_rndhard_05.txt,02_rndhard_06.txt,02_rndhard_07.txt,02_rndhard_08.txt,02_rndhard_09.txt,02_rndhard_10.txt,02_rndhard_11.txt,02_rndhard_12.txt,02_rndhard_13.txt,02_rndhard_14.txt,02_rndhard_15.txt,02_rndhard_16.txt,02_rndhard_17.txt,02_rndhard_18.txt,02_rndhard_19.txt,02_rndhard_20.txt,02_rndhard_21.txt,02_rndhard_22.txt,02_rndhard_23.txt,02_rndhard_24.txt,02_rndhard_25.txt,02_rndhard_26.txt,02_rndhard_27.txt,02_rndhard_28.txt,02_rndhard_29.txt,02_rndhard_30.txt,02_rndhard_31.txt,02_rndhard_32.txt,02_rndhard_33.txt,02_rndhard_34.txt,02_rndhard_35.txt,02_rndhard_36.txt,02_rndhard_37.txt,02_rndhard_38.txt,02_rndhard_39.txt,03_rndhardsmall_00.txt,03_rndhardsmall_01.txt,03_rndhardsmall_02.txt,03_rndhardsmall_03.txt,03_rndhardsmall_04.txt,03_rndhardsmall_05.txt,03_rndhardsmall_06.txt,03_rndhardsmall_07.txt,03_rndhardsmall_08.txt,03_rndhardsmall_09.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
00_min_01.txt AC 38 ms 4924 KB
00_min_02.txt AC 34 ms 4784 KB
00_min_03.txt AC 34 ms 4792 KB
00_min_04.txt AC 33 ms 4788 KB
00_min_05.txt AC 33 ms 4788 KB
00_min_06.txt AC 34 ms 4792 KB
00_min_07.txt AC 34 ms 4792 KB
00_min_08.txt AC 34 ms 4788 KB
00_sample_01.txt AC 33 ms 4788 KB
00_sample_02.txt AC 30 ms 4788 KB
00_sample_03.txt AC 30 ms 4792 KB
00_sample_04.txt AC 31 ms 4788 KB
00_sample_05.txt AC 33 ms 4792 KB
01_rnd_00.txt AC 51 ms 5048 KB
01_rnd_01.txt AC 88 ms 5040 KB
01_rnd_02.txt AC 106 ms 5088 KB
01_rnd_03.txt AC 84 ms 5084 KB
01_rnd_04.txt AC 91 ms 5044 KB
01_rnd_05.txt AC 52 ms 5044 KB
01_rnd_06.txt AC 107 ms 5048 KB
01_rnd_07.txt AC 105 ms 5048 KB
01_rnd_08.txt AC 52 ms 5044 KB
01_rnd_09.txt AC 52 ms 5044 KB
01_rnd_10.txt AC 81 ms 5088 KB
01_rnd_11.txt AC 51 ms 5048 KB
01_rnd_12.txt AC 97 ms 5076 KB
01_rnd_13.txt AC 98 ms 5048 KB
01_rnd_14.txt AC 56 ms 5048 KB
01_rnd_15.txt AC 106 ms 5044 KB
01_rnd_16.txt AC 53 ms 5044 KB
01_rnd_17.txt AC 90 ms 5096 KB
01_rnd_18.txt AC 52 ms 5040 KB
01_rnd_19.txt AC 84 ms 5044 KB
02_rndhard_00.txt AC 53 ms 5092 KB
02_rndhard_01.txt AC 53 ms 5048 KB
02_rndhard_02.txt AC 64 ms 5044 KB
02_rndhard_03.txt AC 63 ms 5048 KB
02_rndhard_04.txt AC 72 ms 5044 KB
02_rndhard_05.txt AC 64 ms 5048 KB
02_rndhard_06.txt AC 57 ms 5044 KB
02_rndhard_07.txt AC 58 ms 5044 KB
02_rndhard_08.txt AC 57 ms 5044 KB
02_rndhard_09.txt AC 58 ms 5040 KB
02_rndhard_10.txt AC 57 ms 5064 KB
02_rndhard_11.txt AC 58 ms 5036 KB
02_rndhard_12.txt AC 54 ms 5040 KB
02_rndhard_13.txt AC 56 ms 5096 KB
02_rndhard_14.txt AC 60 ms 5044 KB
02_rndhard_15.txt AC 57 ms 5048 KB
02_rndhard_16.txt AC 60 ms 5044 KB
02_rndhard_17.txt AC 57 ms 5048 KB
02_rndhard_18.txt AC 56 ms 5044 KB
02_rndhard_19.txt AC 55 ms 5044 KB
02_rndhard_20.txt AC 55 ms 5048 KB
02_rndhard_21.txt AC 54 ms 5048 KB
02_rndhard_22.txt AC 55 ms 5100 KB
02_rndhard_23.txt AC 54 ms 5044 KB
02_rndhard_24.txt AC 60 ms 5044 KB
02_rndhard_25.txt AC 57 ms 5044 KB
02_rndhard_26.txt AC 52 ms 5048 KB
02_rndhard_27.txt AC 51 ms 5044 KB
02_rndhard_28.txt AC 52 ms 5044 KB
02_rndhard_29.txt AC 52 ms 5048 KB
02_rndhard_30.txt AC 51 ms 5048 KB
02_rndhard_31.txt AC 51 ms 5044 KB
02_rndhard_32.txt AC 57 ms 5096 KB
02_rndhard_33.txt AC 55 ms 5096 KB
02_rndhard_34.txt AC 56 ms 5096 KB
02_rndhard_35.txt AC 55 ms 5040 KB
02_rndhard_36.txt AC 59 ms 5044 KB
02_rndhard_37.txt AC 56 ms 5044 KB
02_rndhard_38.txt AC 56 ms 5044 KB
02_rndhard_39.txt AC 52 ms 5044 KB
03_rndhardsmall_00.txt AC 34 ms 4788 KB
03_rndhardsmall_01.txt AC 32 ms 4784 KB
03_rndhardsmall_02.txt AC 33 ms 4788 KB
03_rndhardsmall_03.txt AC 33 ms 4792 KB
03_rndhardsmall_04.txt AC 32 ms 4784 KB
03_rndhardsmall_05.txt AC 33 ms 4828 KB
03_rndhardsmall_06.txt AC 32 ms 4784 KB
03_rndhardsmall_07.txt AC 31 ms 4788 KB
03_rndhardsmall_08.txt AC 33 ms 4788 KB
03_rndhardsmall_09.txt AC 33 ms 4888 KB