[Solution] problem wolves and sheep -C ++

Description
Mickey possessor backyard flock, Mickey asleep due to fatigue, when got into the backyard group wolves start of the herd, the backyard is composed of many rectangular region composed of squares, each square '?' means open space with character, '#' indicates the fence, 'o' represents the sheep, 'v' represents the lattice wolf, the sheep and the wolf are located open space. If a space A from the horizontal direction or the vertical direction through a series of open space can reach space B, Site A and Site B is said to belong to the same fold. To be able to escape from the backyard open space we think it does not belong to any one sheepfold. When the number of sheep in the fold greater than a wolf, they will use their sharp corners top dead wolf in the sheepfold, otherwise it will be eaten by wolves, the end of each sheepfold will leave an animal. Total number of sheep and wolves all in the fold after the end of the program to write a statistical battle.
Input
The first line contains two natural numbers separated by a space R and C, where 3 <= R, C <= 250, R represents a backyard Mickey line, C denotes the number of columns, the next R lines
per row contains C characters, each case represents a grid.
Output
only one comprising two spaced apart by a space integer representing the number of wolves and sheep requirements.
Sample Input

9 12
.###.#####..
#.oo#...#v#.
#..o#.#.#.#.
#..##o#...#.
#.#v#o###.#.
#..#v#....#.
#...v#v####.
.####.#vv.o#
.......####.

Sample Output

3 5

This question emm, although BFS water problem, but there is a point card me a long time.
The beginning of my code always TLE, to find a room big brothers of view I can not see what's wrong, probably thinking is this:

Input -> traverse the map again, not found on the # bfs-> bfs this one counted the number of sheep and wolves -> the adjacent not have become # # -> Output Statistics

Finally I found my code which counts and become # This section is placed at the beginning bfs in while, that is, when the head of the treatment team was counting change #, so TLE, ...
then this one change to into the queue when operating, actually 24ms water passed!
Code is not standardized, WA two lines of tears!
The following is the code portion emm

#include<bits/stdc++.h>
using namespace std;
int n,m;
int lang,yang;//awa
char mp[258][258];
struct node
{
    int x,y;
};
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void bfs(int x,int y)
{
    int nl=0,ny=0;//now_lang,now_yang
    queue<node> q;
    q.push((node){x,y});
    if(mp[x][y]=='o')ny++;
    else if(mp[x][y]=='v')nl++;
    mp[x][y]='#';
    while(!q.empty())
    {
        node now=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int tx=now.x+dir[i][0],ty=now.y+dir[i][1];
            if(mp[tx][ty]!='#'&&1<=tx&&tx<=n&&1<=ty&&ty<=m)
            {
                q.push((node){tx,ty});
                if(mp[tx][ty]=='o')ny++;
                else if(mp[tx][ty]=='v')nl++;
                mp[tx][ty]='#';
            }
        }
    }
    if(ny>nl)yang+=ny;
    else lang+=nl;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    { 
        for(int j=1;j<=m;j++)
        {
            cin>>mp[i][j];
        }
    }   
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(mp[i][j]!='#')bfs(i,j);
//  for(int i=1;i<=n;i++)
//  {
//      for(int j=1;j<=m;j++)
//      {
//          cout<<mp[i][j];
//      }
//      cout<<endl;
//  }
            
    cout<<yang<<" "<<lang<<endl;
    return 0;
}

ps terrible awa portion skip own

Guess you like

Origin www.cnblogs.com/moyujiang/p/11225621.html