Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdlib.h>
- typedef struct kolejka{
- int x;
- int y;
- int c;
- struct kolejka *next;
- }kolejka;
- void print(kolejka* first) {
- while (first != NULL) {
- printf("%d ", first->c);
- first = first->next;
- }
- }
- void add(int x, int y, int c, kolejka* obraz){
- kolejka *tmp = malloc(3*sizeof(int)+sizeof(kolejka*));
- while(obraz->next != NULL){
- obraz=obraz->next;
- }
- tmp->x = x;
- tmp->y = y;
- tmp->c = c;
- tmp->next = NULL;
- obraz->next = tmp;
- }
- kolejka * usun(kolejka *a){
- kolejka *w =a;// zapamietujemy to co usuwamy
- a=a->next; // przeskakujemy usuwany element
- free(w); // zwalniamy pamiec przeskoczonego elementu
- return a; // tak spreparowana liste wzracamy
- }
- int main(){
- int n, k;
- scanf("%d %d", &n, &k);
- int **tab;
- tab=(int**)malloc(n*sizeof(int*));
- for(int i=0; i<n; i++){
- tab[i]=(int*)malloc(n*sizeof(int));
- }
- for(int i=0 ; i<n ; i++){
- for(int j=0 ; j<n ; j++){
- int s;
- scanf("%d", &s);
- tab[i][j] = s;
- }
- }
- kolejka* obraz = malloc(3*sizeof(int)+sizeof(kolejka*));
- int x, y, c;
- scanf("%d %d %d", &x, &y, &c);
- obraz->x = x;
- obraz->y = y;
- obraz->c = c;
- obraz->next = NULL;
- int i=1;
- while(i<k){
- int x, y, c;
- scanf("%d %d %d", &x, &y, &c);
- add(x,y,c,obraz);
- i++;
- }
- while(obraz!=NULL){
- if(tab[obraz->x][obraz->y]==0){
- tab[obraz->x][obraz->y]=obraz->c;
- if(obraz->x+1 < n && tab[obraz->x+1][obraz->y]==0){
- add(obraz->x+1,obraz->y,obraz->c,obraz);
- }
- if(obraz->x-1 > 0 && tab[obraz->x-1][obraz->y]==0){
- add(obraz->x-1,obraz->y, obraz->c,obraz);
- }
- if(obraz->y+1 < n && tab[obraz->x][obraz->y+1]==0){
- add(obraz->x,obraz->y+1, obraz->c,obraz);
- }
- if(obraz->y-1 > 0 && tab[obraz->x][obraz->y-1]==0){
- add(obraz->x,obraz->y-1, obraz->c,obraz);
- }
- }
- obraz = usun(obraz);
- }
- for(int i=0; i<n; i++){
- for(int j=0; j<n; j++){
- printf("%d ",tab[i][j]);
- }
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement