답안 #257958

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
257958 2020-08-05T06:26:08 Z 최은수(#5047) Sandwich (JOI16_sandwich) C++17
0 / 100
1 ms 512 KB
#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18+7;
int n,m;
char map[420][420];
int par;
int chk[420][420];
int dfs(int x,int y)
{
    if(x<=0||x>n||y<=0||y>m)
        return 0;
    chk[x][y]=par+1;
    bool ch[4];
    for(int i=0;i<4;i++)
        ch[i]=0;
    int ans=2;
    for(int i=-2;i<2;i++)
    {
        int nx=x+i%2;
        int ny=y+(i+1)%2;
        if(chk[nx][ny]!=par+1)
            ch[i+2]=1;
    }
    if(map[x][y]=='N')
    {
        if(ch[1]&&ch[2])
        {
            int r=chk[x-1][y]==par+2?0:dfs(x-1,y);
            ans+=r;
            if(r==-1)
                return -1;
            r=chk[x][y+1]==par+2?0:dfs(x,y+1);
            ans+=r;
            if(r==-1)
                return -1;
        }
        else if(ch[0]&&ch[3])
        {
            int r=chk[x][y-1]==par+2?0:dfs(x,y-1);
            ans+=r;
            if(r==-1)
                return -1;
            r=chk[x+1][y]==par+2?0:dfs(x+1,y);
            ans+=r;
            if(r==-1)
                return -1;
        }
        else
            return -1;
    }
    else
    {
        if(ch[0]&&ch[1])
        {
            int r=chk[x][y-1]==par+2?0:dfs(x,y-1);
            ans+=r;
            if(r==-1)
                return -1;
            r=chk[x-1][y]==par+2?0:dfs(x-1,y);
            ans+=r;
            if(r==-1)
                return -1;
        }
        else if(ch[2]&&ch[3])
        {
            int r=chk[x][y+1]==par+2?0:dfs(x,y+1);
            ans+=r;
            if(r==-1)
                return -1;
            r=chk[x+1][y]==par+2?0:dfs(x+1,y);
            ans+=r;
            if(r==-1)
                return -1;
        }
        else
            return -1;
    }
    chk[x][y]=par+2;
    return ans;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>m;
    for(int i=0;i++<n;)
        for(int j=0;j++<m;)
            cin>>map[i][j];
    for(int i=0;i++<n;cout<<endl)
    {
        for(int j=0;j++<m;)
        {
            int ans=inf;
            chk[i][j]=par+1;
            int r=dfs(i,j-1);
            if(r!=-1)
            {
                int r2=dfs(i+(map[i][j]=='Z'?-1:1),j);
                if(r2!=-1)
                    ans=min(ans,r2+r);
            }
            par+=2;
            chk[i][j]=par+1;
            r=dfs(i,j+1);
            if(r!=-1)
            {
                int r2=dfs(i+(map[i][j]=='Z'?1:-1),j);
                if(r2!=-1)
                    ans=min(ans,r2+r);
            }
            par+=2;
            cout<<(ans==inf?-1:ans+2)<<' ';
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Incorrect 1 ms 512 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Incorrect 1 ms 512 KB Output isn't correct
6 Halted 0 ms 0 KB -