페이지

2423번: 전구를 켜라

https://www.acmicpc.net/problem/2423


#include<stdio.h>
#include<algorithm>
using namespace std;
const int MAX_N = 500, fx[] = { -1, 1, 1, -1 }, fy[] = { 1, 1, -1, -1 };
int n, m, map[MAX_N + 1][MAX_N + 1][4], dist[MAX_N + 1][MAX_N + 1];
struct st {
    int x, y, w;
    st() {}
    st(int _x, int _y, int _w)
        :x(_x), y(_y), w(_w) {}
}q[(MAX_N + 1)*(MAX_N + 1)];
int h = 0, t = 0;
void dfs(int x, int y, int w) {
    dist[x][y] = w;
    for (int i = 0; i < 4; i++) {
        int tx = x + fx[i], ty = y + fy[i];
        if (tx<0 || ty<0 || tx>n || ty>m || dist[tx][ty] != 0x7fffffff) continue;
        if (map[x][y][i]) dfs(tx, ty, w);
        else q[t++] = st(tx, ty, w + 1);
    }
}
int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            char c;
            scanf("\n%c", &c);
            if (c == '/') map[i][j + 1][2] = map[i + 1][j][0] = 1;
            else map[i][j][1] = map[i + 1][j + 1][3] = 1;
        }
    }
    fill(&dist[0][0], &dist[MAX_N][MAX_N + 1], 0x7fffffff);
    q[t++] = st(0, 0, 0);
    while (h != t) {
        if (dist[q[h].x][q[h].y] == 0x7fffffff)
            dfs(q[h].x, q[h].y, q[h].w);
        h++;
    }
    if (dist[n][m] == 0x7fffffff) puts("NO SOLUTION");
    else printf("%d", dist[n][m]);
    return 0;
}

댓글 없음 :

댓글 쓰기