BOJ::14503 로봇 청소기

시사점

이해(3)

설계, 손 코딩(4)

복잡도

구현(15)

#include<bits/stdc++.h>
#define endl '\n'
#define rep(i,a,b) for(int i=a;i<b;i++)
#define r_rep(i,a,b) for(int i=a;i>b;i--)
#define WALL 1
const int MAXNM = 50 + 4;
const int dx[]={-1, 0, 1, 0}, dy[]={0, 1, 0, -1};
const int LE[]={3, 0, 1, 2};
using namespace std;
int n, m;
int x, y, d;
int a[MAXNM][MAXNM];
bool seen[MAXNM][MAXNM];
void input(){
    cin >> n >> m >> x >> y >> d;
    x+=1, y+=1;
    rep(i, 0, n + 2){
        rep(j, 0, m + 2){
            if(i == 0 || j == 0 || i == n+1 || j == m+1){
                seen[i][j] = true;
                continue;
            }
            cin >> a[i][j];
            if(a[i][j]) seen[i][j] = true;
        }
    }
}
void process(){
    input();
    int ans = 1;
    seen[x][y] = true;
    while(true){
        bool found = false;
        rep(i, 0, 4){
            d = LE[d];
            int nx = x+dx[d], ny = y+dy[d];
            if(seen[nx][ny] || a[nx][ny]) continue;
            seen[nx][ny] = true;
            found = true;
            ans++;
            x = nx, y = ny;
            break;
        }
        if(!found){
            int nx = x-dx[d], ny = y-dy[d];
            if(a[nx][ny] == WALL) break;
            x = nx, y = ny;
        }
    }
    cout << ans << endl;
}
int main(){
    freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    process();
    return 0;
}

디버깅(20)

좋은 코드

최적화