1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | int count_one( int mines[][5], int row, int col, int x, int y) { int start_x = x - 1; int end_x = x + 1; int start_y = y - 1; int end_y = y + 1; int i, j, count = 0; if (start_x < 0) start_x = 0; if (end_x >= col) end_x = col - 1; if (start_y < 0) start_y = 0; if (end_y >= row) end_y = row - 1; for (i = start_x; i <= end_x; i++){ for (j = start_y; j <= end_y; j++){ count += mines[j][i]; } } return count; } int main( void ) { srand ( time (NULL)); int mines[5][5], i, j, x, y; for (i = 0; i < 5; i++){ for (j = 0; j < 5; j++){ int temp = rand () % 2; cout << temp << " " ; mines[i][j] = temp; } cout << endl; } do { // 사용자 입력 // cin 을 사용해서 x, y에 위치 입력 cin >> x >> y; if (x == -1 || y == -1) break ; // 함수 호출 cout << "1의 개수는 " << count_one(mines, 5, 5, x, y) << endl; } while ( true ); return 0; } |