can someone give me tricky test example that crash my code. Task --> http://www.spoj.com/problems/SHOP/
I can't find any mistake in my code so I am asking your help.
Code:
http://pastebin.com/6wuFWWJH
#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
struct koord
{
int x;
int y;
koord(int _x=0,int _y=0)
{
x=_x;
y=_y;
}
};
queue<koord>Q;
bool bio[150][150];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int dist[150][150];
char polje[150][150];
int a,b;
void bfs(int a1,int b1)
{
Q.push(koord(a1,b1));
bio[a1][b1]=true;
while(!Q.empty())
{
koord pos=Q.front();
Q.pop();
for(int i=0;i<4;++i)
{
koord dalje=koord(pos.x+dx[i],pos.y+dy[i]);
if(dalje.x>=0 && dalje.x<b && dalje.y>=0 && dalje.y<a)
{
if(polje[dalje.x][dalje.y]!='X' || polje[dalje.x][dalje.y]!='S')
if(bio[dalje.x][dalje.y]==false)
{
if(polje[dalje.x][dalje.y]=='D')
{
bio[dalje.x][dalje.y]=true;
dist[dalje.x][dalje.y]=dist[pos.x][pos.y];
Q.push(dalje);
}
else
{
bio[dalje.x][dalje.y]=true;
dist[dalje.x][dalje.y]=dist[pos.x][pos.y]+(polje[dalje.x][dalje.y]-'0');
Q.push(dalje);
}
}
if(polje[dalje.x][dalje.y]=='D' && dist[dalje.x][dalje.y]>dist[pos.x][pos.y])
{
dist[dalje.x][dalje.y]=dist[pos.x][pos.y];
Q.push(dalje);
}
else if(dist[dalje.x][dalje.y]>dist[pos.x][pos.y]+(polje[dalje.x][dalje.y]-'0'))
{
dist[dalje.x][dalje.y]=dist[pos.x][pos.y]+(polje[dalje.x][dalje.y]-'0');
Q.push(dalje);
}
}
}
}
}
int main()
{
scanf("%d%d",&a,&b);
while(a!=0 && b!=0)
{
int c=0,d=0,e=0,f=0;
for(int i=0;i<b;++i)scanf("%s",polje[i]);
//scanf("\n");
for(int i=0;i<b;++i)
for(int j=0;j<a;++j)
{
if(polje[i][j]=='S'){c=i;d=j;}
if(polje[i][j]=='D'){e=i;f=j;}
}
bfs(c,d);
printf("%d\n",dist[e][f]);
for(int i=0;i<150;++i)
for(int j=0;j<150;++j){bio[i][j]=false;dist[i][j]=0;}
Q.empty();
scanf("%d%d",&a,&b);
}
return 0;
}
I try some weird test example like :
3 3
S9D
1X1
111
But my program print 5, and that is good. So I need your help.
Mistake was here:
if(polje[dalje.x][dalje.y]!='X' || polje[dalje.x][dalje.y]!='S')
It should be
if(polje[dalje.x][dalje.y]!='X' && polje[dalje.x][dalje.y]!='S')
Related
Hi ,I am trying to solve the question PALIN on SPOJ where the idea is to find the next immediate palindrome of a number. I tested my code on the IDE with all possible test cases I found and the program is working fine but I can't figure out why I am getting SIGABRT error when I submit the solution.Please I request someone help me figure out the problem in the code For yor reference I am providing the link of the question:- https://www.spoj.com/problems/PALIN/
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
bool all_nines(string s)
{
for(int i=0;i<s.length();i++)
{
if(s[i]!='9')
return false;
}
return true;
}
int reverse(int a)
{
int rev=0;
while(a)
{
rev=(rev*10)+(a%10);
a/=10;
}
return rev;
}
void convert_and_compare(string &s,int low,int high)
{
int low_num,high_num;
string low_str="",high_str="";
for(int i=0,j=high;i<=low,j<s.size();i++,j++)
{
low_str+=s[i];
high_str+=s[j];
}
low_num=stoi(low_str);
high_num=stoi(high_str);
if(reverse(low_num)<=high_num)
{
low_num++;
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
}
else
{
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
}
s="";
s+=low_str+high_str;
}
void convert_and_compare(string &s,int low,int high,int mid)
{
int low_num,high_num,mid_num;
string low_str="",high_str="";
char mid_str;
for(int i=0,j=high;i<=low,j<s.size();i++,j++)
{
low_str+=s[i];
high_str+=s[j];
}
low_num=stoi(low_str);
high_num=stoi(high_str);
mid_num=(s[mid]-'0');
if(reverse(low_num)>high_num)
{
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
else if(reverse(low_num)<=high_num && mid_num!=9)
{
mid_num++;
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
else if(reverse(low_num)<=high_num && mid_num==9)
{
mid_num=0;
low_num++;
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
s="";
s=low_str+mid_str+high_str;
}
string find_next_palin(string s)
{
if(all_nines(s))
{
s[0]='1';
for(int i=1;i<s.length();i++)
s[i]='0';
s+='1';
return s;
}
else
{
if(s.length()%2==0)
{
int low,high;
low=s.length()/2-1;
high=s.length()/2;
convert_and_compare(s,low,high);
return s;
}
else
{
int low,high,mid;
mid=s.length()/2;
low=mid-1;
high=mid+1;
convert_and_compare(s,low,high,mid);
return s;
}
}
}
int main()
{
int t;
scanf("%d",&t);
vector <string> v(t);
for(int i=0;i<t;i++)
{
cin>>v[i];
}
for(int i=0;i<t;i++)
{
if(v[i].length()==1)
{
if(v[i]=="9")
{ printf("11");
printf("\n");
}
else
{
printf("%d",(v[i][0]-'0')+1);
printf("\n");
}
}
else
{
string temp;
temp=find_next_palin(v[i]);
for(int j=0;j<temp.length();j++)
cout<<temp[j];
printf("\n");
}
}
return 0;
}
In My Program what is logical error,
I don't understand Please Help me,,,I tried different Test case,,in which I found all Correct....
but in Codechef it's Wrong answer.
#include<iostream>
using namespace std;
int main()
{
int T,N,M;
cin>>T;
while(T)
{
cin>>N>>M;
int X,Y,S,c=0,d=0,i1=0,j1=0;
cin>>X>>Y>>S;
int x[X],y[Y];
if(X)
{
for(int i=0;i<X;i++)
cin>>x[i];
}
if(Y)
{
for(int i=0;i<Y;i++)
cin>>y[i];
}
for(int i=1;i<=N;i=i+S)
{
for(int j=1;j<=M;j=j+S)
{
for(int k=0;k<X;k++)
{
if(i<=x[k] && i+S-1>=x[k])
{
c=1;
i1=i-S+1;
}
}
for(int k=0;k<Y;k++)
{
if(j<=y[k] && j+S-1>=y[k] )
{
c=1;
j1=j-S+1;
}
}
if(c==0 && i+S-1<=N && j+S-1<=M)
{
d++;
}
c=0;
if(j1)
j=j1;
j1=0;
}
if(i1)
i=i1;
i1=0;
}
cout<<d<<endl;
T--;
}
}
The program(Question) in Codechef in beginners.
The link is here-enter link description here
Try this:
#include<iostream>
#define gc getchar_unlocked
using namespace std;
void fs(int &x)
{
register int c=gc();
x=0;
for(;c<48||c>57;c=gc());
for(;c>47&&c<58;c=gc())
x=(x<<1)+(x<<3)+c-48;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t,n,m,s,x,y,sum1,sum2,a,b;
fs(t);
while(t--)
{
fs(n);
fs(m);
fs(x);
fs(y);
fs(s);
sum1=0;
sum2=0;
a=0;
while(x--)
{
fs(b);
sum1=sum1+(b-a-1)/s;
a=b;
}
sum1=sum1+(n-a)/s;
This is the skyscraper floors problem from spoj. I m getting SIGABRT when I run the code.Link for the problem is http://www.spoj.com/problems/SCRAPER/. When I run this code on my PC, it runs without any error but spoj's online judge shows SIGABRT error.
#include <iostream>
#include <vector>
using namespace std;
struct floor
{
unsigned int floornum;
bool calldone;
vector <floor*> connection;
floor()
{
floornum=0;
calldone=0;
}
};
struct elevator
{
unsigned int x,y;
elevator()
{
x=y=0;
}
};
struct skyscrapper
{
unsigned int f,e,a,b;
vector <elevator> ele;
vector <floor*> flooraddress; //this is a spoj problem
void read()
{
cin>>f>>e>>a>>b;
elevator *temp;
for(unsigned int i=0;i<e;i++)
{
temp=new elevator;
cin>>(*temp).x>>(*temp).y;
ele.push_back(*temp);
}
for (unsigned int i=0;i<f;i++)
{
floor* tempp=new floor;
(*tempp).floornum=i;
flooraddress.push_back(tempp);
}
}
void allotaddress()
{
unsigned int j,k;
for(unsigned int i=0;i<ele.size();i++)
{
j=ele[i].y;
k=ele[i].x;
while(j<f)
{
if(j!=ele[i].y)
{
(*(flooraddress[j])).connection.push_back(flooraddress[j-k]);
(*(flooraddress[j-k])).connection.push_back(flooraddress[j]);
}
j=j+k;
}
}
}
};
bool flag;
bool traverse(floor* m,int destination)
{
if((*m).calldone==1)
{
return 0;
}
(*m).calldone=1;
if((*m).floornum==destination)
return 1;
if((*m).connection.empty())
return 0;
for(int i=0;i<(*m).connection.size();i++)
{
flag=traverse(((*m).connection[i]),destination);
if(flag==1)
return flag;
}
return 0;
}
int main()
{
int n;
cin>>n;
skyscrapper iit[n];
bool ans[n];
for(int i=0;i<n;i++)
{
iit[i].read();
iit[i].allotaddress();
ans[i]=traverse(iit[i].flooraddress[iit[i].a],iit[i].b);
}
for(int i=0;i<n;i++)
{
if(ans[i]==1)
cout<<"It is possible to move the furniture."<<endl;
else
cout<<"The furniture cannot be moved."<<endl;
}
return 0;
}
The following is the implementation of http://www.spoj.pl/problems/LITE/ using Segment Tree's with lazy propagation. I am new to segment trees and I cannot understand why I am getting TLE. Could someone please look at it and help me correct my error?
#include <iostream>
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX 100000
using namespace std;
int M[2*MAX+1];
int flag[2*MAX+1];
int count;
void refresh(int begin,int end,int n)
{
M[n] = end-begin+1 - M[n];
flag[n]=0;
flag[n*2] =!flag[n*2];
flag[n*2+1] =!flag[n*2+1];
}
void update(int begin,int end,int i,int j,int n=1)
{
if(flag[n])
{
refresh(begin,end,n);
}
if(begin>=i && end<=j)
{
if(!flag[n])
{
refresh(begin,end,n);
}
flag[n] = 0;
return;
}
else if(begin>=end)
{
return;
}
else
{
int mid = (begin+end)>>1;
if(i<=mid)
{
update(begin,mid,i,j,n*2);
}
if(j>mid)
{
update(mid+1,end,i,j,n*2+1);
}
if(flag[2*n])
{
refresh(begin,mid,2*n);
}
if(flag[2*n+1])
{
refresh(mid+1,end,2*n+1);
}
M[n] = M[n*2]+ M[n*2+1];
}
}
int query(int begin,int end,int i,int j,int n=1)
{
if(flag[n])
{
refresh(begin,end,n);
}
if(begin>=i && end<=j)
{
return M[n];
}
if(begin>=end)
{
return 0;
}
int mid = (begin+end)>>1;
int l=0,r=0;
if(i<=mid)
{
l = query(begin,mid,i,j,n*2);
}
if(j>mid)
{
r = query(mid+1,end,i,j,n*2+1);
}
if(flag[2*n])
{
refresh(begin,mid,2*n);
}
if(flag[2*n+1])
{
refresh(mid+1,end,2*n+1);
}
M[n] = M[n*2]+ M[n*2+1];
return l+r;
}
int main()
{
memset(M,0,sizeof M);
int n,m,a,b,c;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
if(a==0)
{
update(1,n,b,c);
}
else
{
printf("%d\n",query(1,n,b,c));
}
}
return 0;
}
M[node]^=1; might be faster than M[node] = (M[node]==0)?1:0;, and (begin+end)>>1 faster than (begin/end)/2, but not very relevant
LE: Try if making the recursive functions inline will run faster. I think it unravels the recursion a couple of times and works a little bit faster. Maybe sending the parameters as references will make it run faster, try that out. If the test cases are chosen properly you still shouldn't be able to pass the tests with this trickery, but it helps sometimes.
why my program Time Limited Error?
because of the sort?
this is the question
link text
#include <cstdio>
#include <cstring>
using namespace std;
int map[22][44];
int book[22];
int total;
int sum;
int way[22];
int tails[22];
int tail;
void init()
{
memset(map,0,sizeof(map));
memset(book,0,sizeof(book));
sum =0;
memset(way,0,sizeof(way));
way[1]=1;
memset(tails,0,sizeof(tails));
}
void sort()
{
int t;
for (int i=1;i<=22;i++)
{
if (tails[i]==0)
break;
else
{
for (int j=1;j<=tails[i]-1;j++)
for (int k=j+1;k<=tails[i];k++)
{
if (map[i][j] > map[i][k])
{
t = map[i][j];
map[i][j]=map[i][k];
map[i][k]=t;
}
}
}
}
}
void dfs(int x,int y)
{
if ((x < 1)||(x > 22))
return;
if (book[x]==1)
return;
//printf("%d \n",x);
if (x == total)
{
sum++;
for (int i=1;i<=y-1;i++)
{
printf("%d ",way[i]);
}
printf("%d",total);
printf("\n");
return;
}
tail = tails[x];
for (int i=1;i<=43;i++)
{
book[x]=1;
way[y]=x;
dfs(map[x][i],y+1);
book[x]=0;
}
}
int main()
{
int temp1,temp2;
//freopen("ex.in","r",stdin);
//freopen("ex.out","w",stdout);
int c = 0;
while(scanf("%d",&total)!=EOF)
{
c++;
printf("CASE ");
printf("%d",c);
printf(":");
printf("\n");
init();
for (;;)
{
scanf("%d%d",&temp1,&temp2);
if ((temp1 == 0)&&(temp2 == 0))
break;
else
{
tails[temp1]++;
tail = tails[temp1];
map[temp1][tail]=temp2;
tails[temp2]++;
tail = tails[temp2];
map[temp2][tail]=temp1;
}
}
sort();
dfs(1,1);
printf("There are ");printf("%d",sum);printf(" routes from the firestation to streetcorner ");printf("%d",total);printf(".");
printf("\n");
}
return 0;
}
Because your sorting algoritm is in worst-case O(n*n), you can use InnoSort for better worst-case complexity O(n*log(n)).
You are using C++ then use sort function from <algorithm> header to do this simplest.
Documentation you can find at http://www.sgi.com/tech/stl/sort.html
For a start, you're accessing tails and map past the end. C++ arrays are zero-indexed, so the first element is 0, and the last valid elements are tails[21] and map[21][43].