5 条题解

  • 2
    @ 2023-1-6 11:59:11
    #include<iostream>
    #include<string.h>
    #include<queue>
    using namespace std;
    struct id
    {
    	int x,y,s;
    };
    queue<id>q;
    int n,m,x,y,sx,sy,ans,k;
    int xx[8]={-1,-1,-1,0,0,1,1,1};
    int yy[8]={-1,0,1,-1,1,-1,0,1};
    bool qp[110][110];
    char ch;
    int main()
    {
    	memset(qp,false,sizeof(qp));
    	cin>>m>>n>>x>>y;
    	for(int i=1;i<=n;i++)
    	{
    		for(int j=1;j<=m;j++)
    		{
    			cin>>ch;
    			if(ch=='.')qp[i][j]=true;
    		}
    	}
    	sy=x;
    	sx=n-y+1;
    	q.push(id{sx,sy,0});
    	qp[sx][sy]=false;
    	while(!q.empty())
    	{
    		int tx=q.front().x;
    		int ty=q.front().y;
    		ans=q.front().s;
    		q.pop();
    		for(int i=0;i<8;i++)
    		{
    			int nx=tx+xx[i];
    			int ny=ty+yy[i];
    			if(qp[nx][ny])
    			{
    				q.push(id{nx,ny,ans+1});
    				qp[nx][ny]=false;
    			}
    		}
    	}
    	cout<<ans;
    	return 0;
    }
    
    • 1
      @ 2022-7-28 9:46:15

      不用广搜,直接暴力居然可以AC

      #include <iostream>
      using namespace std;
      
      char a[101][101];
      int m,n,x,y;
      
      void kuo()
      {
      	for(int i = 1;i <= n;i++)
      	{
      		for(int j = 1;j <= m;j++)
      		{
      			if(a[i][j] == 'M')
      			{
      				if(a[i][j + 1] == '.') a[i][j + 1] = 'A';
      				if(a[i][j - 1] == '.') a[i][j - 1] = 'A';
      				if(a[i + 1][j] == '.') a[i + 1][j] = 'A';
      				if(a[i - 1][j] == '.') a[i - 1][j] = 'A';
      				
      				if(a[i + 1][j + 1] == '.') a[i + 1][j + 1] = 'A';
      				if(a[i - 1][j + 1] == '.') a[i - 1][j + 1] = 'A';
      				if(a[i - 1][j - 1] == '.') a[i - 1][j - 1] = 'A';
      				if(a[i + 1][j - 1] == '.') a[i + 1][j - 1] = 'A';
      			}
      		}
      	}
      	for(int i = 1;i <= n;i++)
      	{
      		for(int j = 1;j <= m;j++)
      		{
      			if(a[i][j] == 'A')
      			{
      				a[i][j] = 'M';
      			}
      		}
      	}
      }
      
      bool check()
      {
      	for(int i = 1;i <= n;i++)
      	{
      		for(int j = 1;j <= m;j++)
      		{
      			if(a[i][j] == '.')
      			{
      				return false;
      			}
      		}
      	}
      	return true;
      }
      
      int main()
      {
      	cin >> m >> n >> x >> y;
      	for(int i = 1;i <= n;i++)
      	{
      		for(int j = 1;j <= m;j++)
      		{
      			cin >> a[i][j];
      		}
      	}
      	a[n - y + 1][x] = 'M';
      	for(int ans = 0;;ans++)
      	{
      		if(check())
      		{
      			cout << ans << endl;
      			return 0;
      		}
      		kuo();
      	}
      	return 0;
      }
      
    • 0
      @ 2022-7-28 10:37:04

      名副其实的广搜题, 但是我仍然不会 废话不多说,直接上代码

      #include <iostream>
      #include <queue>
      using namespace std;
      int r, c, x, y;
      int dir[8][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
      char ma[105][105];
      bool vis[105][105];
      int ans = 0;
      struct node{
      	int x, y, s;
      };
      queue<node > q;
      void bfs(int x, int y){
      	vis[x][y] = 1;
      	q.push((node){x, y, 0});
      	while(!q.empty()){
      		node p = q.front();
      		q.pop();
      		ans = max(ans, p.s);
      		for(int i=0; i<8; i++){
      			int nx = p.x + dir[i][0];
      			int ny = p.y + dir[i][1];
      			if(1 <= nx && nx <= r && 1 <= ny && ny <= c){
      				if(vis[nx][ny] == 0 && ma[nx][ny] != '*'){
      					vis[nx][ny] = 1;
      					q.push((node){nx, ny, p.s + 1});
      				}
      			}
      		}
      	}
      }
      int main(){
      	cin>>c>>r>>x>>y;
      	for(int i=1; i<=r; i++){
      		for(int j=1; j<=c; j++){
      			cin>>ma[i][j];
      		}
      	}
      	bfs(r - y + 1, x);
      	cout<<ans<<endl;
      }
      
      • 0
        @ 2022-3-25 20:58:52
        #include<iostream>  
        #include<cstring>  
        #include<cstdio>  
        using namespace std;  
        int sx[8]={1,1,1,0,0,-1,-1,-1};  
        int sy[8]={-1,0,1,-1,1,-1,0,1};  
        int x,y,mx,my,nowx,nowy,nowstep,head,tail,n,m,ans;  
        char s[105];  
        struct hp{  
            int x,y,step;  
        }queue[10005];  
        int a[105][105];   
        int main(){  
            scanf("%d%d%d%d\n",&n,&m,&mx,&my);  
            swap(n,m); swap(mx,my);  
            for (int i=1;i<=n;++i){  
                gets(s);  
                for (int j=1;j<=m;++j)  
                  if (s[j-1]=='*') a[i][j]=1;  
            }  
            a[mx][my]=1;  
            head=0,tail=1,queue[tail].x=mx,queue[tail].y=my;  
            while (head<tail){  
                head++;  
                nowx=queue[head].x,nowy=queue[head].y,nowstep=queue[head].step;  
                for (int i=0;i<8;++i){  
                    x=nowx+sx[i],y=nowy+sy[i];  
                    if (x>0&&x<=n&&y>0&&y<=m&&!a[x][y]){  
                        a[x][y]=1;  
                        tail++;  
                        queue[tail].x=x,queue[tail].y=y;  
                        queue[tail].step=nowstep+1;  
                        ans=max(ans,queue[tail].step);  
                    }  
                }  
            }  
            printf("%d",ans);  
        }
        
        • 0
          @ 2021-8-7 21:15:25

          C++ :

          #include<iostream>
          #include<algorithm>
          #include<queue>
          using namespace std;
          const int maxn = 120;
          int X, Y, Mx, My;
          struct node{
              int r,c,s;
              node(int r,int c,int s):r(r),c(c),s(s){}
          };
          int dir[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
          char m[maxn][maxn];
          bool visited[maxn][maxn];
          int s;
          void bfs(int r, int c)
          {
              queue<node> q;
              visited[r][c] = true;
              q.push(node(r, c, 0));
              while(!q.empty()){
                  const node&cur=q.front();
                  s=cur.s;
                  for(int i=0;i<8; ++i){
                      int nr = cur.r + dir[i][0];
                      int nc = cur.c + dir[i][1];
                      if(0 < nr && nr <= Y && 0 < nc && nc <= X)
                          if(m[nr][nc] == '.' && !visited[nr][nc]){
                              visited[nr][nc] = true;
                              q.push(node(nr, nc, cur.s + 1));
                          }
                  }
                  q.pop();
              }
          } 
          int main(){
              cin>>X>>Y>>Mx>>My;
              for(int i=Y;i>0;--i)
                  for(int j=0;j<X;)
                      cin>>m[i][++j];
              bfs(My,Mx);
              cout<<s;
              return 0;
          }
          
          • 1

          信息

          ID
          100
          时间
          1000ms
          内存
          128MiB
          难度
          7
          标签
          递交数
          663
          已通过
          137
          上传者