4 solutions

  • 0
    @ 2022-4-2 9:00:58

    #include<iostream> #include<vector> #include<string> using namespace std; int W,H,ans=1; vector<vector <string> > A; bool vit[20][20]; void dfs(int x,int y){

    if(y+1<W&&A[x][y+1]=="."&&!vit[x][y+1])
    {
    	ans++;
    	vit[x][y+1]=1;
    	dfs(x,y+1);
    }
    if(x+1<H&&A[x+1][y]!="#"&&!vit[x+1][y])
    {
    	ans++;
    	vit[x+1][y]=1;
    	dfs(x+1,y);
    }
    if(y-1>=0&&A[x][y-1]!="#"&&!vit[x][y-1])
    {
    	ans++;
    	vit[x][y-1]=1;
    	dfs(x,y-1);
    }
    if(x-1>=0&&A[x-1][y]=="."&&!vit[x-1][y])
    {
    	ans++;
    	vit[x-1][y]=1;
    	dfs(x-1,y);
    }
    

    // return; } int main(){ char ch; int x,y; cin>>W>>H; vector<vector <string> > B(H,vector<string> (W)); for(int i=0;i<H;i++) { for(int j=0;j<W;j++) { cin>>ch; B[i][j]=ch; if(B[i][j]=="@") { x=i;y=j; vit[i][j]=1; } } } A=B; dfs(x,y); cout<<ans<<endl; return 0; }

    Information

    ID
    1233
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    4
    Tags
    # Submissions
    175
    Accepted
    80
    Uploaded By