Why vs2010 is correct but OJ is WA ? I cann't find out reason , please help me


  • 0
    C

    Error message:
    Input["1","1"];
    Output:2
    Expected:1
    But, when I test {'1','1'} in local vs2010, and CodeBlocks, its output is 1. I don't know why?

     int numIslands(vector<vector<char>> &grid) {
    if(grid.empty())return 0;
    int m=grid.size();//lines
    int n=grid[0].size();//cols
    int count=0;
    queue<pair<int,int>> seq;
    while(!seq.empty())seq.pop();
    vector<bool> gridvisited(m*n,false);
    for(int i=0;i<m;i++){
    	for(int j=0;j<n;j++){
    		if(gridvisited[i*n+j]==false&&grid[i][j]=='1'){
    			count++;
    			gridvisited[i*n+j]=true;
    			seq.push(make_pair(i,j));
    			while(!seq.empty()){
    				pair<int,int> tmp=seq.front();//队首元素
    				seq.pop();//删除队首元素
    				if((tmp.second+1)<n&&grid[tmp.first][tmp.second+1]=='1'&&gridvisited[tmp.first*n+tmp.second+1]==false){//right
    					gridvisited[tmp.first*n+tmp.second+1]=true;
    					seq.push(make_pair(tmp.first,tmp.second+1));//将此元素插入队列
    				}
    				if((tmp.first+1)<m &&grid[tmp.first][tmp.second+1]=='1'&&gridvisited[(tmp.first+1)*n+tmp.second]==false){//down
    					gridvisited[(tmp.first+1)*n+tmp.second]=true;
    					seq.push(make_pair(tmp.first+1,tmp.second));//插入队列
    				}
    			}}
    		else if(gridvisited[i*n+j]==false)gridvisited[i*n+j]=true;
    		else continue;
    	}
    }
    return count;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.