From 7d0ceb2c1670fec8b6377aef71d73e33a0a63a01 Mon Sep 17 00:00:00 2001 From: Dhairya0079 <81615643+dhairya244@users.noreply.github.com> Date: Sat, 28 Oct 2023 13:11:18 +0530 Subject: [PATCH] Create FireInTheCell --- Amazon/FireInTheCell | 112 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 Amazon/FireInTheCell diff --git a/Amazon/FireInTheCell b/Amazon/FireInTheCell new file mode 100644 index 0000000..2c4e8ce --- /dev/null +++ b/Amazon/FireInTheCell @@ -0,0 +1,112 @@ +#include + +int fireInTheCells(vector> &mat, int n, int m, int x, int y) { + + vector> fire(n,vector(m,-1)); + + queue>> q; + + for(int i = 0;i= 0 && ncol >= 0 && nrow < n && ncol < m) { + + if(fire[nrow][ncol] == -1){ + + q.push({count+1,{nrow,ncol}}); + + fire[nrow][ncol] = count+1; + + } + + } + + } + + } + + + + + if(fire[x][y]==0 && x!=0 && y!=0 && x!=n-1 && y!=m-1) return -1; + + queue>> q2; + + q2.push({0,{x,y}}); + + while(!q2.empty()){ + + int xcor = q2.front().second.first; + + int ycor = q2.front().second.second; + + int count = q2.front().first; + + q2.pop(); + +if((xcor==0 && ycor!=0 && ycor!=m-1) || (xcor==n-1 && ycor!=0 && ycor!=m-1) || + + (xcor!=0 && ycor==0 && xcor!=n-1) || (xcor!=n-1 && xcor!=0 && ycor==m-1)) + + { + + return count; + + } for(int i = 0;i<4;i++){ + + int nrow = drow[i]+xcor; + + int ncol = dcol[i]+ycor; + + if (nrow >= 0 && ncol >= 0 && nrow < n && ncol < m) { + + if(fire[nrow][ncol] > count+1){ + + q2.push({count+1,{nrow,ncol}}); + + } + + } + + } + + } + + return -1; + + // Write your code here. + +}