# CARLOT why getting WA

can anybody tell me where iam getting WA

my solution for problem : CodeChef: Practical coding for everyone

solution : CodeChef: Practical coding for everyone

Have you Considered the case when there are no cars on a floor between your starting and ending floors?

yes it should give 0

COnsider this case
1
5 4
N N N N
N N P N
N N N N
N N N N
N N P N

answer is 1 for this test case according to my solution.

but what shall be the actual answer I think 3.

no it should be 1 because it is given in question

If the parking lot is empty, then the time taken is considered to be 0.

Considering the whole Parking lot as a single unit then it is not empty see two cars are there on 4and 1st floor
now say you directly went into the building at floor 4 then moved down 3 levels to reach floor 1 and then exited thus answer is 3
.
code
cin>>m>>n;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
char c;
cin>>c;
cin.clear();
if(c==‘N’)
{
a[i][j]=0;
}
else
{
a[i][j]=1;
}
}
}
ll start={0},end={0};
for(i=1;i<=m;i++)
{
ll vals=P,vale=0;
for(j=1;j<=n;j++)
{
if(a[i][j]==1)
{
vals=min(vals,j);
vale=max(vale,j);
}
}
start[i]=vals;
end[i]=vale;
}
ll ans=0,cs=0,ce=0;
for(i=1;i<=m;i++)
{
if(start[i]!=P)
{
cs=i;
break;
}
}
for(i=m;i>=1;i–)
{
if(start[i]!=P)
{
ce=i;
break;
}
}
if(cs==0)
{
cout<<0<<endl;
continue;
}
else if(cs==ce)
{
ans+=abs(start[cs]-end[ce]);
cout<<ans<<endl;
continue;
}
else
{
vector v;
ans+=ce-cs;
for(i=cs;i<=ce;i++)
{
if(start[i]!=P)
{
if(i%2==1)
{
v.push_back(start[i]);
v.push_back(end[i]);
}
else
{
v.push_back(end[i]);
v.push_back(start[i]);
}
}
}
for(i=1;i<v.size();i++)
{
ans+=abs(v[i]-v[i-1]);
}
cout<<ans<<endl;
}

1 Like

Empty parking lot means that there is no free spot at any level of the matrix in which case the answer is 0. So, for the above test case the answer should be 3 ( moving downward 3 steps where each step counts as one operation ).

at first i tried that only here is the solution link : CodeChef: Practical coding for everyone

but I got WA for the same test cases both the times.

The 'P’s are three floors apart. So he will need at least 3 units to climb down.

look at my solution it is correct for this test case as well :

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

int main()
{
IOS
int t;
cin>>t;
while(t–)
{
int m,n;
cin>>m>>n;
vector<pair<int,int>> use(m);
for(int i=0;i<m;i++)
use[i]={-1,-1};
int count=0;
int start=302;

``````    char c;

for(int i=0;i<m;i++)
{
count=0;
for(int j=0;j<n;j++)
{
cin>>c;
if(c=='P') count++;
if(count==1 and c=='P')
{
if(start==302) start=i;
use[i].first=j;
use[i].second=j;
}

else if(c=='P' and count>1)
{
use[i].second=j;
}
}
}

int ans=0,j,flag;

for(int i=start;i<m;)
{
flag=0;
for(j=i+1;j<m;j++)
{
if(use[j].second!=-1)
{
flag=1;break;
}
}

if(flag)
{
ans+=j-i;
if(i%2==0)
ans+=abs(use[j].second-use[i].second);

else
ans+=abs(use[i].first - use[j].first);

}
if(use[i].second!=-1)
ans+=use[i].second-use[i].first;
i=j;

}
cout<<ans<<endl;
}
``````

}