Getting error while using pair

question from leetcode: https://leetcode.com/problems/rotting-oranges/

my code: https://ide.codingblocks.com/s/436556

getting error while using pair in map. pls help…

hello @aman17nov1999

u dont have to use ( …) instead use [ …]

i.e
visite[ your pair ] ==2

sorry it was visited[…].
in this also i m facing error

ok so in ur visited map .
the key is a pair right? which is storing (x,y) coordintes

here for(auto p : visited) will return key : value pair

so if u just want to access key.
u just have to use p.first thats it, p.second will give u value corresponding to that key , which is not required.

apart from that there is logical mistake too.

the idea is to first push all starting vertices in queue and then start doing bfs, but here u are picking one vertices at a time, and running bfs , which will not work.

sir, i have put all elements in visited map first. and traversing on it for bfs

thats the issue na, from ur map u will pick one element at a time and start bfs from that node here which will give wrong result.
for example->
[2,1,1,2] here u will put all two in ur map

then u start bfs from first 2 ,
[-1,1,1,2] -> [-1,-1,1,2] -> [-1,-1,-1,2] (2 steps here)

but actualy u have to do something like
[-1,1,1,-1] -> [-1,-1,-1,-1] (1 step only)

see first put all ur rooten oranges in queue.
and run ur ordinary bfs on that queue thats it.
use visited map to keep track of visited states.

refer this for clarity->

  int orangesRotting(vector<vector<int>>& grid) {
        int ct=0, res=-1;
        queue<vector<int>> q;
        vector<vector<int>> dir={{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for(int i=0;i<grid.size();i++) {
            for(int j=0;j<grid[0].size();j++) {
                if(grid[i][j]>0) ct++;
                if(grid[i][j]==2) q.push({i, j});
            }
        }
        while(!q.empty()) {
            res++;
            int size=q.size();
            for(int k=0;k<size;k++) {
                vector<int> cur=q.front();
                ct--;
                q.pop();
                for(int i=0;i<4;i++) {
                    int x=cur[0]+dir[i][0], y=cur[1]+dir[i][1];
                    if(x>=grid.size()||x<0||y>=grid[0].size()||y<0||grid[x][y]!=1) continue;
                    grid[x][y]=2;
                    q.push({x, y});
                }
            }
        }
        if(ct==0) return max(0, res);
        return -1;
    }

ok sir. i got it.
thnx a lot.

can i get ur phone no. sir…

for what ?
if it is for any dsa related help then u can always use ask doubt feature i will be happy to help u.

if any other reason. then u can connect me on linkedin my handle is aman212yadav

i was just asking because my course duration is going to be over in few days.

if u r practing on leetcode then i think leetcode discuss section is the best resource to resolve ur doubt (it has good explanations).
most of the student/working professionals do the same thing.
they try some approach , if it doesnt work they read discuss section .

okay sir ji…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.