Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct kolejka{
  5. int x;
  6. int y;
  7. int c;
  8. struct kolejka *next;
  9. }kolejka;
  10.  
  11. void print(kolejka* first) {
  12. while (first != NULL) {
  13. printf("%d ", first->c);
  14. first = first->next;
  15. }
  16. }
  17.  
  18. void add(int x, int y, int c, kolejka* obraz){
  19. kolejka *tmp = malloc(3*sizeof(int)+sizeof(kolejka*));
  20. while(obraz->next != NULL){
  21. obraz=obraz->next;
  22. }
  23. tmp->x = x;
  24. tmp->y = y;
  25. tmp->c = c;
  26. tmp->next = NULL;
  27. obraz->next = tmp;
  28. }
  29.  
  30.  
  31. kolejka * usun(kolejka *a){
  32. kolejka *w =a;// zapamietujemy to co usuwamy
  33. a=a->next; // przeskakujemy usuwany element
  34. free(w); // zwalniamy pamiec przeskoczonego elementu
  35. return a; // tak spreparowana liste wzracamy
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42. int main(){
  43. int n, k;
  44. scanf("%d %d", &n, &k);
  45. int **tab;
  46. tab=(int**)malloc(n*sizeof(int*));
  47. for(int i=0; i<n; i++){
  48. tab[i]=(int*)malloc(n*sizeof(int));
  49. }
  50. for(int i=0 ; i<n ; i++){
  51. for(int j=0 ; j<n ; j++){
  52. int s;
  53. scanf("%d", &s);
  54. tab[i][j] = s;
  55. }
  56. }
  57. kolejka* obraz = malloc(3*sizeof(int)+sizeof(kolejka*));
  58. int x, y, c;
  59. scanf("%d %d %d", &x, &y, &c);
  60. obraz->x = x;
  61. obraz->y = y;
  62. obraz->c = c;
  63. obraz->next = NULL;
  64. int i=1;
  65. while(i<k){
  66. int x, y, c;
  67. scanf("%d %d %d", &x, &y, &c);
  68. add(x,y,c,obraz);
  69. i++;
  70. }
  71. while(obraz!=NULL){
  72. if(tab[obraz->x][obraz->y]==0){
  73. tab[obraz->x][obraz->y]=obraz->c;
  74. if(obraz->x+1 < n && tab[obraz->x+1][obraz->y]==0){
  75. add(obraz->x+1,obraz->y,obraz->c,obraz);
  76. }
  77. if(obraz->x-1 > 0 && tab[obraz->x-1][obraz->y]==0){
  78. add(obraz->x-1,obraz->y, obraz->c,obraz);
  79. }
  80. if(obraz->y+1 < n && tab[obraz->x][obraz->y+1]==0){
  81. add(obraz->x,obraz->y+1, obraz->c,obraz);
  82. }
  83. if(obraz->y-1 > 0 && tab[obraz->x][obraz->y-1]==0){
  84. add(obraz->x,obraz->y-1, obraz->c,obraz);
  85. }
  86. }
  87. obraz = usun(obraz);
  88. }
  89. for(int i=0; i<n; i++){
  90. for(int j=0; j<n; j++){
  91. printf("%d ",tab[i][j]);
  92. }
  93. printf("\n");
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement