NYOJ 92

1、深搜(会爆栈,通过开全局栈模拟递归)

爆栈代码

# include<iostream>
# include<string>
# include<string.h>
# include<queue>
# include<stdio.h>
# include<math.h>
#include <algorithm>
using namespace std;
int d[2001][2001]; 
void dfs(int i,int j)
{
    if(d[i][j]==0)    return;
    else
    {
        d[i][j] = 0;
        dfs(i-1,j);
        dfs(i+1,j);
        dfs(i,j-1);
        dfs(i,j+1);
    }
}
int main()
{
      int n,m,i,j,w,h;
      cin>>n;
      while(n--)
      {
          cin>>w>>h;

        d[0][0] = 1;
       d[0][h+1] = 1;
       d[w+1][0] = 1;
       d[w+1][h+1] = 1;

for(i=1;i<=w;i++)
          {
              for(j=1;j<=h;j++)
            {
                if(i==1)            d[i-1][j] = 1;
                else if(i==w)        d[i+1][j] = 1;
                else if(j==1)        d[i][j-1] = 1;
                else if(j==h)        d[i][j+1] = 1;
                scanf("%d",&d[i][j]);    
            }    
        }
        int flag = 0;
        for(i=1;i<=w;i++)
          {
              for(j=1;j<=h;j++)
            {
                if(d[i][j]!=0 && (i==1||i==w||j==1||j==h))
                {
                    dfs(i,j);
                    break;
                }
            }    
            if(flag == 1)    break;
        }
        
        for(i=1;i<=w;i++)
          {
              for(j=1;j<=h;j++)
            {
                printf("%d ",d[i][j]);    
            }    
            printf("\n"); 
        }
        
    }
    return 0;
} 

2、广搜(注意w,h输入顺序 先输入h后输入w)

# include<iostream>
# include<string>
# include<string.h>
# include<queue>
# include<stdio.h>
# include<math.h>
#include <algorithm>
using namespace std;
int d[2001][2001]; 
struct Node
{
    int x,y;
}node,temp,f[4];
queue<Node> q;
void bfs(int x,int y,int w,int h)
{
    node.x = x;
    node.y = y; 
    q.push(node);
    d[x][y] = 0;
    while(!q.empty())
    {
        temp = q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int t1,t2;
            t1 = temp.x + f[i].x;
            t2 = temp.y + f[i].y;
            if(t1>=0 && t1<=w+1 && t2>=0 && t2<=h+1 && d[t1][t2]!=0)
            {
                d[t1][t2] = 0;
                node.x = t1;
                node.y = t2; 
                q.push(node);
            }
        }
    }
}
int main()
{
      int n,m,i,j,w,h;
      cin>>n;
      f[0].x = 0;f[0].y = 1;
      f[1].x = 0;f[1].y = -1;
      f[2].x = 1;f[2].y = 0;
      f[3].x = -1;f[3].y = 0;
      while(n--)
      {
          //cin>>w>>h;
          cin>>h>>w;
          d[0][0] = 1;
          d[0][h+1] = 1;
          d[w+1][0] = 1;
          d[w+1][h+1] = 1;
          for(i=1;i<=w;i++)
          {
              for(j=1;j<=h;j++)
            {
                if(i==1)            d[i-1][j] = 1;
                else if(i==w)        d[i+1][j] = 1;
                if(j==1)            d[i][j-1] = 1;
                else if(j==h)        d[i][j+1] = 1;
                scanf("%d",&d[i][j]);    
            }    
        }
        bfs(0,0,w,h);
        for(i=1;i<=w;i++)
          {
              for(j=1;j<=h;j++)
            {
                printf("%d ",d[i][j]);    
            }    
            printf("\n"); 
        }
        
    }
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/fzuhyj/p/9483819.html
92