NA (score:50%) 有人可以幫忙一下嗎,一直卡在記憶體錯誤,應該是讀到陣列外面去了,但是檢查dfs應該又沒問題 #include<stdio.h> #include<string.h> char arr[251][251], color, cmd, old, name[100], garbage[100]; int m, n, x1, y1, x2, y2; void dfs(int x, int y){ if (y < 0 || y >= n || x < 0 || x >= m || arr[y][x] != old) return; arr[y][x] = color; dfs(x, y - 1); dfs(x - 1, y); dfs(x, y + 1); dfs(x + 1, y); } void swap(int *a, int *b){ int temp; temp = *a; *a = *b; *b = temp; } int main(){ while (1){ scanf(" %c", &cmd); if (cmd == 'I'){ scanf(" %d %d", &m, &n); memset(arr, 'O', sizeof(arr)); } else if (cmd == 'C') memset(arr, 'O', sizeof(arr)); else if (cmd == 'L'){ scanf(" %d %d %c", &x1, &y1, &color); arr[y1 - 1][x1 - 1] = color; } else if (cmd == 'V'){ scanf(" %d %d %d %c", &x1, &y1, &y2, &color); if (y1 > y2) swap(&y1, &y2); for (int i = y1 - 1; i < y2; i++){ arr[i][x1 - 1] = color; } } else if (cmd == 'H'){ scanf(" %d %d %d %c", &x1, &x2, &y1, &color); if (x1 > x2) swap(&x1, &x2); for (int i = x1 - 1; i < x2; i++){ arr[y1 - 1][i] = color; } } else if (cmd == 'K'){ scanf(" %d %d %d %d %c", &x1, &y1, &x2, &y2, &color); if (y1 > y2) swap(&y1, &y2); if (x1 > x2) swap(&x1, &x2); for (int i = y1 - 1; i < y2; i++){ for (int j = x1 - 1; j < x2; j++){ arr[i][j] = color; } } } else if (cmd == 'F'){ scanf(" %d %d %c", &x1, &y1, &color); x1--; y1--; if (y1 < 0 || y1 >= n || x1 < 0 || x1 >= m) continue; old = arr[y1][x1]; dfs(x1, y1); } else if (cmd == 'S'){ scanf(" %s", &name); puts(name); for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ putchar(arr[i][j]); } putchar('\n'); } } else if (cmd == 'X') return 0; else gets(garbage); }
}
|