C언어/문제풀다 하나씩
파원처럼 퍼질경우
mcdn
2020. 4. 18. 12:12
반응형
#include<iostream>
using namespace std;
void fillup(int arr[4][4], int y, int x, int now) {
int divide[4][2] = {
{-1,0},
{0,-1}, {0,1},
{1,0},
};
int dy, dx;
for (int i = 0;i < 4;i++) {
dy = y + divide[i][0];
dx = x + divide[i][1];
if (dy >= 0 && dx >= 0 && dy < 4 && dx < 4) {
if (arr[dy][dx] == 0) {
//cout << dy << dx << endl;
arr[dy][dx] = now + 1;
}
}
}
}
int main() {
int y, x;
cin >> y >> x;
int arr[4][4] = { 0 };
arr[y][x] = 1;
int now = 1;
while (1) {
int cnt = 0;
for (int i = 0; i < 4;i++) {
for (int j = 0; j < 4;j++) {
if (arr[i][j] == now) {
cnt += 1;
fillup(arr, i, j, now);
}
}
}
//cout << cnt << endl;
if (cnt == 0) {
break;
}
now++;
//cout << now << endl;
}
for (int i = 0; i < 4;i++) {
for (int j = 0; j < 4;j++) {
cout << arr[i][j] << " ";
}cout << endl;
}
}
어휴 y x i j 헷갈려서 계속 에러남
+ dy dx 의 범위는 바깥으로 나가면 안됨! 에러난당
반응형